python-markdown无法识别代码块? [英] python-markdown doesn't recognize code block?

查看:470
本文介绍了python-markdown无法识别代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究将Markdown转换为HTML的脚本,我已经尝试了markdownmarkdown2. 当我使用MathJax使其能够在LaTex中显示数学公式时,我发现markdown对我来说比markdown2好. 但是,它们都不能识别```中的代码块. 我的代码是用Python编写的.

I'm working on a script to transfer Markdown to HTML, I've tried both markdown and markdown2. As I use MathJax to make it able to show math formulas in LaTex, I found markdown is better for me than markdown2. However, both of them don't recognize code blocks in ```. My code is written in Python.

我的Markdown代码是:

计算香农熵的函数:

```

from math import log

def calcShannonEnt(dataSet):
   numEntries = len(dataSet) #类别个数
   labelCount = {}
   for featVec in dataSet: #对每一条数据
       currentLabel = featVec[-1] #currentLabel为当前数据的类别
       if currentLabel not in labelCount.keys(): #计数
           labelCount[currentLabel] = 0
       labelCount[currentLabel] += 1
   shannonEnt = 0.0
   for key in labelCount.keys():
       prob = float(labelCount[key]) / float(numEntries)
       shannonEnt -= prob * float(log(prob,2))#计算香农熵
   return shannonEnt

```


使用要求:

- 调用的数据必须储存在列表中,且所有列表元素有相同长度
- 列表元素的最后一列为类别

[sorted函数及operator.itemgetter函数的用法详解](http://blog.csdn.net/alvine008/article/details/37757753

我希望那些汉字不会打扰您. HTML代码是:

I hope those Chinese characters don't bother you. The HTML code is :

<p>计算香农熵的函数:</p>
<pre><code>```

from math import log

def calcShannonEnt(dataSet):
   numEntries = len(dataSet) #类别个数
   labelCount = {}
   for featVec in dataSet: #对每一条数据
       currentLabel = featVec[-1] #currentLabel为当前数据的类别
       if currentLabel not in labelCount.keys(): #计数
           labelCount[currentLabel] = 0
       labelCount[currentLabel] += 1
   shannonEnt = 0.0
   for key in labelCount.keys():
       prob = float(labelCount[key]) / float(numEntries)
       shannonEnt -= prob * float(log(prob,2))#计算香农熵
   return shannonEnt

```


使用要求:

- 调用的数据必须储存在列表中,且所有列表元素有相同长度
- 列表元素的最后一列为类别

[sorted函数及operator.itemgetter函数的用法详解](http://blog.csdn.net/alvine008/article/details/37757753
</code></pre>

出什么问题了?

推荐答案

借助@Waylan,该问题已得到完美解决. 这是因为我没有启用扩展.请参见扩展

With the help of @Waylan the problem has been solved perfectly. It is because I didn't enable the extensions. See extensions

现在是正确的:

html_txt = markdown.markdown(post.body_markdown, extensions=['fenced_code'])

这篇关于python-markdown无法识别代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆