3

Is there a way to parse a text file to output styling of Github Flavored Markdown (GFM) in python 2.7?

There are plenty of examples on this site and elsewhere that provide the pygments/jinja2 syntax highlighting guide like this:

{% 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. 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:

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:

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. 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?

Thanks for your help.

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

Jared
  • 3,651
  • 11
  • 39
  • 64

2 Answers2

0

The python-markdown library has support for pluggable extensions. There are standard extensions and many more custom ones made by 3rd parties. https://github.com/google/py-gfm implements Github Flavored Markdown. It's simple to add this extension to Python Markdown.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
  • Can you guide me on how to implement this? – Jared Sep 06 '15 at 15:56
  • Sure. First tell me what toolset / environment you're using. Is this straight-up Python; or are you using Mkdocs or a similar doco package; or ? – Chris Johnson Sep 06 '15 at 15:57
  • Plain python. I store a post in my db, and want apply this markdown function (with the correct extensions) to it. Then I will store the stylized post and the raw post for future editing in the db. – Jared Sep 06 '15 at 16:01
  • I believe I just have to pass gfm to `markdown.markdown(some_text, extensions=[MyExtension(), 'path.to.my.ext', 'markdown.extensions.footnotes'])`. The google repo is installed, but if I run `markdown.markdown('Testing', extensions=[gfm])` I get the error `Extension "__builtin__.module" must be of type: "markdown.Extension"`. – Jared Sep 06 '15 at 16:04
  • The py-gfm package comes with multiple different extensions all packaged together. You need to specify which parts you want. If you want everything, try this: `markdown.markdown(some_text, extensions=['mdx_gfm'])`. – Waylan Sep 06 '15 at 23:53
  • Regarding using templates for highlighting code blocks, see [this](http://stackoverflow.com/a/31862421/866026) and for a better explanation of why you shouldn't try to use templates for this sort of thing, see [this](http://stackoverflow.com/a/32254521/866026). – Waylan Sep 06 '15 at 23:56
  • Note that py-gfm doesn't work with markdown3.0. Quoting from [changelog](https://github.com/Zopieux/py-gfm/blob/master/CHANGELOG.md): *Pin Markdown dependency to <3.0, as Markdown 3.0 introduced breaking changes to its internal API (issue #13). A future release of py-gfm that is compatible with Markdown ≥3.0 is planned, with no ETA.* – wastl Nov 22 '18 at 19:44
0

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

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207