Incorrect code block conversion
XDanielPaul opened this issue · 8 comments
Issue
The code block
``` c
const var = foo()
```
is being converted to:
<pre><code><div class="codehilite">
<pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"></span>
</code></pre>
</div>
</code></pre>
and should be converted to:
<div class="codehilite">
<pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"></span>
</code></pre>
</div>
Observation
During the conversion, the input text passes through function _do_code_blocks(self, text)
which hashes the <div class="codehilite"></div>
and it's content and wraps them in <pre><code></code></pre>
Output of _do_code_blocks(self, text)
:
'<pre><code>md5-353f226db77851404d4ad30e2cb646b8\n</code></pre>\n'
Output of _do_code_blocks(self, text)
is passed as input through function _unescape_special_chars(self, text)
and hash is converted to following output:
<pre><code><div class="codehilite">
<pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"></span>
</code></pre>
</div>
</code></pre>
Steps to reproduce:
-
create markdown file and paste the text:
``` c
const var = foo()
``` -
python /lib/markdown2.py --extras fenced-code-blocks foo.md > foo.html
Hi there! I tried this and was unable to reproduce this issue. What version of the library are you using?
Hi @Crozzers! I have cloned the master branch, so I suppose I am using the most recent one
I'm afraid I couldn't reproduce the issue. I cloned master, uninstalled pygments, reinstalled pygments, removed the trailing newline from the file and couldn't get it.
Is that the whole markdown snippet or are you converting other stuff as well?
I can reproduce it if I indent the input like so:
``` c
const var = foo()
```
Is that what is happening?
Oh I see what happened... Bad on my part. I have pasted the code and markdown formatter has removed the indent.
``` c
const var = foo()
```
This is the concerned code, the indent is only inside the codeblock
Also one more thing I found out, but I might create a separate issue if it's not correct behaviour:
Plain text
``` c
const var = foo()
```
Gets converted to
<p>Plain text
<div class="codehilite">
<pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">var</span><span class="> </code></pre>
</div></p>
Is the code block supposed to be wrapped in <p></p>
?
I don't know about the <p>
tags but I have a pretty good idea of why the codeblock is getting messed up.
It's probably detecting the indented code as a code block, not realizing it is inside a fenced code block. I'll look into a fix for that
Thanks a lot!