An attempt to catch BBCode references in code and escape them (by underlining the key word)
[pre=#000000]NUMBERED_BBCODE_LINE = ‘%(color)s]%(line)s /color]%(code)s\n’
BBCODE_LINE = ‘%(code)s\n’
BBCODE_CODE = ‘%(color)s]%(content)s/color]’
BBCODE_ESCAPE = ‘/color]%(color_open)s]%(content)s/color]%(color_close)s]’
BBCODE_BOLD = ‘b]%(content)s/b]’
BBCODE_ITALIC = ‘i]%(content)s/i]’
POST_START = ‘[pre=[/color]%(bg_color)s]’
POST_END = ‘/pre]\n’
BBCODE_MATCH =* r*"""(/?)((?:code|pre|table|tr|td|th|b|i|u|sup|color|url|img|list|trac|center|quote|size|li|ul|ol|youtube|gvideo)(?:=^]]+)?)(])"""[/pre]
Seems to work
[size=85][pre=#000000][/size]pre=#000000]color=#F8F8F2]NUMBERED_BBCODE_LINE /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]color=]color=#AE81FF]%(color)s/color]color=#E6DB74]]/color]color=#AE81FF]%(line)s/color]color=#E6DB74] //color]color=#E6DB74]color/color]color=#E6DB74]]/color]color=#AE81FF]%(code)s/color]color=#AE81FF]\n/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_LINE /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#AE81FF]%(code)s/color]color=#AE81FF]\n/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_CODE /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]color=]color=#AE81FF]%(color)s/color]color=#E6DB74]]/color]color=#AE81FF]%(content)s/color]color=#E6DB74]//color]color=#E6DB74]color/color]color=#E6DB74]]/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_ESCAPE /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]//color]color=#E6DB74]color/color]color=#E6DB74]]color=]color=#AE81FF]%(color_open)s/color]color=#E6DB74]]/color]color=#AE81FF]%(content)s/color]color=#E6DB74]//color]color=#E6DB74]color/color]color=#E6DB74]]color=]color=#AE81FF]%(color_close)s/color]color=#E6DB74]]/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_BOLD /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]/color]color=#E6DB74]b/color]color=#E6DB74]]/color]color=#AE81FF]%(content)s/color]color=#E6DB74]//color]color=#E6DB74]b/color]color=#E6DB74]]/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_ITALIC /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]/color]color=#E6DB74]i/color]color=#E6DB74]]/color]color=#AE81FF]%(content)s/color]color=#E6DB74]//color]color=#E6DB74]i/color]color=#E6DB74]]/color]color=#E6DB74]’/color]
color=#F8F8F2]POST_START /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]pre=]color=#AE81FF]%(bg_color)s/color]color=#E6DB74]]/color]color=#E6DB74]’/color]
color=#F8F8F2]POST_END /color]color=#F92672]=/color]color=#E6DB74] ‘/color]color=#E6DB74]//color]color=#E6DB74]pre/color]color=#E6DB74]]/color]color=#AE81FF]\n/color]color=#E6DB74]’/color]
color=#F8F8F2]BBCODE_MATCH /color]color=#F92672]=/color]i]color=#66D9EF] r/color]/i]color=#E6DB74]"""/color]color=#E6DB74](/color]color=#AE81FF]/color]color=#E6DB74]//color]color=#F92672]?/color]color=#E6DB74])(/color]color=#E6DB74](/color]color=#E6DB74]?:/color]color=#E6DB74]code/color]color=#F92672]|/color]color=#E6DB74]pre/color]color=#F92672]|/color]color=#E6DB74]table/color]color=#F92672]|/color]color=#E6DB74]tr/color]color=#F92672]|/color]color=#E6DB74]td/color]color=#F92672]|/color]color=#E6DB74]th/color]color=#F92672]|/color]color=#E6DB74]b/color]color=#F92672]|/color]color=#E6DB74]i/color]color=#F92672]|/color]color=#E6DB74]u/color]color=#F92672]|/color]color=#E6DB74]sup/color]color=#F92672]|/color]color=#E6DB74]color/color]color=#F92672]|/color]color=#E6DB74]url/color]color=#F92672]|/color]color=#E6DB74]img/color]color=#F92672]|/color]color=#E6DB74]list/color]color=#F92672]|/color]color=#E6DB74]trac/color]color=#F92672]|/color]color=#E6DB74]center/color]color=#F92672]|/color]color=#E6DB74]quote/color]color=#F92672]|/color]color=#E6DB74]size/color]color=#F92672]|/color]color=#E6DB74]li/color]color=#F92672]|/color]color=#E6DB74]ul/color]color=#F92672]|/color]color=#E6DB74]ol/color]color=#F92672]|/color]color=#E6DB74]youtube/color]color=#F92672]|/color]color=#E6DB74]gvideo/color]color=#E6DB74])(/color]color=#E6DB74]?:/color]color=#E6DB74]=/color]color=#AE81FF]/color]color=#F92672]^/color]color=#AE81FF]]/color]color=#AE81FF]]/color]color=#F92672]+/color]color=#E6DB74])/color]color=#F92672]?/color]color=#E6DB74])(/color]color=#AE81FF]]/color]color=#E6DB74])/color]color=#E6DB74]"""/color]
/pre][/pre]
Edit:
Even better escaping. No underlines needed, and more reliable.