我用Flask写博客,用Markdown的Python库为我生成HTML,我愿意使用语法高亮显示,因此我使用markdown.markdown(string, extensions=['codehilite']
根据他们的维基,它应该添加一个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的库,但是什么也没有。这里的预期结果是Markdown将添加html类codehilite,这样我就能够使语法工作起来。这里似乎有什么问题?
发布于 2013-05-30 09:03:01
我找到了另一个解决方案,markdown2
这里有几个例子(以下是我所愿意的)
In [1]: import markdown2
In [2]: markdown2.markdown("> This is a paragraph and I am **bold**")
Out[2]: u'<blockquote>\n <p>This is a paragraph and I am <strong>bold</strong></p>\n</blockquote>\n'
In [3]: code = """```python
if True:
print "hi"
```"""...:
注释2.标记(代码,extras=‘fenced code-块’)
Out4: U‘’if\n打印“hi”\n‘
发布于 2015-09-29 01:15:48
我已经确定了,除了一般的性情之外,当眼前有一份清单时,可怜虫就会中断:
这个标记,以及它的变体,只是不起作用:
* apples
* oranges
#!python
import os但是,如果我在列表和代码之间添加了一些东西,那么它就会起作用:
* apples
* oranges
Put something between the code and the list
#!python
import os但这通常是不可预测的。我尝试了数以百万计的组合,成功地复制了文档中的内容。不高兴..。
使用fenced_code代替
然后,我走入其他的延伸,尝试显式地添加fenced_code扩展,并重新尝试有围栏的代码示例。效果更好。
就这样开始
pygmented_body = markdown.markdown(rendered_body,
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 os
print('hello, world')
~~~~
And final comments here.发布于 2014-01-13 07:27:39
很抱歉刚才看到你的问题。
在python标记中,每一行代码都需要4个空格。
In [13]: text = """
....: :::python
....: import os
....: """
In [14]: markdown.markdown(text, extensions = ['codehilite'])
Out[14]: u'<div class="codehilite"><pre><span class="kn">import</span>
<span class="nn">os</span>\n</pre></div>'https://stackoverflow.com/questions/16576882
复制相似问题