我没有从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>
脚注根本没有被解析!那是为什么?
答案 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. <a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p>
</li>
<li id="fn:@#$%">
<p>A footnote on the label: @#$%. <a class="footnote-backref" href="#fnref:@#$%" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p>
</li>
</ol>
</div>