Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-markdown doesn't recognize code block?

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.

My Markdown code is:

计算香农熵的函数:

```

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

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>

What's the problem?

like image 935
jinglei Avatar asked Feb 10 '16 15:02

jinglei


People also ask

How do you put a code block in Markdown?

Code Blocks To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab. For example, given this input: This is a normal paragraph: This is a code block. A code block continues until it reaches a line that is not indented (or the end of the article).

How do I use Python code in Markdown?

To add a Python code chunk to an R Markdown document, you can use the chunk header ```{python} , e.g., ```{python} print("Hello Python!") ```

How do I add a code block in Git readme?

You can create fenced code blocks by placing triple backticks ``` before and after the code block. We recommend placing a blank line before and after code blocks to make the raw formatting easier to read.

What is fenced Codeblocks?

Fenced Code Blocks are defined using the syntax originally established in PHP Markdown Extra and popularized by GitHub Flavored Markdown. Fenced code blocks begin with three or more backticks ( ``` ) or tildes ( ~~~ ) on a line by themselves and end with a matching set of backticks or tildes on a line by themselves.


1 Answers

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

Now it is right:

html_txt = markdown.markdown(post.body_markdown, extensions=['fenced_code'])
like image 50
jinglei Avatar answered Sep 19 '22 06:09

jinglei