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