2

Python Markdown 做语法限制

 2 years ago
source link: https://www.isaced.com/post-249.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python Markdown 做语法限制

这几天埋头写NSLoger,需要给Markdown禁用一些语法,比如禁止评论内容中出现H1~H6标签之类的,这个问题困扰了我几天,今天终于找到了解决方案。Python-Markdown的文档看起来真伤神…

之前我用的Markdown2,不为什么,随便挑的,前两天我在SegmentFault上提了一个问题:Python中的Markdown和Markdown2有何区别?,据说Misaka很快,因为是CPython,试了下,没太大感觉,我要的是简单,性能暂时还没考虑在之中。

通过markdown的Extension来del掉指定的语法就行了,如下:

import markdown

class MyMarkdownExtension(markdown.Extension):

	def extendMarkdown(self, md, md_globals):
		del md.parser.blockprocessors['hashheader']

my_markdown_extension = MyMarkdownExtension()

raw_text = '#this H1 ~~~'

print markdown.markdown(raw_text, [my_markdown_extension], safe_mode = 'escape')

输出<p>#this H1 ~~~</p>,要的就是这个效果,当然还有其他的可以删除。

下面是所有md.inlinePatterns包含的key: > ‘backtick’ ‘escape’ ‘reference’ ‘link’ ‘image_link’ ‘image_reference’ ‘short_reference’ ‘autolink’ ‘automail’ ‘linebreak2’ ‘linebreak’ ‘html’ ‘entity’ ‘not_strong’ ‘strong_em’ ‘strong’ ‘emphasis’ ‘emphasis2’

如果你要的没在上面,那么看看md.parser.blockprocessors: >‘indent’ ‘code’ ‘hashheader’ ‘setextheader’ ‘hr’ ‘olist’ ‘ulist’ ‘quote’ ‘paragraph’

参考:http://blog.magicalhobo.com/2011/05/05/disabling-images-in-python-markdown/


是不是记记自己遇到过的坑,也是极好滴~~~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK