pythonmarkdownpygmentscodehighlighter

Python Markdown - Not adding codehilite


I am using Flask to write a Blog, with that I am using Markdown's Python library to generate HTML for me, I am willing to have syntax highlighting, therefore I am using markdown.markdown(string, extensions=['codehilite']

According to their wiki, it should add a html class;

<div class="codehilite"><pre><code># Code goes here ...</code></pre></div>

But it doesn't seem to be working, following the tryouts from my interpreter;

In [9]: markdown.version
Out[9]: '2.3.1'

In [10]: text = """:::python
   ....: import os
   ....: print "This is a text!"
   ....: """

In [11]: html = markdown.markdown(text, extensions=['codehilite'])

In [12]: html
Out[12]: u'<p>:::python\nimport os\nprint "This is a text!"</p>'

In [13]: # Even more funnier, when following the examples in the usage section "..['codehilite(linenums=True)']

In [14]: html = markdown.markdown(text, extensions=['codehilite(linenums=True)'])

In [15]: html
Out[15]: u'<p>:::python\nimport os\nprint "This is a text!"</p>'

In [16]: # No line numbers, or any class..

I am not sure what's the problem here, I have Pygments installed, I already upgraded Markdown's lib, but nothing. The expected results here would be that Markdown would add the html class codehilite so I will be able to get the syntax working. What seems to be the problem here?


Solution

  • I found another solution, markdown2

    Here are a few examples (following what I was willing..)

    In [1]: import markdown2
    
    In [2]: markdown2.markdown("> This is a paragraph and I am **bold**")
    Out[2]: u'<blockquote>\n  <p>This is a paragraph and I am <strong>bold</strong></p>\n</blockquote>\n'
    
    In [3]: code = """```python
    if True:
        print "hi"
    ```"""
       ...: 
    
    In [4]: markdown2.markdown(code, extras=['fenced-code-blocks'])
    Out[4]: u'<div class="codehilite"><pre><code><span class="k">if</span> <span class="bp">True</span><span class="p">:</span>\n    <span class="k">print</span> <span class="s">&quot;hi&quot;</span>\n</code></pre></div>\n'