commonmark标记规范的python解析器
commonmark的Python项目详细描述
py是jgm的纯python端口 commonmark.js,一个 标记分析器和渲染器 CommonMark规范,仅使用本机 模块。一旦这个项目和commonmark规范 稳定我们将发布第一个1.0版本并尝试跟上 以commonmark.js中的更改为日期。
py使用python版本根据commonmark规范进行测试 2.7、3.4、3.5、3.6和3.7。
当前版本:0.9.0
安装
$ pip install commonmark
用法
>>> import commonmark >>> commonmark.commonmark('*hello!*') '<p><em>hello!</em></p>\n'
或者,如果没有语法糖:
importcommonmarkparser=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 treejson=commonmark.dumpJSON(ast)commonmark.dumpAST(ast)# pretty print generated AST structure
还有一个cli:
$ 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
贡献
如果您想通过 拉请求请做!如果您发现 当前测试套件未捕获的分析器/呈现程序请打开 新的一期,我也建议你把 commonmark.js项目 请求将测试添加到现有测试套件中。
测试
要使用commonmark.py,您需要能够运行测试套件 确保您的更改不会破坏任何内容。要运行测试,您可以 像这样:
$ pyvenv venv $ ./venv/bin/python setup.py develop test
测试脚本commonmark/tests/run_spec_tests.py实际上是一个devtool。作为 除了运行spec.txt中嵌入的所有测试之外,它还允许您 要使用-t参数运行特定测试,请提供信息 关于使用-p通过的测试,按测试类别通过的百分比 使用-s,并使用-i交互输入markdown(在 交互模式通过输入一行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