def detail(request, pk): article = get_object_or_404(Article, pk=pk) article.increase_readcnt() md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) article.content = md.convert(article.content) article.toc = md.toc comment_list = article.comment_set.all() context = {'article': article, 'comment_list': comment_list} return render(request, 'blog/post.html', context=context)
def get_object(self, queryset=None): # 覆写 get_object 方法的目的是因为需要对 article 的 body 值进行渲染 article = super(ArticleDetailView, self).get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.tables', TocExtension(slugify=slugify), ]) article.body = md.convert(article.body) article.toc = md.toc return article
def get_object(self, queryset=None): post = super(PostDetailView, self).get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', TocExtension(slugify=slugify), ]) post.body = md.convert(post.body) post.toc = md.toc return post
def detail(request, pk): post = get_object_or_404(Post, pk=pk) md = markdown.Markdown( extensions={ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) }) post.body = md.convert(post.body) post.toc = md.toc return render(request, template_name='blog/detail.html', context={'post': post})
def get_object(self): obj = super(DetailView, self).get_object() # 阅读量+1 obj.increase_views() md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) obj.content = md.convert(obj.content) obj.toc = md.toc return obj
def get_object(self, queryset=None): # 覆写 get_object 方法的目的是因为需要对 post 的 body 值进行渲染 post = super(PostDetailView, self).get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', # 语法高亮拓展 'markdown.extensions.toc', # 允许自动生成目录 TocExtension(slugify=slugify), # 美化标题的锚点 URL ]) post.body = md.convert(post.body) post.toc = md.toc return post
def get_object(self, qureyset=Post.objects.all()): post = super().get_object(queryset=Post.objects.all()) #进行markdown处理 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) post.content = md.convert(post.content) #增加目录 post.toc = md.toc #返回对象 return post
def get_object(self, queryset=None): post = super().get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', #语法高亮拓展 'markdown.extensions.codehilite', #自动生成目录 TocExtension(slugify=slugify), ]) post.body = md.convert(post.body) post.toc = md.toc return post
def generate_rich_content(value): md = Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) content = md.convert(value) result = re.search(r'<ul>(.*)</ul>', md.toc, re.S) if result: toc = result.group(1) else: toc = "" return {'content': content, 'toc': toc}
def convert_markdown(text): from _1327.documents.markdown_internal_link_extension import InternalLinksMarkdownExtension md = markdown.Markdown(extensions=[ EscapeHtml(), TocExtension(baselevel=2), InternalLinksMarkdownExtension(), '_1327.minutes.markdown_minutes_extensions', '_1327.documents.markdown_scaled_image_extension', 'markdown.extensions.abbr', 'markdown.extensions.tables', ]) return md.convert(text + abbreviation_explanation_markdown()), md.toc
def get_object(self, queryset=None): post = super().get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) post.body = md.convert(post.body) m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) post.toc = m.group(1) if m is not None else '' return post
class Settings: HEADER_PATH = 'html/header.html' FOOTER_PATH = 'html/footer.html' MD_PATH = 'report.md' OUT_PATH = 'report.html' MD_EXTENSIONS = [ 'markdown.extensions.footnotes', 'markdown.extensions.tables', 'markdown.extensions.codehilite', TocExtension(baselevel=1, title='Table of Contents'), ]
def detail(request, pk): post = get_object_or_404(Post, pk=pk) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) post.body = md.convert(post.body) post.toc = md.toc m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) post.toc = m.group(1) if m is not None else '' return render(request, 'blog/detail.html', context={'post': post})
def get_object(self, queryset=None): # 复写get_object方法的目的是国为需要对post的body值进行渲染 post = super().get_object() # 对post的content进行markdown处理 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) post.content = md.convert(post.content) post.toc = md.toc return post
def toc_markdown(article): md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.fenced_code', 'markdown.extensions.codehilite', 'markdown.extensions.tables', 'markdown.extensions.toc', TocExtension(slugify=slugify) ]) article.content = md.convert(article.content) article.toc = md.toc return article
def generate_rich_content(value): md = markdown.Markdown(extensions=[ "markdown.extensions.extra", "markdown.extensions.codehilite", # 记得在顶部引入 TocExtension 和 slugify TocExtension(slugify=slugify), ]) content = md.convert(value) content = emoji.emojize(content, use_aliases=True) m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) toc = m.group(1) if m is not None else "" return {"content": content, "toc": toc}
def get_object(self, queryset=None): # 这里覆写的主要目的是获取文章的主体,并进行Markdown渲染 post = super(DetailView, self).get_object(queryset=None) # post.body = markdown2.markdown(post.body, extras=['fenced-code-blocks']) # 覆写 get_object 方法的目的是因为需要对 post 的 body 值进行渲染 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) post.body = md.convert(post.body) post.toc = md.toc return post
def get_object(self, queryset=None): blog = super(BlogDetailView, self).get_object(queryset=None) blog.increase_view_times() # Markdown 类实例 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) blog.content = md.convert(blog.content) blog.toc = md.toc # 动态属性 return blog
def get_object(self, queryset=None): # 覆写 get_object 方法的目的是因为需要对 post 的 body 值进行渲染 post = super(PostDetailView, self).get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', # 记得在顶部引入 TocExtension 和 slugify TocExtension(slugify=slugify), ]) post.body = md.convert(post.body) post.toc = md.toc return post
def save(self, *args, **kwargs): md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', # 添加代码高亮扩展 'markdown.extensions.codehilite', # 添加目录扩展 # 'markdown.extensions.toc', TocExtension(slugify=slugify) ]) self.content_html = md.convert(self.content) self.toc_html = md.toc self.excerpt = strip_tags(self.content_html[:100]) super(Article, self).save(*args, **kwargs)
def wiki_view(request, pk): post = Post.objects.select_related('author').get(id=pk) post.increase_views() md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', TocExtension(slugify=slugify), ]) post.md_content = md.convert(post.md_content) return render(request, 'wiki/wiki_view.html', { 'post': post, 'toc': md.toc })
def get_object(self, queryset=None): article = super(ArticleDetailView, self).get_object(queryset=None) #实例化一个markdown.markdown类,并传入参数 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', # 转换html结构文本 'markdown.extensions.codehilite', #高亮标记代码 TocExtension(slugify=slugify), #自动生成文章目录 ]) article.content = md.convert( article.content) #利用实例方法convert()将文章内容渲染成HTML文本 article.toc = md.toc #给文章添加目录 return article
def detail(request, pk): post = get_object_or_404(Post, pk=pk) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', # 基本扩展 'markdown.extensions.fenced_code', # 语法高亮 TocExtension(slugify=slugify), # 允许自动生成目录 ]) post.body = md.convert(post.body) # 空目录不处理 m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) # 动态添加属性,动态语言的好处 post.toc = m.group(1) if m is not None else '' return render(request, 'blog/detail.html', context={'post': post})
def detail(request, pk): task = get_object_or_404(Task, pk=pk) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) task.body = md.convert(task.body) m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) task.toc = m.group(1) if m is not None else '' return render(request, 'web/single.html', context={'task': task})
def detail(request, pk): post = get_object_or_404(Post, pk = pk) tags = Tag.objects.all() post.increase_views() md = markdown.Markdown(extensions = [ 'markdown.extensions.extra', 'markdown.extensions.codehilite', # 'markdown.extensions.toc', TocExtension(slugify = slugify) ]) post.body = md.convert(post.body) post.toc = md.toc return render(request, 'blog/detail.html', {'post': post, 'tags': tags})
def get_object(self, queryset=None): # 调用父类的get_object方法。 post = super().get_object() # 对post的content进行markdown处理。 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) post.content = md.convert(post.content) # 讲markdown的目录赋值给post post.toc = md.toc return post
def get_object(self, queryset=None): # 重写get_object方法 对post的body值进行渲染 post = super(PostDetailViews, self).get_object(queryset=None) md = markdown.Markdown( extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', # 语法高亮扩展 TocExtension(slugify=slugify) ]) post.body = md.convert(post.body) post.toc = md.toc return post
def generate_rich_content(value): # 配置 markdown 转 html 处理 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify), ]) content = md.convert(value) # re.S 将toc作为一整个字符串匹配,不逐行匹配 m = re.search(r'<div class="toc">\s*<ul>(.*)</ul>\s*</div>', md.toc, re.S) # group(1) 返回第一个()中匹配的字符串 toc = m.group(1) if m is not None else '' return {'content': content, 'toc': toc}
def get_object(self, queryset=None): # 调用父类的get_object,以获取对象 self.object = super().get_object(queryset=None) md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', TocExtension(slugify=slugify) ]) self.object.content = md.convert(self.object.content) self.object.toc = md.toc # 返回object return self.object
def testPermalinkWithUnicodeTitle(self): from markdown.extensions.toc import slugify_unicode self.assertMarkdownRenders( '# Unicode ヘッダー', '<h1 id="unicode-ヘッダー">' # noqa 'Unicode ヘッダー' # noqa '<a class="headerlink" href="#unicode-ヘッダー" title="パーマリンク">¶</a>' # noqa '</h1>', # noqa extensions=[ TocExtension(permalink=True, permalink_title="パーマリンク", slugify=slugify_unicode) ])
def extendMarkdown(self, md): if 'toc' in settings.METHODS: TocExtension.extendMarkdown(self, md)