Python的轻量级标记语言(Lightweight markup language for Python)

编写一个Python Web应用程序,我想创建一个文本区域,用户可以用轻量级标记语言输入文本。 文本将被导入到html模板并在页面上查看。 今天我使用这个命令来创建textarea,它允许用户输入任何(html)文本:

my_text = cgidata.getvalue('my_text', 'default_text') ftable.AddRow([Label(_('Enter your text')), TextArea('my_text', my_text, rows=8, cols=60).Format()])

我怎样才能改变这一点,以便只允许一些(安全,最终轻量级)标记? 包括消毒剂在内的所有建议都是受欢迎的,只要它可以轻松地与Python集成即可。

Programming a Python web application, I want to create a text area where the users can enter text in a lightweight markup language. The text will be imported to a html template and viewed on the page. Today I use this command to create the textarea, which allows users to enter any (html) text:

my_text = cgidata.getvalue('my_text', 'default_text') ftable.AddRow([Label(_('Enter your text')), TextArea('my_text', my_text, rows=8, cols=60).Format()])

How can I change this so that only some (safe, eventually lightweight) markup is allowed? All suggestions including sanitizers are welcome, as long as it easily integrates with Python.

最满意答案

使用python markdown实现

import markdown mode = "remove" # or "replace" or "escape" md = markdown.Markdown(safe_mode=mode) html = md.convert(text)

它非常灵活,你可以使用各种扩展,创建自己的等等。

Use the python markdown implementation

import markdown mode = "remove" # or "replace" or "escape" md = markdown.Markdown(safe_mode=mode) html = md.convert(text)

It is very flexible, you can use various extensions, create your own etc.

更多推荐