Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown2 how to get "extras" working?

It is unclear to me how to get syntax hilighting with python's markdown2 library.

text = """```if True: 
print "hello"```"""
markdown2.markdown(text,extras=['fenced-code-blocks'])
u'<p><code>if True:\nprint "hello"</code></p>\n'

It seems the "extra" fenced-code-blocks is not working as the output is the same as without it

markdown2.markdown(text)
u'<p><code>if True:\nprint "hello"</code></p>\n'

Whereas the output should have all the span classes for a css file to highlight, like

<pre><code><span class="k">if</span> <span class="bp">True</span>, etc...
like image 585
yayu Avatar asked Oct 19 '25 13:10

yayu


1 Answers

Currently, the problem is that markdown2 doesn't know which language your code snippit is and so won't know how to parse it/add syntax highlighting. You'll need to modify your Markdown to be more explicit:

import markdown2

text = """
```python
if True: 
    print "hello"
```
"""

print markdown2.markdown(text,extras=['fenced-code-blocks'])

This produces the following output:

<div class="codehilite"><pre><code><span class="k">if</span> <span class="bp">True</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;hello&quot;</span>
</code></pre></div>

Note that you must also have the pygments library installed, and must provide one of the following CSS files for the final HTML file.

like image 124
Michael0x2a Avatar answered Oct 22 '25 04:10

Michael0x2a



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!