跳转到主要内容

CommonMark Markdown规范的Python解析器

项目描述

commonmark.py是jgmcommonmark.js的纯Python端口,它是一个用于CommonMark规范的Markdown解析器和渲染器,仅使用原生模块。一旦本项目和CommonMark规范都稳定,我们将发布第一个1.0版本,并尝试与commonmark.js的更改保持同步。

commonmark.py已在Python 2.7、3.4、3.5、3.6和3.7版本下针对CommonMark规范进行了测试。

当前版本 0.9.1

Pypi Link Build Status Documentation Status

安装

$ pip install commonmark

使用

>>> import commonmark
>>> commonmark.commonmark('*hello!*')
'<p><em>hello!</em></p>\n'

或者,不使用语法糖

import commonmark
parser = commonmark.Parser()
ast = parser.parse("Hello *World*")

renderer = commonmark.HtmlRenderer()
html = renderer.render(ast)
print(html) # <p>Hello <em>World</em><p/>

# inspecting the abstract syntax tree
json = commonmark.dumpJSON(ast)
commonmark.dumpAST(ast) # pretty print generated AST structure

还有一个命令行界面

$ cmark README.md -o README.html
$ cmark README.md -o README.json -aj # output AST as JSON
$ cmark README.md -a # pretty print generated AST structure
$ cmark -h
usage: cmark [-h] [-o [O]] [-a] [-aj] [infile]

Process Markdown according to the CommonMark specification.

positional arguments:
  infile      Input Markdown file to parse, defaults to stdin

optional arguments:
  -h, --help  show this help message and exit
  -o [O]      Output HTML/JSON file, defaults to stdout
  -a          Print formatted AST
  -aj         Output JSON AST

贡献

如果您想通过pull请求提供建议/优化/错误修复,请这样做!另外,如果您发现解析器/渲染器中存在当前测试套件未捕获的错误,请创建一个新问题,我还建议您向commonmark.js项目发送pull请求,将您的测试添加到现有测试套件中。

测试

要处理commonmark.py,您需要能够运行测试套件以确保您的更改不会破坏任何东西。要运行测试,您可以执行以下操作

$ pyvenv venv
$ ./venv/bin/python setup.py develop test

测试脚本commonmark/tests/run_spec_tests.py基本上是一个开发工具。除了运行spec.txt中嵌入的所有测试外,它还允许您使用-t参数运行特定测试,使用-p提供通过测试的信息,使用-s按测试类别提供通过百分比,并使用-i(在交互模式下,通过输入只有end的一行来结束一个块,通过输入相同的quit来退出)。-d可用于打印调用跟踪。

$ ./venv/bin/python commonmark/tests/run_spec_tests.py -h
usage: run_spec_tests.py [-h] [-t T] [-p] [-f] [-i] [-d] [-np] [-s]

script to run the CommonMark specification tests against the commonmark.py
parser.

optional arguments:
  -h, --help  show this help message and exit
  -t T        Single test to run or comma separated list of tests (-t 10 or -t 10,11,12,13)
  -p          Print passed test information
  -f          Print failed tests (during -np...)
  -i          Interactive Markdown input mode
  -d          Debug, trace calls
  -np         Only print section header, tick, or cross
  -s          Print percent of tests passed by category

作者

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

commonmark-0.9.1.tar.gz (95.8 kB 查看哈希

上传时间 源代码

构建分发

commonmark-0.9.1-py2.py3-none-any.whl (51.1 kB 查看哈希

上传时间 Python 2 Python 3

支持者