def article_detail_handler(request, article_id):
account = request.META["user_info"]
result = dict()
renderer = mistune.Renderer(hard_wrap=True, parse_inline_html=True)
markdown = mistune.Markdown(renderer=renderer)
article = BlogArticle.query_article_by_id(article_id)
article["content"] = markdown(article["content"])
article["comment_list"] = BlogComment.query_comment_list(article_id, account.id if account else 0)
result["article"] = article
if account:
article["meta_info"]["liked"] = 1 if BlogLike.query_like_blog(account.id, article_id) else 0
result["user_info"] = UserInfo.query_format_info_by_user_id(request.META["user_info"].id)
# ?????? ????????????????????
author_id = article["user_info"]["id"]
if not account or author_id != account.id:
BlogArticleMeta.change_meta_record(article_id, author_id, ["hit"])
# ????
BlogArticle.query_article_by_id(article_id, False)
return render(request, "article/detail.html", result)
python类Renderer()的实例源码
def edit_article_handler(request, article_id):
"""
???? ????
"""
result = base_result()
user_id = request.META["user_info"].id
if article_id != "0":
result["data"] = BlogArticle.query_article_by_id(article_id)
renderer = mistune.Renderer(hard_wrap=True, parse_inline_html=True)
markdown = mistune.Markdown(renderer=renderer)
result["mark_down"] = markdown(result["data"]["content"])
else:
result["mark_down"] = None
del result["data"]
result["kind_list"] = BlogKind.query_user_kind(1)
result["user_info"] = UserInfo.query_format_info_by_user_id(user_id)
return render(request, "manage/edit.html", result)
def markdown_engin(ori_str=""):
renderer = mistune.Renderer(hard_wrap=True, parse_inline_html=True)
markdown = mistune.Markdown(renderer=renderer)
return markdown(ori_str)
def get_markdown_renderer( url_transform ):
''' This function returns a markdown renderer which transforms
URLs according to the relative paradigm of mkaestatic.
'''
class MyRenderer( mistune.Renderer ):
def link(self, link, title, text):
link = url_transform(link)
return super(MyRenderer, self).link(link, title, text)
def image(self, src, title, alt_text):
src = url_transform(src)
return super(MyRenderer, self).image(src, title, alt_text)
return mistune.Markdown(renderer=MyRenderer(escape=True, use_xhtml=True))
def __init__(self, renderer=None, inline=None, block=None, **kwargs):
if not renderer:
renderer = Renderer(**kwargs)
else:
kwargs.update(renderer.options)
super(Markdown, self).__init__(
renderer=renderer, inline=inline, block=block)