Skip to main content
 首页 » 编程设计

带脚注的 Python Markdown

2025年05月04日19yjmyzz

我没有从 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> 

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

请您参考如下方法:

您的行中没有换行符。 \ at the end of the lines 仅仅允许你把你的字符串放在多行中,它实际上并不包含换行符。如果您要显式地包含换行符,那么您的行的开头会有太多空格,并且您最终会得到一个 <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>