diff --git a/src/odf/odf2xhtml.py b/src/odf/odf2xhtml.py
index 87b8573419..d38d064190 100644
--- a/src/odf/odf2xhtml.py
+++ b/src/odf/odf2xhtml.py
@@ -18,8 +18,8 @@
#
# Contributor(s):
#
-#import pdb
-#pdb.set_trace()
+# import pdb
+# pdb.set_trace()
from xml.sax import handler
from xml.sax.saxutils import escape, quoteattr
from xml.dom import Node
@@ -30,7 +30,7 @@ from namespaces import ANIMNS, CHARTNS, CONFIGNS, DCNS, DR3DNS, DRAWNS, FONS, \
FORMNS, MATHNS, METANS, NUMBERNS, OFFICENS, PRESENTATIONNS, SCRIPTNS, \
SMILNS, STYLENS, SVGNS, TABLENS, TEXTNS, XLINKNS
-if False: # Added by Kovid
+if False: # Added by Kovid
DR3DNS, MATHNS, CHARTNS, CONFIGNS, ANIMNS, FORMNS, SMILNS, SCRIPTNS
# Handling of styles
@@ -55,6 +55,7 @@ if False: # Added by Kovid
# In ODF a style can have a parent, these parents can be chained.
class StyleToCSS:
+
""" The purpose of the StyleToCSS class is to contain the rules to convert
ODF styles to CSS2. Since it needs the generic fonts, it would probably
make sense to also contain the Styles in a dict as well..
@@ -75,8 +76,8 @@ class StyleToCSS:
(FONS,u"border-left"): self.c_fo,
(FONS,u"border-right"): self.c_fo,
(FONS,u"border-top"): self.c_fo,
- (FONS,u"break-after"): self.c_break, # Added by Kovid
- (FONS,u"break-before"): self.c_break,# Added by Kovid
+ (FONS,u"break-after"): self.c_break, # Added by Kovid
+ (FONS,u"break-before"): self.c_break, # Added by Kovid
(FONS,u"color"): self.c_fo,
(FONS,u"font-family"): self.c_fo,
(FONS,u"font-size"): self.c_fo,
@@ -119,12 +120,18 @@ class StyleToCSS:
This method put the font and fallback into a dictionary
"""
htmlgeneric = "sans-serif"
- if generic == "roman": htmlgeneric = "serif"
- elif generic == "swiss": htmlgeneric = "sans-serif"
- elif generic == "modern": htmlgeneric = "monospace"
- elif generic == "decorative": htmlgeneric = "sans-serif"
- elif generic == "script": htmlgeneric = "monospace"
- elif generic == "system": htmlgeneric = "serif"
+ if generic == "roman":
+ htmlgeneric = "serif"
+ elif generic == "swiss":
+ htmlgeneric = "sans-serif"
+ elif generic == "modern":
+ htmlgeneric = "monospace"
+ elif generic == "decorative":
+ htmlgeneric = "sans-serif"
+ elif generic == "script":
+ htmlgeneric = "monospace"
+ elif generic == "system":
+ htmlgeneric = "serif"
self.fontdict[name] = (family, htmlgeneric)
def c_drawfillimage(self, ruleset, sdict, rule, val):
@@ -139,7 +146,7 @@ class StyleToCSS:
selector = rule[1]
sdict[selector] = val
- def c_break(self, ruleset, sdict, rule, val): # Added by Kovid
+ def c_break(self, ruleset, sdict, rule, val): # Added by Kovid
property = 'page-' + rule[1]
values = {'auto': 'auto', 'column': 'always', 'page': 'always',
'even-page': 'left', 'odd-page': 'right',
@@ -159,8 +166,10 @@ class StyleToCSS:
def c_text_align(self, ruleset, sdict, rule, align):
""" Text align """
- if align == "start": align = "left"
- if align == "end": align = "right"
+ if align == "start":
+ align = "left"
+ if align == "end":
+ align = "right"
sdict['text-align'] = align
def c_fn(self, ruleset, sdict, rule, fontstyle):
@@ -168,7 +177,7 @@ class StyleToCSS:
A generic font can be found in two ways. In a
element or as a font-family-generic attribute in text-properties.
"""
- generic = ruleset.get((STYLENS,'font-family-generic') )
+ generic = ruleset.get((STYLENS,'font-family-generic'))
if generic is not None:
self.save_font(fontstyle, fontstyle, generic)
family, htmlgeneric = self.fontdict.get(fontstyle, (fontstyle, 'serif'))
@@ -205,7 +214,7 @@ class StyleToCSS:
sdict['vertical-align'] = textpos[0]
def c_hp(self, ruleset, sdict, rule, hpos):
- #FIXME: Frames wrap-style defaults to 'parallel', graphics to 'none'.
+ # FIXME: Frames wrap-style defaults to 'parallel', graphics to 'none'.
# It is properly set in the parent-styles, but the program doesn't
# collect the information.
wrap = ruleset.get((STYLENS,'wrap'),'parallel')
@@ -213,37 +222,37 @@ class StyleToCSS:
if hpos == "center":
sdict['margin-left'] = "auto"
sdict['margin-right'] = "auto"
-# else:
-# # force it to be *something* then delete it
-# sdict['margin-left'] = sdict['margin-right'] = ''
-# del sdict['margin-left'], sdict['margin-right']
+ # else:
+ # # force it to be *something* then delete it
+ # sdict['margin-left'] = sdict['margin-right'] = ''
+ # del sdict['margin-left'], sdict['margin-right']
if hpos in ("right","outside"):
- if wrap in ( "left", "parallel","dynamic"):
+ if wrap in ("left", "parallel","dynamic"):
sdict['float'] = "right"
elif wrap == "run-through":
- sdict['position'] = "absolute" # Simulate run-through
+ sdict['position'] = "absolute" # Simulate run-through
sdict['top'] = "0"
- sdict['right'] = "0";
- else: # No wrapping
+ sdict['right'] = "0"
+ else: # No wrapping
sdict['margin-left'] = "auto"
sdict['margin-right'] = "0px"
elif hpos in ("left", "inside"):
- if wrap in ( "right", "parallel","dynamic"):
+ if wrap in ("right", "parallel","dynamic"):
sdict['float'] = "left"
elif wrap == "run-through":
- sdict['position'] = "absolute" # Simulate run-through
+ sdict['position'] = "absolute" # Simulate run-through
sdict['top'] = "0"
sdict['left'] = "0"
- else: # No wrapping
+ else: # No wrapping
sdict['margin-left'] = "0px"
sdict['margin-right'] = "auto"
elif hpos in ("from-left", "from-inside"):
- if wrap in ( "right", "parallel"):
+ if wrap in ("right", "parallel"):
sdict['float'] = "left"
else:
- sdict['position'] = "relative" # No wrapping
- if ruleset.has_key( (SVGNS,'x') ):
+ sdict['position'] = "relative" # No wrapping
+ if (SVGNS,'x') in ruleset:
sdict['left'] = ruleset[(SVGNS,'x')]
def c_page_width(self, ruleset, sdict, rule, val):
@@ -279,18 +288,19 @@ class StyleToCSS:
if rule[0] == '':
sdict[rule[1]] = val
continue
- method = self.ruleconversions.get(rule, None )
+ method = self.ruleconversions.get(rule, None)
if method:
method(ruleset, sdict, rule, val)
return sdict
class TagStack:
+
def __init__(self):
self.stack = []
def push(self, tag, attrs):
- self.stack.append( (tag, attrs) )
+ self.stack.append((tag, attrs))
def pop(self):
item = self.stack.pop()
@@ -303,13 +313,14 @@ class TagStack:
def rfindattr(self, attr):
""" Find a tag with the given attribute """
for tag, attrs in self.stack:
- if attrs.has_key(attr):
+ if attr in attrs:
return attrs[attr]
return None
def count_tags(self, tag):
c = 0
for ttag, tattrs in self.stack:
- if ttag == tag: c = c + 1
+ if ttag == tag:
+ c = c + 1
return c
special_styles = {
@@ -335,12 +346,13 @@ special_styles = {
# 'P-Text_20_body':'p'
}
-#-----------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
#
# ODFCONTENTHANDLER
#
-#-----------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
class ODF2XHTML(handler.ContentHandler):
+
""" The ODF2XHTML parses an ODF file and produces XHTML"""
def __init__(self, generate_css=True, embedable=False):
@@ -459,7 +471,6 @@ class ODF2XHTML(handler.ContentHandler):
self.elements[(OFFICENS, u"presentation")] = (None,None)
self.elements[(OFFICENS, u"document-content")] = (None,None)
-
def add_style_file(self, stylefilename, media=None):
""" Add a link to an external style file.
Also turns of the embedding of styles in the HTML
@@ -491,7 +502,7 @@ class ODF2XHTML(handler.ContentHandler):
self.processelem = True
self.processcont = True
self.listtypes = {}
- self.headinglevels = [0, 0,0,0,0,0, 0,0,0,0,0] # level 0 to 10
+ self.headinglevels = [0, 0,0,0,0,0, 0,0,0,0,0] # level 0 to 10
self.use_internal_css = True
self.cs = StyleToCSS()
self.anchors = {}
@@ -506,7 +517,6 @@ class ODF2XHTML(handler.ContentHandler):
# Tags from meta.xml
self.metatags = []
-
def writeout(self, s):
if s != '':
self._wfunc(s)
@@ -526,14 +536,14 @@ class ODF2XHTML(handler.ContentHandler):
self.writeout("<%s>" % tag)
else:
self.writeout("<%s %s>" % (tag, " ".join(a)))
- if block == True:
+ if block:
self.writeout("\n")
def closetag(self, tag, block=True):
""" Close an open HTML tag """
self.htmlstack.pop()
self.writeout("%s>" % tag)
- if block == True:
+ if block:
self.writeout("\n")
def emptytag(self, tag, attrs={}):
@@ -542,34 +552,34 @@ class ODF2XHTML(handler.ContentHandler):
a.append('''%s=%s''' % (key, quoteattr(val)))
self.writeout("<%s %s/>\n" % (tag, " ".join(a)))
-#--------------------------------------------------
+# --------------------------------------------------
# Interface to parser
-#--------------------------------------------------
+# --------------------------------------------------
def characters(self, data):
if self.processelem and self.processcont:
self.data.append(data)
def startElementNS(self, tag, qname, attrs):
- self.pstack.append( (self.processelem, self.processcont) )
+ self.pstack.append((self.processelem, self.processcont))
if self.processelem:
- method = self.elements.get(tag, (None, None) )[0]
+ method = self.elements.get(tag, (None, None))[0]
if method:
self.handle_starttag(tag, method, attrs)
else:
self.unknown_starttag(tag,attrs)
- self.tagstack.push( tag, attrs )
+ self.tagstack.push(tag, attrs)
def endElementNS(self, tag, qname):
stag, attrs = self.tagstack.pop()
if self.processelem:
- method = self.elements.get(tag, (None, None) )[1]
+ method = self.elements.get(tag, (None, None))[1]
if method:
self.handle_endtag(tag, attrs, method)
else:
self.unknown_endtag(tag, attrs)
self.processelem, self.processcont = self.pstack.pop()
-#--------------------------------------------------
+# --------------------------------------------------
def handle_starttag(self, tag, method, attrs):
method(tag,attrs)
@@ -604,27 +614,27 @@ class ODF2XHTML(handler.ContentHandler):
def get_anchor(self, name):
""" Create a unique anchor id for a href name """
- if not self.anchors.has_key(name):
+ if name not in self.anchors:
# Changed by Kovid
self.anchors[name] = "anchor%d" % (len(self.anchors) + 1)
return self.anchors.get(name)
-#--------------------------------------------------
+# --------------------------------------------------
def purgedata(self):
self.data = []
-#-----------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
#
# Handle meta data
#
-#-----------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
def e_dc_title(self, tag, attrs):
""" Get the title from the meta data and create a HTML
"""
self.title = ''.join(self.data)
- #self.metatags.append('%s\n' % escape(self.title))
+ # self.metatags.append('%s\n' % escape(self.title))
self.data = []
def e_dc_metatag(self, tag, attrs):
@@ -652,9 +662,9 @@ class ODF2XHTML(handler.ContentHandler):
"""
anchor_type = attrs.get((TEXTNS,'anchor-type'),'notfound')
htmltag = 'div'
- name = "G-" + attrs.get( (DRAWNS,'style-name'), "")
+ name = "G-" + attrs.get((DRAWNS,'style-name'), "")
if name == 'G-':
- name = "PR-" + attrs.get( (PRESENTATIONNS,'style-name'), "")
+ name = "PR-" + attrs.get((PRESENTATIONNS,'style-name'), "")
name = name.replace(".","_")
if anchor_type == "paragraph":
style = 'position:absolute;'
@@ -665,13 +675,13 @@ class ODF2XHTML(handler.ContentHandler):
style = ''
else:
style = "position: absolute;"
- if attrs.has_key( (SVGNS,"width") ):
+ if (SVGNS,"width") in attrs:
style = style + "width:" + attrs[(SVGNS,"width")] + ";"
- if attrs.has_key( (SVGNS,"height") ):
+ if (SVGNS,"height") in attrs:
style = style + "height:" + attrs[(SVGNS,"height")] + ";"
- if attrs.has_key( (SVGNS,"x") ):
+ if (SVGNS,"x") in attrs:
style = style + "left:" + attrs[(SVGNS,"x")] + ";"
- if attrs.has_key( (SVGNS,"y") ):
+ if (SVGNS,"y") in attrs:
style = style + "top:" + attrs[(SVGNS,"y")] + ";"
if self.generate_css:
self.opentag(htmltag, {'class': name, 'style': style})
@@ -688,9 +698,9 @@ class ODF2XHTML(handler.ContentHandler):
"""
anchor_type = attrs.get((TEXTNS,'anchor-type'),'notfound')
htmltag = 'div'
- name = "G-" + attrs.get( (DRAWNS,'style-name'), "")
+ name = "G-" + attrs.get((DRAWNS,'style-name'), "")
if name == 'G-':
- name = "PR-" + attrs.get( (PRESENTATIONNS,'style-name'), "")
+ name = "PR-" + attrs.get((PRESENTATIONNS,'style-name'), "")
name = name.replace(".","_")
if anchor_type == "paragraph":
style = 'position:relative;'
@@ -701,13 +711,13 @@ class ODF2XHTML(handler.ContentHandler):
style = ''
else:
style = "position:absolute;"
- if attrs.has_key( (SVGNS,"width") ):
+ if (SVGNS,"width") in attrs:
style = style + "width:" + attrs[(SVGNS,"width")] + ";"
- if attrs.has_key( (SVGNS,"height") ):
+ if (SVGNS,"height") in attrs:
style = style + "height:" + attrs[(SVGNS,"height")] + ";"
- if attrs.has_key( (SVGNS,"x") ):
+ if (SVGNS,"x") in attrs:
style = style + "left:" + attrs[(SVGNS,"x")] + ";"
- if attrs.has_key( (SVGNS,"y") ):
+ if (SVGNS,"y") in attrs:
style = style + "top:" + attrs[(SVGNS,"y")] + ";"
if self.generate_css:
self.opentag(htmltag, {'class': name, 'style': style})
@@ -720,7 +730,7 @@ class ODF2XHTML(handler.ContentHandler):
self.closetag('div')
def s_draw_fill_image(self, tag, attrs):
- name = attrs.get( (DRAWNS,'name'), "NoName")
+ name = attrs.get((DRAWNS,'name'), "NoName")
imghref = attrs[(XLINKNS,"href")]
imghref = self.rewritelink(imghref)
self.cs.fillimages[name] = imghref
@@ -738,7 +748,7 @@ class ODF2XHTML(handler.ContentHandler):
anchor_type = parent.get((TEXTNS,'anchor-type'))
imghref = attrs[(XLINKNS,"href")]
imghref = self.rewritelink(imghref)
- htmlattrs = {'alt':"", 'src':imghref }
+ htmlattrs = {'alt':"", 'src':imghref}
if self.generate_css:
if anchor_type != "char":
htmlattrs['style'] = "display: block;"
@@ -747,7 +757,7 @@ class ODF2XHTML(handler.ContentHandler):
def s_draw_object(self, tag, attrs):
""" A is embedded object in the document (e.g. spreadsheet in presentation).
"""
- return # Added by Kovid
+ return # Added by Kovid
objhref = attrs[(XLINKNS,"href")]
# Remove leading "./": from "./Object 1" to "Object 1"
# objhref = objhref [2:]
@@ -764,10 +774,10 @@ class ODF2XHTML(handler.ContentHandler):
"""
try:
class_id = attrs[(DRAWNS,"class-id")]
- except KeyError: # Added by Kovid to ignore without the right
+ except KeyError: # Added by Kovid to ignore without the right
return # attributes
- if class_id and class_id.lower() == "00020803-0000-0000-c000-000000000046": ## Microsoft Graph 97 Chart
- tagattrs = { 'name':'object_ole_graph', 'class':'ole-graph' }
+ if class_id and class_id.lower() == "00020803-0000-0000-c000-000000000046": # Microsoft Graph 97 Chart
+ tagattrs = {'name':'object_ole_graph', 'class':'ole-graph'}
self.opentag('a', tagattrs)
self.closetag('a', tagattrs)
@@ -776,13 +786,13 @@ class ODF2XHTML(handler.ContentHandler):
Therefore if you convert a ODP file, you get a series of