带有脚注的Python Markdown

时间:2014-02-19 09:58:58

标签: python markdown

我没有从Python Markdown的脚注扩展中得到我的期望。

import markdown

content = "Footnotes[^1] have a label[^@#$%] and the footnote's content.\
           \
           [^1]: This is a footnote content.\
           [^@#$%]: A footnote on the label: @#$%."


htmlmarkdown=markdown.markdown( content, extensions=['footnotes'] )
print htmlmarkdown

结果是:

<p>Footnotes[^1] have a label[^@#$%] and the footnote's content.[^1]: This is a footnote content.[^@#$%]: A footnote on the label: @#$%.</p>

脚注根本没有被解析!那是为什么?

1 个答案:

答案 0 :(得分:6)

您的行中没有换行符。线条末尾的\仅允许您将字符串放在多行中,它实际上并不包含换行符。如果您 明确包含换行符,那么您的行开头会有太多的空格,而您最终会得到<pre>块。

以下内容,使用三引号保留换行符有效:

>>> import markdown
>>> content = '''\
... Footnotes[^1] have a label[^@#$%] and the footnote's content.
... 
... [^1]: This is a footnote content.
... [^@#$%]: A footnote on the label: @#$%.
... '''
>>> print markdown.markdown( content, extensions=['footnotes'] )
<p>Footnotes<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup> have a label<sup id="fnref:@#$%"><a class="footnote-ref" href="#fn:@#$%" rel="footnote">2</a></sup> and the footnote's content.</p>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<p>This is a footnote content.&#160;<a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
<li id="fn:@#$%">
<p>A footnote on the label: @#$%.&#160;<a class="footnote-backref" href="#fnref:@#$%" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
</ol>
</div>