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...
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">"hello"</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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With