RTF Output: Rendering tweaks.

This commit is contained in:
John Schember 2009-05-27 21:24:45 -04:00
parent 814763fc59
commit 9f5e16cc12

View File

@ -37,16 +37,19 @@ TAGS = {
} }
SINGLE_TAGS = { SINGLE_TAGS = {
'br': '{\\line }', 'br': '\n{\\line }\n',
'div': '{\\line }', 'div': '\n{\\line }\n',
}
SINGLE_TAGS_END = {
'div': '\n{\\line }\n',
} }
STYLES = [ STYLES = [
('display', {'block': '\\par \\pard \\hyphpar \\keep'}), ('display', {'block': '\\par \\pard \\hyphpar \\keep'}),
('font-weight', {'bold': '\\b', 'bolder': '\\b'}), ('font-weight', {'bold': '\\b', 'bolder': '\\b'}),
('font-style', {'italic': '\\i'}), ('font-style', {'italic': '\\i'}),
# ('page-break-before', {'always': '\\pagebb '}), ('text-align', {'center': '\\qc', 'left': '\\ql', 'right': '\\qr'}),
('text-align', {'center': '\\qc', 'left': '\\ql', 'right': '\\qr', 'justify': '\\qj'}),
('text-decoration', {'line-through': '\\strike', 'underline': '\\ul'}), ('text-decoration', {'line-through': '\\strike', 'underline': '\\ul'}),
] ]
@ -141,8 +144,11 @@ class RTFMLizer(object):
# Remove excessive spaces # Remove excessive spaces
text = re.sub('[ ]{2,}', ' ', text) text = re.sub('[ ]{2,}', ' ', text)
text = re.sub(r'(\{\\line \}){3,}', r'{\\line }{\\line }', text) text = re.sub(r'(\{\\line \}\s*){3,}', r'{\\line }{\\line }', text)
text = re.sub(r'(\{\\line \})+\{\\par', r'{\\par', text) text = re.sub(r'(\{\\line \}\s*)+\{\\par', r'{\\par', text)
# Remove non-breaking spaces
text = text.replace(u'\xa0', ' ')
return text return text
@ -208,6 +214,10 @@ class RTFMLizer(object):
if end_tag != 'block': if end_tag != 'block':
text += u'}' text += u'}'
single_tag_end = SINGLE_TAGS_END.get(tag, None)
if single_tag_end:
text += single_tag_end
if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '': if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '':
if 'block' in tag_stack: if 'block' in tag_stack:
text += '%s ' % elem.tail text += '%s ' % elem.tail