简体   繁体   English

Python中的Github Flavored Markdown

[英]Github Flavored Markdown in Python

Is there a way to parse a text file to output styling of Github Flavored Markdown (GFM) in python 2.7? 有没有办法解析文本文件以在python 2.7中输出Github Flavored Markdown(GFM)的样式?

There are plenty of examples on this site and elsewhere that provide the pygments/jinja2 syntax highlighting guide like this: 这个网站和其他地方有很多例子提供pygments / jinja2语法高亮指南,如下所示:

{% highlight 'python' %}
def testing(x):
    print x
{% endhighlight %}

but I'd like to format my whole post using markdown similar to writing this question and then passing it to a jinja2 filter to apply styles. 但我想使用markdown格式化我的整个帖子,类似于编写这个问题,然后将其传递给jinja2过滤器以应用样式。 The above snippet would only work if I could predetermine where the blocks of code were and treat them separately than the rest of the text file. 上面的代码片段只有在我可以预先确定代码块的位置时才会起作用,并将它们分开处理,而不是文本文件的其余部分。

I've found code maintained by Google ( https://github.com/google/py-gfm ) which I believe is the right track here, but I only have available these extensions: 我发现Google维护的代码( https://github.com/google/py-gfm )我认为这是正确的跟踪,但我只提供以下扩展程序:

In [10]: gfm.
gfm.AutolinkExtension       gfm.SpacedLinkExtension     gfm.hidden_hilite
gfm.AutomailExtension       gfm.StrikethroughExtension  gfm.semi_sane_lists
gfm.HiddenHiliteExtension   gfm.autolink                gfm.spaced_link
gfm.SemiSaneListExtension   gfm.automail                gfm.strikethrough

with no clear idea of how to parse my string/text file to output what I need. 我不清楚如何解析我的字符串/文本文件以输出我需要的东西。

Right now, I pass my post to a filter called markdown: {{ post.body|markdown() }} where markdown is defined: 现在,我将我的帖子传递给名为markdown的过滤器: {{ post.body|markdown() }}其中定义了markdown:

def markdown(code):
    from pygments import highlight
    from pygments.lexers import PythonLexer
    from pygments.formatters import HtmlFormatter

    return highlight(code, PythonLexer(), HtmlFormatter())

This is where I am now-- but this treats the entire post like a code block and highlights according to python's syntax rules. 这就是我现在所处的位置 - 但这会像整个帖子一样对待代码块并根据python的语法规则进行突出显示。 Is there already available or a way to write a filter github_markdown() that will take my raw post body (similar to this post) and add styles and links how I'd like? 是否已经有可能或者一种方法来编写一个过滤器github_markdown() ,它将采用我的原始帖子体(类似于这篇文章)并添加样式和链接我的喜好?

Thanks for your help. 谢谢你的帮助。

Stack: Ubuntu 14.04, Python 2.7, Pygments 2.02, Flask 0.10.1, Jinja2, MongoDB 3.0.6 Stack:Ubuntu 14.04,Python 2.7,Pygments 2.02,Flask 0.10.1,Jinja2,MongoDB 3.0.6

The python-markdown library has support for pluggable extensions. python-markdown库支持可插入扩展。 There are standard extensions and many more custom ones made by 3rd parties. 有第三方制作的标准扩展和更多定制扩展。 https://github.com/google/py-gfm implements Github Flavored Markdown. https://github.com/google/py-gfm实现了Github Flavored Markdown。 It's simple to add this extension to Python Markdown. 将此扩展添加到Python Markdown很简单。

Given how much of a pain this seems to be, I've decided to combine mistune and pygments to give me what I wanted: https://github.com/asottile/markdown-code-blocks 鉴于这似乎有多大的痛苦,我决定将mistunepygments结合起来给我想要的东西: https//github.com/asottile/markdown-code-blocks

The usage is pretty straightforward: 用法非常简单:

pip install markdown-code-blocks

The library provides a single function 该库提供单一功能

markdown_code_blocks.highlight(markdown_s)

You can style the output with typical pygments themes 您可以使用典型的pygments主题设置输出样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM