纯python中最快的降价解析器

mistune的Python项目详细描述


纯python中最快的降价解析器,具有渲染特性, 灵感来自marked

Donate leptureWheel StatusConda VersionLatest VersionTravis CI StatusCoverage StatusApp Veyor CI Status

功能

  • pure python。在Python2.7、Python3.5+和Pypy中测试。
  • 非常快。它是所有pure python标记解析器中最快的。
  • 更多功能。表格、脚注、自动链接、围栏代码等。

查看benchmark results

安装

使用pip安装mistune:

$ pip install mistune

如果使用cython编译,则mistune可能更快:

$ pip install cython mistune

基本用法

呈现标记格式文本的简单api:

importmistunemistune.markdown('I am using **mistune markdown parser**')# output: <p>I am using <strong>mistune markdown parser</strong></p>

如果您关心性能,最好重新使用降价实例:

importmistunemarkdown=mistune.Markdown()markdown('I am using **mistune markdown parser**')

默认情况下,mistune已启用所有功能。您不必配置 什么都行。但是您可以选择更改解析器行为。

选项

下面是将影响渲染结果的所有选项的列表, 用mistune.Renderer

配置它们
renderer=mistune.Renderer(escape=True,hard_wrap=True)# use this renderer instancemarkdown=mistune.Markdown(renderer=renderer)markdown(text)
  • escape:如果设置为false,则不会对所有原始html标记进行转义。
  • 硬包装:如果设置为true,它将具有gfm换行功能。 所有新行将替换为<br>标记
  • 使用xhtml:如果设置为true,则所有标记都将使用xhtml,例如:<hr />
  • parse_block_html:仅在块级html中分析文本。
  • parse_inline_html:仅在内联级html中分析文本。

使用默认渲染器时,可以使用以下快捷方式之一:

mistune.markdown(text, escape=True, hard_wrap=True)

markdown = mistune.Markdown(escape=True, hard_wrap=True)
markdown(text)

渲染器

与Misaka/Sundown类似,您可以通过自定义渲染器影响渲染。 您所需要做的就是子类化一个渲染器类。

下面是代码突出显示的示例:

importmistunefrompygmentsimporthighlightfrompygments.lexersimportget_lexer_by_namefrompygments.formattersimporthtmlclassHighlightRenderer(mistune.Renderer):defblock_code(self,code,lang):ifnotlang:return'\n<pre><code>%s</code></pre>\n'% \
                mistune.escape(code)lexer=get_lexer_by_name(lang,stripall=True)formatter=html.HtmlFormatter()returnhighlight(code,lexer,formatter)renderer=HighlightRenderer()markdown=mistune.Markdown(renderer=renderer)print(markdown('```python\nassert 1 == 1\n```'))

mistune-contrib中查找更多渲染器。

块级

下面是块级渲染器API的列表:

block_code(code, language=None)
block_quote(text)
block_html(html)
header(text, level, raw=None)
hrule()
list(body, ordered=True)
list_item(text)
paragraph(text)
table(header, body)
table_row(content)
table_cell(content, **flags)

flags告诉您它是否是带有flags['header']的头。而且它 还告诉您与flags['align']对齐。

跨度水平

下面是一个跨度级渲染器api的列表:

autolink(link, is_email=False)
codespan(text)
double_emphasis(text)
emphasis(text)
image(src, title, alt_text)
linebreak()
newline()
link(link, title, content)
strikethrough(text)
text(text)
inline_html(text)

脚注

以下是与脚注相关的渲染器列表:

footnote_ref(key, index)
footnote_item(key, text)
footnotes(text)

雷克萨斯

有时您希望添加自己的规则来标记,例如github wiki 链接。使用渲染器无法实现此目标。你需要处理 对于雷克萨斯来说,这将是第一次有点困难。

我们将以github wiki链接为例:[[Page 2|Page 2]]。 它是内联语法,需要自定义的InlineGrammarInlineLexer

importcopy,refrommistuneimportRenderer,InlineGrammar,InlineLexerclassWikiLinkRenderer(Renderer):defwiki_link(self,alt,link):return'<a href="%s">%s</a>'%(link,alt)classWikiLinkInlineLexer(InlineLexer):defenable_wiki_link(self):# add wiki_link rulesself.rules.wiki_link=re.compile(r'\[\['# [[r'([\s\S]+?\|[\s\S]+?)'# Page 2|Page 2r'\]\](?!\])'# ]])# Add wiki_link parser to default rules# you can insert it some place you like# but place matters, maybe 3 is not goodself.default_rules.insert(3,'wiki_link')defoutput_wiki_link(self,m):text=m.group(1)alt,link=text.split('|')# you can create an custom render# you can also return the html if you likereturnself.renderer.wiki_link(alt,link)

您应该将内联lexer传递给Markdown解析器:

renderer=WikiLinkRenderer()inline=WikiLinkInlineLexer(renderer)# enable the featureinline.enable_wiki_link()markdown=Markdown(renderer,inline=inline)markdown('[[Link Text|Wiki Link]]')

与块级lexer相同。需要一段时间才能理解 整个机制。但你不会玩太多的把戏。

贡献和扩展

mistune本身不接受任何扩展。永远都是简单的 文件脚本。

如果要添加功能,可以转到mistune-contrib

这里有一些扩展已经在mistune-contrib

  • math/mathjax特性
  • 突出显示代码呈现器
  • 目录功能表
  • 多标记元数据分析器

从contrib存储库中获得灵感。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用Ibatis在插入时返回ID(使用返回关键字)   java(org.hibernate.TransactionException)org。冬眠TransactionException:事务未成功启动   java小程序jwindow始终位于JNLP顶部   在Java中重新解析JSON对象?   java单击后将ListView数据移动到新屏幕   Mule ESB中的java WSA寻址特性   Java,对象编程:获取返回0值的方法   hibernate的Java通用问题,如何处理T get(K id)   java在使用超级CSV读取CSV时忽略引用   ssh使用Java远程运行命令   java如何向具体用户发送websocket消息?   在JAVA中,我可以在不指定的情况下使用条件运算符吗?