Empty lines in fenced code block starts a new code block when using pygments #88

Closed
colinta opened this Issue May 7, 2012 · 3 comments

2 participants

@colinta
def fine():
    # so far so good

    return 'how is this?'

If you are not using pygments, it generates the <pre><code> block correctly:

<pre><code>def fine():
    # so far so good

    return 'how is this?'
</code></pre>

And pygments can parse this just fine:

>>> from pygments import highlight
>>> from pygments.lexers import PythonLexer
>>> from pygments.formatters import HtmlFormatter
>>> 
>>> code = '''def fine():
...     # so far so good
... 
...     return 'how is this?'
... '''
>>> print highlight(code, PythonLexer(), HtmlFormatter())
<div class="highlight"><pre><span class="k">def</span> <span class="nf">fine</span><span class="p">():</span>
    <span class="c"># so far so good</span>

    <span class="k">return</span> <span class="s">&#39;how is this?&#39;</span>
</pre></div>

But running it through Markdown2 inserts an extra <pre><code>:

<div class="codehilite"><pre><code><span class="k">def</span> <span class="nf">fine</span><span class="p">():</span>
    <span class="c"># so far so good</span>

<pre><code>&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;how is this?&amp;#39;&lt;/span&gt;
</code></pre>

</code></pre></div>

(version 1.4.2)

@colinta

Line 3 does not have to be a blank line. Even if it is not blank, I get extra <pre><code> tags..

@colinta

I wanted to make sure it wasn't something my code was doing, which I didn't think likely, but you never know. Here is some simple code that recreates the bug:

import markdown2
import pygments
markdowner = markdown2.Markdown(extras=["fenced-code-blocks", "header-ids",
    "code-friendly"])
# markdown filter
def markdown(markdown):
    return markdowner.convert(markdown).strip()

markdown('''```python
def fine():
    # so far so good

    return 'how is this?'
\```
''')  # remove the forward slash there - it's to prevent github from closing this code block

returns

<div class="codehilite">
<pre><code><span class="k">def</span> <span class="nf">fine</span><span class="p">():</span>
    <span class="c"># so far so good</span>

<pre><code>&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;how is this?&amp;#39;&lt;/span&gt;
</code></pre>

</code></pre></div>
@nicholasserra
Collaborator

Fixed in f2c4b4f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment