markdown没有正确转换为html

时间:2018-12-27 16:50:14

标签: python markdown

我在数据库(sqlite)的某个表列中存储了一些markdown文本,并试图将其转换为markdown;这是我运行这两个命令(.mode lineSELECT content FROM post LIMIT 1之后获得的一个表格单元格全部内容之一的示例。

content = **Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Etiam a arcu eget tortor venenatis laoreet. In hac habitasse platea dictumst. Nunc nec ipsum eu massa pharetra scelerisque. Maecenas accumsan ex nisl, ac condimentum lorem porttitor a. Nullam posuere ligula ac mauris cursus mattis.

Proin ut hendrerit lorem. Donec imperdiet risus in dui vulputate, vitae volutpat arcu ultricies. Quisque et pellentesque lectus, eutempus nisi. Sed vel felis nibh. Duis eu lorem non odio blandit ornare. Mauris facilisis, urna nec condimentum sodales, nulla nisi aliquam ante, eget pulvinar nibh sem ut ipsum.

Pellentesque vel justo ut felis eleifend tincidunt. Pellentesque vel porttitor nisl. Fusce eu ligula id arcu volutpat elementum quis quis arcu.

```python
@main.route('/<slug>')
def show_post(slug):
    entities = (Post.title, Post.created_at, Post.cover_image,
                Post.content)
    post = Post.query.with_entities(*entities) \
        filter(Post.published == 1). \
        filter(Post.slug == slug).first()
    if not post:
        abort(404)
```

当我获取该内容并尝试以下代码时,将其通过python shell进行尝试

from markdown import markdown
s = '''**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Etiam a arcu eget tortor venenatis laoreet. In hac habitasse platea dictumst. Nunc nec ipsum eu massa pharetra scelerisque. Maecenas accumsan ex nisl, ac condimentum lorem porttitor a. Nullam posuere ligula ac mauris cursus mattis.

Proin ut hendrerit lorem. Donec imperdiet risus in dui vulputate, vitae volutpat arcu ultricies. Quisque et pellentesque lectus, eutempus nisi. Sed vel felis nibh. Duis eu lorem non odio blandit ornare. Mauris facilisis, urna nec condimentum sodales, nulla nisi aliquam ante, eget pulvinar nibh sem ut ipsum.

Pellentesque vel justo ut felis eleifend tincidunt. Pellentesque vel porttitor nisl. Fusce eu ligula id arcu volutpat elementum quis quis arcu.

```python
@main.route('/<slug>')
def show_post(slug):
    entities = (Post.title, Post.created_at, Post.cover_image,
                Post.content)
    post = Post.query.with_entities(*entities) \
        filter(Post.published == 1). \
        filter(Post.slug == slug).first()
    if not post:
        abort(404)
```
'''
markdown(s)

我得到以下输出

<p><strong>Lorem ipsum</strong> dolor sit amet, consectetur adipiscing elit. Etiam a arcu eget tortor venenatis laoreet. In hac hab
itasse platea dictumst. Nunc nec ipsum eu massa pharetra scelerisque. Maecenas accumsan ex nisl, ac condimentum lorem porttitor a. N
ullam posuere ligula ac mauris cursus mattis.</p>\n<p>Proin ut hendrerit lorem. Donec imperdiet risus in dui vulputate, vitae volutp
at arcu ultricies. Quisque et pellentesque lectus, eutempus nisi. Sed vel felis nibh. Duis eu lorem non odio blandit ornare. Mauris
facilisis, urna nec condimentum sodales, nulla nisi aliquam ante, eget pulvinar nibh sem ut ipsum.</p>\n<p>Pellentesque vel justo ut
 felis eleifend tincidunt. Pellentesque vel porttitor nisl. Fusce eu ligula id arcu volutpat elementum quis quis arcu.</p>\n<p><code
>python\n@main.route('/&lt;slug&gt;')\ndef show_post(slug):\n    entities = (Post.title, Post.created_at, Post.cover_image,\n
         Post.content)\n    post = Post.query.with_entities(*entities)         filter(Post.published == 1).         filter(Post.slug
 == slug).first()\n    if not post:\n        abort(404)</code></p>

一切似乎都很好,直到最后一部分,我希望这样 <pre><code class="lang-python">...</code></pre>here所述,其中...代表标签之间的python代码,但却得到了<p><code>...</code></p>

是什么原因造成的,是markdown库还是其他?

1 个答案:

答案 0 :(得分:2)

受防护的代码块不是standard Markdown。因此,您需要enable extension才能正确解析它们:

filter

哪个呈现以下输出:

import pandas as pd
import numpy as np


#I create and set the column equal to 0 here so that it starts out automatically below my desired ratio threshold

ssdf['SGRS'] = 0

def min_throw_solver():
    #here i set the x value at 0 to start with
    x = 0

    while ssdf['SGRS'].min()<.3:

        #here is where I try to add 1 to the x value each time the calculation loops and yields a ratio less then
        #desired. I think this is where I have gone wrong.
        x+=1

        ssdf['SGRS'] = (((ssdf['cell_thickness']* ssdf['VShale']).rolling(fault_throw_cell_thickness).sum())/x)
        print ('x is currently',x)

        else:
            break