Domanda

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?

È stato utile?

Soluzione 2

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'

Altri suggerimenti

I've established that codehilite, aside from being generally temperamental, breaks when there is a list immediately before it:

This markdown, and variations of it, just doesn't work:

* apples
* oranges

    #!python
    import os

But if I put something between the list and the code, then it does work:

* apples
* oranges

Put something between the code and the list

    #!python
    import os

But it's generally unpredictable. I tried a zillion combinations with very mixed success replicating what a read in the documentation. Not happy...

Use fenced_code instead

Then I wandered into the other sub-extensions of pygments and tried adding the fenced_code extension explicitly and retrying the fenced code examples. Works better.

So proceeding with

pygmented_body = markdown.markdown(rendered_body, 
                                   extensions=['codehilite', 'fenced_code'])

I'm having much greater success using fenced code exclusively:

* Don't need to indent 4 spaces
* Don't need something between the list and the code

~~~~{.python hl_lines='3'}
import os

print('hello, world')
~~~~

And final comments here.

i' m sorry see your question just now.

in the python-markdown, you need 4 spaces in each line of code.

In [13]: text = """
   ....:        :::python
   ....:        import os
   ....:        """

In [14]: markdown.markdown(text, extensions = ['codehilite'])
Out[14]: u'<div class="codehilite"><pre><span class="kn">import</span> 
<span class="nn">os</span>\n</pre></div>'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top