跳转到主要内容

纯Python bbcode解析器和格式化器。

项目描述

概述

最新包 http://pypi.python.org/pypi/bbcode

源代码 https://github.com/dcwatson/bbcode

文档 https://dcwatson.github.io/bbcode/

CI Status

安装

安装bbcode模块的最简单方法是使用pip,例如。

pip install bbcode

要求

Python,已测试2.7、3.5、3.6、3.7和3.8版本。还测试了PyPy(2和3)。

基本用法

# Using the default parser.
import bbcode
html = 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.
def render_color(tag_name, value, options, parent, context):
    return '<span style="color:%s;">%s</span>' % (tag_name, value)

# Installing advanced formatters.
for color in ('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字典传递给渲染函数。

  • 可以指定标签的起始和结束定界符(默认:[和])。这个库的附带好处是能够使用这个库通过使用<和>有选择地从字符串中删除HTML标签。

  • 包括可运行的unittest套件。

  • Python 3支持。

项目详情


下载文件

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

源代码分布

bbcode-1.1.0.tar.gz (11.1 kB 查看哈希值)

上传时间 源代码

构建分布

bbcode-1.1.0-py2.py3-none-any.whl (11.3 kB 查看哈希值)

上传时间 Python 2 Python 3