FB2 otput fixes.

This commit is contained in:
John Schember 2009-08-05 07:24:58 -04:00
parent 95f9cf113a
commit 911d0cbf92

View File

@ -23,9 +23,14 @@ TAG_MAP = {
'b' : 'strong', 'b' : 'strong',
'i' : 'emphasis', 'i' : 'emphasis',
'p' : 'p', 'p' : 'p',
'li' : 'p' 'li' : 'p',
} }
TAG_SPACE = [
'div',
'br',
]
STYLES = [ STYLES = [
('font-weight', {'bold' : 'strong', 'bolder' : 'strong'}), ('font-weight', {'bold' : 'strong', 'bolder' : 'strong'}),
('font-style', {'italic' : 'emphasis'}), ('font-style', {'italic' : 'emphasis'}),
@ -128,14 +133,12 @@ class FB2MLizer(object):
if tag == 'img': if tag == 'img':
fb2_text += '<image xlink:href="#%s" />' % os.path.basename(elem.attrib['src']) fb2_text += '<image xlink:href="#%s" />' % os.path.basename(elem.attrib['src'])
fb2_tag = TAG_MAP.get(tag, None) fb2_tag = TAG_MAP.get(tag, None)
if fb2_tag and fb2_tag not in tag_stack: if fb2_tag and fb2_tag not in tag_stack:
tag_count += 1 tag_count += 1
fb2_text += '<%s>' % fb2_tag fb2_text += '<%s>' % fb2_tag
tag_stack.append(fb2_tag) tag_stack.append(fb2_tag)
# Processes style information # Processes style information
for s in STYLES: for s in STYLES:
style_tag = s[1].get(style[s[0]], None) style_tag = s[1].get(style[s[0]], None)
@ -144,7 +147,11 @@ class FB2MLizer(object):
fb2_text += '<%s>' % style_tag fb2_text += '<%s>' % style_tag
tag_stack.append(style_tag) tag_stack.append(style_tag)
if hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '': if tag in TAG_SPACE:
if not fb2_text or fb2_text[-1] != ' ':
fb2_text += ' '
if hasattr(elem, 'text') and elem.text != None:
fb2_text += prepare_string_for_xml(elem.text) fb2_text += prepare_string_for_xml(elem.text)
for item in elem: for item in elem:
@ -155,12 +162,12 @@ class FB2MLizer(object):
close_tag_list.insert(0, tag_stack.pop()) close_tag_list.insert(0, tag_stack.pop())
fb2_text += self.close_tags(close_tag_list) fb2_text += self.close_tags(close_tag_list)
if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '': if hasattr(elem, 'tail') and elem.tail != None:
if 'p' not in tag_stack: if 'p' not in tag_stack:
fb2_text += '<p>%s</p>' % prepare_string_for_xml(elem.tail) fb2_text += '<p>%s</p>' % prepare_string_for_xml(elem.tail)
else: else:
fb2_text += prepare_string_for_xml(elem.tail) fb2_text += prepare_string_for_xml(elem.tail)
return fb2_text return fb2_text
def close_tags(self, tags): def close_tags(self, tags):