Ejemplo n.º 1
0
    def save(self, *args, **kwargs):
        self.modified_time = timezone.now()

        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
        ])

        self.excerpt = strip_tags(md.convert(self.body))[:54]

        super().save(*args, **kwargs)
Ejemplo n.º 2
0
 def save(self, *args, **kwargs):
     
     if not self.excerpt:
         md = markdown.Markdown(extensions=[
                 'markdown.extensions.extra',
                 'markdown.extensions.codehilite',
             ])
         
         self.excerpt = strip_tags(md.convert(self.body))[:54]
         
     super(Post, self).save(*args, **kwargs)
Ejemplo n.º 3
0
def detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    # 先实例化了一个 markdown.Markdown 对象 md
    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 render(request, 'blog/detail.html', context={'post': post})
Ejemplo n.º 4
0
def article_detail(request, id):
    article = ArticlePost.objects.get(id=id)
    # 取出对应文章的评论
    comments = Comment.objects.filter(article=id)

    article.total_views += 1
    article.save(update_fields=['total_views'])
    md = markdown.Markdown(extensions=[
        # 包含 缩写、表格等常用扩展
        'markdown.extensions.extra',
        # 语法高亮扩展
        'markdown.extensions.codehilite',
        # 目录扩展
        'markdown.extensions.toc',
    ])
    article.body = md.convert(article.body)
    context = {'article': article, 'toc': md.toc, 'comments': comments}
    # 载入模板,并返回context对象p
    return render(request, 'article/detail.html', context)
Ejemplo n.º 5
0
def makeExtension(configs={}):
    return MacroExtension(configs=configs)


#-- sample macros


@macro
def HelloWorld():
    """Hello world macro."""
    return "<b>Hello, world</b>."


@macro
def ListOfMacros():
    """Lists all available macros."""
    out = ""
    out += "<ul>"
    for name, macro in macrostore.items():
        out += '  <li><b>%s</b>: %s</li>\n' % (name, macro.__doc__ or "")
    out += "</ul>"
    return out


if __name__ == "__main__":
    text = "{{HelloWorld()}}"
    md = markdown.Markdown(source=text, safe_mode=False)
    MacroExtension().extendMarkdown(md, {})
    html = md.convert()
    print replace_macros(html, md.macros)