bbcovert/t_parser.py

40 lines
1.2 KiB
Python
Raw Normal View History

2024-06-22 14:50:40 +08:00
import bbcode
# init parser
parser = bbcode.Parser()
parser.add_simple_formatter('hr', '<hr />', standalone=True)
parser.add_simple_formatter('hl', '<mark>%(value)s</mark>')
parser.add_simple_formatter('sub', '<sub>%(value)s</sub>')
parser.add_simple_formatter('sup', '<sup>%(value)s</sup>')
parser.add_simple_formatter(
'iframe', '<iframe src="%(value)s">IFrame tag</iframe>')
parser.add_simple_formatter(
'showhide', '--- 隐藏内容 ---<br />%(value)s<br /> ----------------')
def render_color(tag_name, value, options, parent, context):
colors = "#0099ff"
if 'color' in options:
colors = options['color']
return f'<span style="color:{colors};">{value}</span>'
def render_bgcolor(tag_name, value, options, parent, context):
colors = "#0099ff"
if 'bgcolor' in options:
colors = options['bgcolor']
return f'<span style="background-color:{colors};">{value}</span>'
def render_size(tag_name, value, options, parent, context):
if 'size' in options:
sizes = options['size']
else:
return value
return f'<span style="font-size:{sizes};">{value}</span>'
parser.add_formatter("color", render_color)
parser.add_formatter("bgcolor", render_bgcolor)
parser.add_formatter("size", render_size)