def decode(self, text, location=None):
self.doc = pyglet.text.document.FormattedDocument()
self.length = 0
self.attributes = {}
next_trailing_space = True
trailing_newline = True
for m in _pattern.finditer(text):
group = m.lastgroup
trailing_space = True
if group == 'text':
t = m.group('text')
self.append(t)
trailing_space = t.endswith(' ')
trailing_newline = False
elif group == 'nl_soft':
if not next_trailing_space:
self.append(' ')
trailing_newline = False
elif group in ('nl_hard1', 'nl_hard2'):
self.append('\n')
trailing_newline = True
elif group == 'nl_para':
self.append(m.group('nl_para')[1:]) # ignore the first \n
trailing_newline = True
elif group == 'attr':
try:
ast = parser.expr(m.group('attr_val'))
if self.safe(ast):
val = eval(ast.compile())
else:
val = None
except (parser.ParserError, SyntaxError):
val = None
name = m.group('attr_name')
if name[0] == '.':
if trailing_newline:
self.attributes[name[1:]] = val
else:
self.doc.set_paragraph_style(self.length, self.length,
{name[1:]: val})
else:
self.attributes[name] = val
elif group == 'escape_dec':
self.append(chr(int(m.group('escape_dec_val'))))
elif group == 'escape_hex':
self.append(chr(int(m.group('escape_hex_val'), 16)))
elif group == 'escape_lbrace':
self.append('{')
elif group == 'escape_rbrace':
self.append('}')
next_trailing_space = trailing_space
return self.doc
评论列表
文章目录