Python Markdown nl2br扩展 [英] Python Markdown nl2br extension

查看:44
本文介绍了Python Markdown nl2br扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我一直在尝试在我的Web应用程序中使用python markdown库.一切正常,除了nl2br扩展名.

I'm a beginner programmer, and i've been trying to use the python markdown library in my web app. everything works fine, except the nl2br extension.

当我尝试使用md.convert(text)将文本文件转换为html时,看不到将换行符转换为< br> .

when i try to convert text file to html using md.convert(text), it doesn't see to convert newlines to <br>.

例如,在我转换之前,文本为:

for example, before i convert, the text is:

Puerto Rico

===========

------------------------------

### Game Rules





hello world!

转换后,我得到:

 <h1>Puerto Rico</h1>
<hr />
<h3>Game Rules</h3>
<p>hello world!</p>

我的理解是空格由'\ n'表示,应将其转换为< br> ,但我没有得到该结果.这是我的代码:

My understanding is that the blank spaces are represented by '\n' and should be converted to <br>, but i'm not getting that result. Here's my code:

import markdown
md = markdown.Markdown(safe_mode='escape',extensions=['nl2br']) 
html = md.convert(text)

如果您有任何想法或可以向我指出正确的方向,请告诉我.谢谢.

Please let me know if you have any idea or can point me in the right direction. Thank you.

推荐答案

尝试在行尾添加两个或多个空格以插入< br/> 标签

Try adding two or more white spaces at the end of a line to insert <br/> tags

示例:

hello  
world

产生

<p>hello <br>
world</p>

请注意,单词"hello"后面有两个空格.仅当行尾两个空格之前有一些文本时,此方法才有效.但这与您的nl2br扩展名无关,这是降价标准.

Notice that there are two spaces after the word hello. This only works if you have some text before the two spaces at the end of a line. But this has nothing to do with your nl2br extension, this is markdown standard.

我的建议是,如果您不必明确地进行此转换,就不要这样做.使用段落别名< p> -tags是分隔文本区域的更干净的方法.

My advice is, if you don't explicitly have to do this conversion, just don't do it. Using paragraphs alias <p>-tags is the cleaner way to seperate text regions.

如果您只是想在< h3> 标题之后留出更多空间,请为其定义一些CSS:

If you simply want to have more space after your <h3> headlines then define some css for it:

h3 { margin-bottom: 4em; }

图像,如果您在所有500个Wiki页面的标题后都使用&br; br> -标记进行间隔,然后又决定它的20px太多的空间.然后,您必须手动编辑所有页面,并在每个页面上删除两个< br> 标签.否则,您只需在css文件中编辑一行即可.

Image if you do spacing with <br>-tags after your headlines in all your 500 wiki pages and later you decide that it's 20px too much space. Then you have to edit all your pages by hand and remove two <br>-tags on every page. Otherwise you just edit one line in a css file.

这篇关于Python Markdown nl2br扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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