纯python bbcode解析器和格式化程序。
bbcode的Python项目详细描述
概述
最新包http://pypi.python.org/pypi/bbcode
源代码https://github.com/dcwatson/bbcode
文档http://bbcode.readthedocs.org/
安装
安装bbcode模块的最简单方法是使用pip,例如:
pip install bbcode
要求
python,在2.6到3.7版本中进行了测试。也在Pypy(2和3)上进行了测试。
基本用法
# Using the default parser.importbbcodehtml=bbcode.render_html(text)# Installing simple formatters.parser=bbcode.Parser()parser.add_simple_formatter('hr','<hr />',standalone=True)parser.add_simple_formatter('sub','<sub>%(value)s</sub>')parser.add_simple_formatter('sup','<sup>%(value)s</sup>')# A custom render function.defrender_color(tag_name,value,options,parent,context):return'<span style="color:%s;">%s</span>'%(tag_name,value)# Installing advanced formatters.forcolorin('red','blue','green','yellow','black','white'):parser.add_formatter(color,render_color)# Calling format with context.html=parser.format(text,somevar='somevalue')
优于Postmarkup
有关如何/何时转义的更多标记选项-例如,可以指定 是转义HTML还是逐标记执行外观替换 基础。用于自动链接和转换换行符。
使用john gruber的 URL正则表达式: http://daringfireball.net/2010/07/improved_regex_for_matching_urls
不接受无法识别的标记。例如,[3]将输出为 [3],不被忽视。
更灵活的标签选项解析器。标签可能有标准的bbcode选项, 例如[url=something]text[/url],但也可能有命名选项, 例如[url=something alt=icon]text[/url]。这些选项已通过 作为标准python字典的render函数。
能够指定标记的开始和结束分隔符(默认值:[and])。 这样做的另一个好处是可以使用这个库有选择地 使用<;和>;从字符串中删除HTML标记。
包括一个可运行的unittest套件。
支持Python3。