【问题标题】:Python Markdown - Not adding codehilitePython Markdown - 不添加 codehilite
【发布时间】:2013-05-10 17:21:46
【问题描述】:

我正在使用 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 的库,但没有。 此处的预期结果是 Markdown 将添加 html 类 codehilite,因此我将能够使语法正常工作。 这里似乎有什么问题?

【问题讨论】:

标签: python markdown pygments codehighlighter


【解决方案1】:

我已经确定,除了一般喜怒无常之外,当它之前有一个列表时,codehilite 就会崩溃:

这个降价,以及它的变体,是行不通的:

* apples
* oranges

    #!python
    import os

但如果我在列表和代码之间放置一些东西,那么它确实有效:

* apples
* oranges

Put something between the code and the list

    #!python
    import os

但这通常是不可预测的。我尝试了无数种组合,成功地复制了文档中的内容。不开心...

改用fenced_code

然后我进入the other sub-extensions of pygments 并尝试显式添加 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.

【讨论】:

    【解决方案2】:

    我找到了另一个解决方案,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"
    ```"""
       ...: 
    
    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    <span class="k">print</span> <span class="s">&quot;hi&quot;</span>\n</code></pre></div>\n'
    

    【讨论】:

      【解决方案3】:

      很抱歉刚才看到你的问题。

      在python-markdown中,每行代码需要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>'
      

      【讨论】:

        猜你喜欢
        • 2014-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        • 2021-07-29
        相关资源
        最近更新 更多