Python Markdown-不添加Codehilite

  我正在使用Flask编写博客,并使用Markdown的Python库为我生成HTML,我愿意突出显示语法,因此我正在使用 markdown.markdown(string, extensions=['codehilite']


根据他们的wiki,它应该添加一个html类。


<div class="codehilite"><pre><code># Code goes here ...</code></pre></div>

但是按照我的解释器的尝试,它似乎没有用。


In [9]: markdown.version

Out[9]: '2.3.1'


In [10]: text = """:::python

   ....: import os

   ....: print "This is a text!"

   ....: """


In [11]: html = markdown.markdown(text, extensions=['codehilite'])


In [12]: html

Out[12]: u'<p>:::python\nimport os\nprint "This is a text!"</p>'


In [13]: # Even more funnier, when following the examples in the usage section "..['codehilite(linenums=True)']


In [14]: html = markdown.markdown(text, extensions=['codehilite(linenums=True)'])


In [15]: html

Out[15]: u'<p>:::python\nimport os\nprint "This is a text!"</p>'


In [16]: # No line numbers, or any class..

我不确定这是什么问题,我安装了Pygments,已经升级了Markdown的lib,但是什么也没有。这里的预期结果是Markdown将添加html类codehilite,因此我将能够使语法正常工作。这里似乎是什么问题?


慕神8447489
浏览 327回答 3
3回答

扬帆大鱼

我已经确定,codehilite除了具有一般性格外,还可以在紧接其前的一个列表时中断:这种降价及其变体是行不通的:* apples* oranges&nbsp; &nbsp; #!python&nbsp; &nbsp; import os但是,如果我在列表和代码之间添加了一些内容,那么它确实可以工作:* apples* orangesPut something between the code and the list&nbsp; &nbsp; #!python&nbsp; &nbsp; import os但这通常是不可预测的。我尝试了成千上万种组合,并成功地复制了文档中的内容,却取得了非常不同的结果。不开心...使用fenced_code替代然后,我进入了pygments的其他子扩展,并尝试显式添加fenced_code扩展并重试一下fenced代码示例。效果更好。所以继续pygmented_body = markdown.markdown(rendered_body,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;extensions=['codehilite', 'fenced_code'])我通过fenced code单独使用获得了更大的成功:* Don't need to indent 4 spaces* Don't need something between the list and the code~~~~{.python hl_lines='3'}import osprint('hello, world')~~~~And final comments here.

森栏

以下是一些示例(遵循我的意愿..)In [1]: import markdown2In [2]: markdown2.markdown("> This is a paragraph and I am **bold**")Out[2]: u'<blockquote>\n&nbsp; <p>This is a paragraph and I am <strong>bold</strong></p>\n</blockquote>\n'In [3]: code = """```pythonif True:&nbsp; &nbsp; print "hi"```"""&nbsp; &nbsp;...:&nbsp;In [4]: markdown2.markdown(code, extras=['fenced-code-blocks'])Out[4]: u'<div class="codehilite"><pre><code><span class="k">if</span> <span class="bp">True</span><span class="p">:</span>\n&nbsp; &nbsp; <span class="k">print</span> <span class="s">&quot;hi&quot;</span>\n</code></pre></div>\n'

喵喵时光机

对不起,刚才看到您的问题。在python-markdown中,每行代码需要4个空格。In [13]: text = """&nbsp; &nbsp;....:&nbsp; &nbsp; &nbsp; &nbsp; :::python&nbsp; &nbsp;....:&nbsp; &nbsp; &nbsp; &nbsp; import os&nbsp; &nbsp;....:&nbsp; &nbsp; &nbsp; &nbsp; """In [14]: markdown.markdown(text, extensions = ['codehilite'])Out[14]: u'<div class="codehilite"><pre><span class="kn">import</span>&nbsp;<span class="nn">os</span>\n</pre></div>'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python