home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import zipfile
- import xml.sax as xml
- from xml.sax import handler, expatreader
- from xml.sax.xmlreader import InputSource
- from xml.sax.saxutils import escape, quoteattr
- from cStringIO import StringIO
- from namespaces import ANIMNS, CHARTNS, CONFIGNS, DCNS, DR3DNS, DRAWNS, FONS, FORMNS, MATHNS, METANS, NUMBERNS, OFFICENS, PRESENTATIONNS, SCRIPTNS, SMILNS, STYLENS, SVGNS, TABLENS, TEXTNS, XLINKNS
-
- class StyleToCSS:
-
- def __init__(self):
- self.fontdict = { }
- self.fillimages = { }
- self.ruleconversions = {
- (DRAWNS, u'fill-image-name'): self.c_drawfillimage,
- (FONS, u'background-color'): self.c_fo,
- (FONS, u'border'): self.c_fo,
- (FONS, u'border-bottom'): self.c_fo,
- (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,
- (FONS, u'break-before'): self.c_break,
- (FONS, u'color'): self.c_fo,
- (FONS, u'font-family'): self.c_fo,
- (FONS, u'font-size'): self.c_fo,
- (FONS, u'font-style'): self.c_fo,
- (FONS, u'font-variant'): self.c_fo,
- (FONS, u'font-weight'): self.c_fo,
- (FONS, u'line-height'): self.c_fo,
- (FONS, u'margin'): self.c_fo,
- (FONS, u'margin-bottom'): self.c_fo,
- (FONS, u'margin-left'): self.c_fo,
- (FONS, u'margin-right'): self.c_fo,
- (FONS, u'margin-top'): self.c_fo,
- (FONS, u'min-height'): self.c_fo,
- (FONS, u'padding'): self.c_fo,
- (FONS, u'padding-bottom'): self.c_fo,
- (FONS, u'padding-left'): self.c_fo,
- (FONS, u'padding-right'): self.c_fo,
- (FONS, u'padding-top'): self.c_fo,
- (FONS, u'page-width'): self.c_page_width,
- (FONS, u'page-height'): self.c_page_height,
- (FONS, u'text-align'): self.c_text_align,
- (FONS, u'text-indent'): self.c_fo,
- (TABLENS, u'border-model'): self.c_border_model,
- (STYLENS, u'column-width'): self.c_width,
- (STYLENS, u'font-name'): self.c_fn,
- (STYLENS, u'horizontal-pos'): self.c_hp,
- (STYLENS, u'text-position'): self.c_text_position,
- (STYLENS, u'text-line-through-style'): self.c_text_line_through_style,
- (STYLENS, u'text-underline-style'): self.c_text_underline_style,
- (STYLENS, u'width'): self.c_width }
-
-
- def save_font(self, name, family, generic):
- 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'
-
- self.fontdict[name] = (family, htmlgeneric)
-
-
- def c_drawfillimage(self, ruleset, sdict, rule, val):
- sdict['background-image'] = "url('%s')" % self.fillimages[val]
-
-
- def c_fo(self, ruleset, sdict, rule, val):
- selector = rule[1]
- sdict[selector] = val
-
-
- def c_break(self, ruleset, sdict, rule, val):
- property = 'page-' + rule[1]
- values = {
- 'auto': 'auto',
- 'column': 'always',
- 'page': 'always',
- 'even-page': 'left',
- 'odd-page': 'right',
- 'inherit': 'inherit' }
- sdict[property] = values.get(val, 'auto')
-
-
- def c_border_model(self, ruleset, sdict, rule, val):
- if val == 'collapsing':
- sdict['border-collapse'] = 'collapse'
- else:
- sdict['border-collapse'] = 'separate'
-
-
- def c_width(self, ruleset, sdict, rule, val):
- sdict['width'] = val
-
-
- def c_text_align(self, ruleset, sdict, rule, align):
- if align == 'start':
- align = 'left'
-
- if align == 'end':
- align = 'right'
-
- sdict['text-align'] = align
-
-
- def c_fn(self, ruleset, sdict, rule, fontstyle):
- 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'))
- sdict['font-family'] = '%s, %s' % (family, htmlgeneric)
-
-
- def c_text_position(self, ruleset, sdict, rule, tp):
- textpos = tp.split(' ')
- if len(textpos) == 2 and textpos[0] != '0%':
- sdict['font-size'] = textpos[1]
-
- if textpos[0] == 'super':
- sdict['vertical-align'] = '33%'
- elif textpos[0] == 'sub':
- sdict['vertical-align'] = '-33%'
- else:
- sdict['vertical-align'] = textpos[0]
-
-
- def c_hp(self, ruleset, sdict, rule, hpos):
- wrap = ruleset.get((STYLENS, 'wrap'), 'parallel')
- if hpos == 'center':
- sdict['margin-left'] = 'auto'
- sdict['margin-right'] = 'auto'
-
- if hpos in ('right', 'outside'):
- if wrap in ('left', 'parallel', 'dynamic'):
- sdict['float'] = 'right'
- elif wrap == 'run-through':
- sdict['position'] = 'absolute'
- sdict['top'] = '0'
- sdict['right'] = '0'
- else:
- sdict['margin-left'] = 'auto'
- sdict['margin-right'] = '0px'
- elif hpos in ('left', 'inside'):
- if wrap in ('right', 'parallel', 'dynamic'):
- sdict['float'] = 'left'
- elif wrap == 'run-through':
- sdict['position'] = 'absolute'
- sdict['top'] = '0'
- sdict['left'] = '0'
- else:
- sdict['margin-left'] = '0px'
- sdict['margin-right'] = 'auto'
- elif hpos in ('from-left', 'from-inside'):
- if wrap in ('right', 'parallel'):
- sdict['float'] = 'left'
- else:
- sdict['position'] = 'relative'
- if ruleset.has_key((SVGNS, 'x')):
- sdict['left'] = ruleset[(SVGNS, 'x')]
-
-
-
-
- def c_page_width(self, ruleset, sdict, rule, val):
- sdict['width'] = val
-
-
- def c_text_underline_style(self, ruleset, sdict, rule, val):
- if val and val != 'none':
- sdict['text-decoration'] = 'underline'
-
-
-
- def c_text_line_through_style(self, ruleset, sdict, rule, val):
- if val and val != 'none':
- sdict['text-decoration'] = 'line-through'
-
-
-
- def c_page_height(self, ruleset, sdict, rule, val):
- sdict['height'] = val
-
-
- def convert_styles(self, ruleset):
- sdict = { }
- for rule, val in ruleset.items():
- if rule[0] == '':
- sdict[rule[1]] = val
- continue
-
- method = self.ruleconversions.get(rule, None)
- if method:
- method(ruleset, sdict, rule, val)
- continue
-
- return sdict
-
-
-
- class TagStack:
-
- def __init__(self):
- self.stack = []
-
-
- def push(self, tag, attrs):
- self.stack.append((tag, attrs))
-
-
- def pop(self):
- item = self.stack.pop()
- return item
-
-
- def stackparent(self):
- item = self.stack[-1]
- return item[1]
-
-
- def rfindattr(self, attr):
- for tag, attrs in self.stack:
- if attrs.has_key(attr):
- return attrs[attr]
-
-
-
- def count_tags(self, tag):
- c = 0
- for ttag, tattrs in self.stack:
- if ttag == tag:
- c = c + 1
- continue
-
- return c
-
-
- special_styles = {
- 'S-Emphasis': 'em',
- 'S-Citation': 'cite',
- 'S-Strong_20_Emphasis': 'strong',
- 'S-Variable': 'var',
- 'S-Definition': 'dfn',
- 'S-Teletype': 'tt',
- 'P-Heading_20_1': 'h1',
- 'P-Heading_20_2': 'h2',
- 'P-Heading_20_3': 'h3',
- 'P-Heading_20_4': 'h4',
- 'P-Heading_20_5': 'h5',
- 'P-Heading_20_6': 'h6',
- 'P-Addressee': 'address',
- 'P-Preformatted_20_Text': 'pre' }
-
- class ODF2XHTML(handler.ContentHandler):
-
- def __init__(self, generate_css = True, embedable = False):
- self.generate_css = generate_css
- self.elements = {
- (DCNS, 'title'): (self.s_processcont, self.e_dc_title),
- (DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage),
- (DCNS, 'creator'): (self.s_processcont, self.e_dc_metatag),
- (DCNS, 'description'): (self.s_processcont, self.e_dc_metatag),
- (DCNS, 'date'): (self.s_processcont, self.e_dc_metatag),
- (DRAWNS, 'frame'): (self.s_draw_frame, self.e_draw_frame),
- (DRAWNS, 'image'): (self.s_draw_image, None),
- (DRAWNS, 'fill-image'): (self.s_draw_fill_image, None),
- (DRAWNS, 'layer-set'): (self.s_ignorexml, None),
- (DRAWNS, 'page'): (self.s_draw_page, self.e_draw_page),
- (DRAWNS, 'text-box'): (self.s_draw_textbox, self.e_draw_textbox),
- (METANS, 'creation-date'): (self.s_processcont, self.e_dc_metatag),
- (METANS, 'generator'): (self.s_processcont, self.e_dc_metatag),
- (METANS, 'initial-creator'): (self.s_processcont, self.e_dc_metatag),
- (METANS, 'keyword'): (self.s_processcont, self.e_dc_metatag),
- (NUMBERNS, 'boolean-style'): (self.s_ignorexml, None),
- (NUMBERNS, 'currency-style'): (self.s_ignorexml, None),
- (NUMBERNS, 'date-style'): (self.s_ignorexml, None),
- (NUMBERNS, 'number-style'): (self.s_ignorexml, None),
- (NUMBERNS, 'text-style'): (self.s_ignorexml, None),
- (OFFICENS, 'automatic-styles'): (self.s_office_automatic_styles, None),
- (OFFICENS, 'document-content'): (self.s_office_document_content, self.e_office_document_content),
- (OFFICENS, 'forms'): (self.s_ignorexml, None),
- (OFFICENS, 'master-styles'): (self.s_office_master_styles, None),
- (OFFICENS, 'meta'): (self.s_ignorecont, None),
- (OFFICENS, 'presentation'): (self.s_office_presentation, self.e_office_presentation),
- (OFFICENS, 'spreadsheet'): (self.s_office_spreadsheet, self.e_office_spreadsheet),
- (OFFICENS, 'styles'): (self.s_office_styles, None),
- (OFFICENS, 'text'): (self.s_office_text, self.e_office_text),
- (OFFICENS, 'scripts'): (self.s_ignorexml, None),
- (PRESENTATIONNS, 'notes'): (self.s_ignorexml, None),
- (STYLENS, 'default-page-layout'): (self.s_ignorexml, None),
- (STYLENS, 'default-style'): (self.s_style_default_style, self.e_style_default_style),
- (STYLENS, 'drawing-page-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'font-face'): (self.s_style_font_face, None),
- (STYLENS, 'graphic-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'handout-master'): (self.s_ignorexml, None),
- (STYLENS, 'master-page'): (self.s_style_master_page, None),
- (STYLENS, 'page-layout-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'page-layout'): (self.s_ignorexml, None),
- (STYLENS, 'paragraph-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'style'): (self.s_style_style, self.e_style_style),
- (STYLENS, 'table-cell-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'table-column-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'table-properties'): (self.s_style_handle_properties, None),
- (STYLENS, 'text-properties'): (self.s_style_handle_properties, None),
- (SVGNS, 'desc'): (self.s_ignorexml, None),
- (TABLENS, 'covered-table-cell'): (self.s_ignorexml, None),
- (TABLENS, 'table-cell'): (self.s_table_table_cell, self.e_table_table_cell),
- (TABLENS, 'table-column'): (self.s_table_table_column, None),
- (TABLENS, 'table-row'): (self.s_table_table_row, self.e_table_table_row),
- (TABLENS, 'table'): (self.s_table_table, self.e_table_table),
- (TEXTNS, 'a'): (self.s_text_a, self.e_text_a),
- (TEXTNS, 'alphabetical-index-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'bibliography-configuration'): (self.s_ignorexml, None),
- (TEXTNS, 'bibliography-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'h'): (self.s_text_h, self.e_text_h),
- (TEXTNS, 'illustration-index-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'line-break'): (self.s_text_line_break, None),
- (TEXTNS, 'linenumbering-configuration'): (self.s_ignorexml, None),
- (TEXTNS, 'list'): (self.s_text_list, self.e_text_list),
- (TEXTNS, 'list-item'): (self.s_text_list_item, self.e_text_list_item),
- (TEXTNS, 'list-level-style-bullet'): (self.s_text_list_level_style_bullet, self.e_text_list_level_style_bullet),
- (TEXTNS, 'list-level-style-number'): (self.s_text_list_level_style_number, self.e_text_list_level_style_number),
- (TEXTNS, 'list-style'): (None, None),
- (TEXTNS, 'note'): (self.s_text_note, None),
- (TEXTNS, 'note-body'): (self.s_text_note_body, self.e_text_note_body),
- (TEXTNS, 'note-citation'): (None, self.e_text_note_citation),
- (TEXTNS, 'notes-configuration'): (self.s_ignorexml, None),
- (TEXTNS, 'object-index-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'p'): (self.s_text_p, self.e_text_p),
- (TEXTNS, 's'): (self.s_text_s, None),
- (TEXTNS, 'span'): (self.s_text_span, self.e_text_span),
- (TEXTNS, 'tab'): (self.s_text_tab, None),
- (TEXTNS, 'table-index-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'table-of-content-source'): (self.s_text_x_source, self.e_text_x_source),
- (TEXTNS, 'user-index-source'): (self.s_text_x_source, self.e_text_x_source) }
- if embedable:
- self.elements[(OFFICENS, u'text')] = (None, None)
- self.elements[(OFFICENS, u'spreadsheet')] = (None, None)
- self.elements[(OFFICENS, u'presentation')] = (None, None)
- self.elements[(OFFICENS, u'document-content')] = (None, None)
-
-
-
- def writeout(self, s):
- if s != '':
- self._wfunc(s)
-
-
-
- def writedata(self):
- d = ''.join(self.data)
- if d != '':
- self.writeout(escape(d))
-
-
-
- def opentag(self, tag, attrs = { }, block = False):
- a = []
- for key, val in attrs.items():
- a.append('%s=%s' % (key, quoteattr(val)))
-
- if len(a) == 0:
- self.writeout('<%s>' % tag)
- else:
- self.writeout('<%s %s>' % (tag, ' '.join(a)))
- if block == True:
- self.writeout('\n')
-
-
-
- def closetag(self, tag, block = True):
- self.writeout('</%s>' % tag)
- if block == True:
- self.writeout('\n')
-
-
-
- def emptytag(self, tag, attrs = { }):
- a = []
- for key, val in attrs.items():
- a.append('%s=%s' % (key, quoteattr(val)))
-
- self.writeout('<%s %s/>\n' % (tag, ' '.join(a)))
-
-
- def characters(self, data):
- if self.processelem and self.processcont:
- self.data.append(data)
-
-
-
- def handle_starttag(self, tag, method, attrs):
- method(tag, attrs)
-
-
- def handle_endtag(self, tag, attrs, method):
- method(tag, attrs)
-
-
- def startElementNS(self, tag, qname, attrs):
- self.pstack.append((self.processelem, self.processcont))
- if self.processelem:
- 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)
-
-
- def endElementNS(self, tag, qname):
- (stag, attrs) = self.tagstack.pop()
- if self.processelem:
- 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 unknown_starttag(self, tag, attrs):
- pass
-
-
- def unknown_endtag(self, tag, attrs):
- pass
-
-
- def s_ignorexml(self, tag, attrs):
- self.processelem = False
-
-
- def s_ignorecont(self, tag, attrs):
- self.processcont = False
-
-
- def s_processcont(self, tag, attrs):
- self.processcont = True
-
-
- def classname(self, attrs):
- c = attrs[(TEXTNS, 'style-name')]
- c = c.replace('.', '_')
- return c
-
-
- def get_anchor(self, name):
- if not self.anchors.has_key(name):
- self.anchors[name] = 'anchor%03d' % (len(self.anchors) + 1)
-
- return self.anchors.get(name)
-
-
- def purgedata(self):
- self.data = []
-
-
- def e_dc_title(self, tag, attrs):
- self.metatags.append('<title>%s</title>\n' % escape(''.join(self.data)))
- self.title = ''.join(self.data)
- self.data = []
-
-
- def e_dc_metatag(self, tag, attrs):
- self.metatags.append('<meta name="%s" content=%s/>\n' % (tag[1], quoteattr(''.join(self.data))))
- self.data = []
-
-
- def e_dc_contentlanguage(self, tag, attrs):
- self.metatags.append('<meta http-equiv="content-language" content="%s"/>\n' % ''.join(self.data))
- self.data = []
-
-
- def s_draw_frame(self, tag, attrs):
- anchor_type = attrs.get((TEXTNS, 'anchor-type'), 'char')
- htmltag = 'div'
- name = 'G-' + attrs.get((DRAWNS, 'style-name'), '')
- if name == 'G-':
- name = 'PR-' + attrs.get((PRESENTATIONNS, 'style-name'), '')
-
- name = name.replace('.', '_')
- if anchor_type == 'paragraph':
- style = 'position:relative;'
- elif anchor_type == 'char':
- style = 'position:relative;'
- elif anchor_type == 'as-char':
- htmltag = 'div'
- style = ''
- else:
- style = 'position: absolute;'
- if attrs.has_key((SVGNS, 'width')):
- style = style + 'width:' + attrs[(SVGNS, 'width')] + ';'
-
- if attrs.has_key((SVGNS, 'height')):
- style = style + 'height:' + attrs[(SVGNS, 'height')] + ';'
-
- if attrs.has_key((SVGNS, 'x')):
- style = style + 'left:' + attrs[(SVGNS, 'x')] + ';'
-
- if attrs.has_key((SVGNS, 'y')):
- style = style + 'top:' + attrs[(SVGNS, 'y')] + ';'
-
- if self.generate_css:
- self.opentag(htmltag, {
- 'class': name,
- 'style': style })
- else:
- self.opentag(htmltag)
-
-
- def e_draw_frame(self, tag, attrs):
- self.closetag('div')
-
-
- def s_draw_fill_image(self, tag, attrs):
- name = attrs.get((DRAWNS, 'name'), 'NoName')
- imghref = attrs[(XLINKNS, 'href')]
- imghref = self.rewritelink(imghref)
- self.cs.fillimages[name] = imghref
-
-
- def rewritelink(self, imghref):
- return imghref
-
-
- def s_draw_image(self, tag, attrs):
- parent = self.tagstack.stackparent()
- anchor_type = parent.get((TEXTNS, 'anchor-type'))
- imghref = attrs[(XLINKNS, 'href')]
- imghref = self.rewritelink(imghref)
- htmlattrs = {
- 'alt': '',
- 'src': imghref }
- if self.generate_css:
- if anchor_type != 'char':
- htmlattrs['style'] = 'display: block;'
-
-
- self.emptytag('img', htmlattrs)
-
-
- def s_draw_page(self, tag, attrs):
- name = attrs.get((DRAWNS, 'name'), 'NoName')
- stylename = attrs.get((DRAWNS, 'style-name'), '')
- stylename = stylename.replace('.', '_')
- masterpage = attrs.get((DRAWNS, 'master-page-name'), '')
- masterpage = masterpage.replace('.', '_')
- if self.generate_css:
- self.opentag('fieldset', {
- 'class': 'DP-%s MP-%s' % (stylename, masterpage) })
- else:
- self.opentag('fieldset')
- self.opentag('legend')
- self.writeout(escape(name))
- self.closetag('legend')
-
-
- def e_draw_page(self, tag, attrs):
- self.closetag('fieldset')
-
-
- def s_draw_textbox(self, tag, attrs):
- style = ''
- if attrs.has_key((FONS, 'min-height')):
- style = style + 'min-height:' + attrs[(FONS, 'min-height')] + ';'
-
- self.opentag('div')
-
-
- def e_draw_textbox(self, tag, attrs):
- self.closetag('div')
-
-
- def html_body(self, tag, attrs):
- self.writedata()
- if self.generate_css:
- self.opentag('style', {
- 'type': 'text/css' }, True)
- self.writeout('/*<![CDATA[*/\n')
- self.writeout('\nimg { width: 100%; height: 100%; }\n')
- self.writeout('* { padding: 0; margin: 0; background-color:white; }\n')
- self.writeout('body { margin: 0 1em; }\n')
- self.writeout('ol, ul { padding-left: 2em; }\n')
- self.generate_stylesheet()
- self.writeout('/*]]>*/\n')
- self.closetag('style')
-
- self.purgedata()
- self.closetag('head')
- self.opentag('body', block = True)
-
-
- def generate_stylesheet(self):
- for name in self.stylestack:
- styles = self.styledict.get(name)
- if styles.has_key('__style-family') and self.styledict.has_key(styles['__style-family']):
- familystyle = self.styledict[styles['__style-family']].copy()
- del styles['__style-family']
- for style, val in styles.items():
- familystyle[style] = val
-
- styles = familystyle
-
- while styles.has_key('__parent-style-name') and self.styledict.has_key(styles['__parent-style-name']):
- parentstyle = self.styledict[styles['__parent-style-name']].copy()
- del styles['__parent-style-name']
- for style, val in styles.items():
- parentstyle[style] = val
-
- styles = parentstyle
- self.styledict[name] = styles
-
- for name in self.stylestack:
- styles = self.styledict.get(name)
- css2 = self.cs.convert_styles(styles)
- self.writeout('%s {\n' % name)
- for style, val in css2.items():
- self.writeout('\t%s: %s;\n' % (style, val))
-
- self.writeout('}\n')
-
-
-
- def generate_footnotes(self):
- if self.currentnote == 0:
- return None
- if self.generate_css:
- self.opentag('ol', {
- 'style': 'border-top: 1px solid black' }, True)
- else:
- self.opentag('ol')
- for key in range(1, self.currentnote + 1):
- note = self.notedict[key]
- self.opentag('li', {
- 'id': 'footnote-%d' % key })
- self.writeout(note['body'])
- self.closetag('li')
-
- self.closetag('ol')
-
-
- def s_office_automatic_styles(self, tag, attrs):
- if self.xmlfile == 'styles.xml':
- self.autoprefix = 'A'
- else:
- self.autoprefix = ''
-
-
- def s_office_document_content(self, tag, attrs):
- self.writeout('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ')
- self.writeout('"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n')
- self.opentag('html', {
- 'xmlns': 'http://www.w3.org/1999/xhtml' }, True)
- self.opentag('head', block = True)
- self.emptytag('meta', {
- 'http-equiv': 'Content-Type',
- 'content': 'text/html;charset=UTF-8' })
- for metaline in self.metatags:
- self.writeout(metaline)
-
-
-
- def e_office_document_content(self, tag, attrs):
- self.closetag('html')
-
-
- def s_office_master_styles(self, tag, attrs):
- pass
-
-
- def s_office_presentation(self, tag, attrs):
- self.styledict['p'] = {
- (FONS, u'font-size'): u'24pt' }
- self.styledict['presentation'] = {
- (FONS, u'font-size'): u'24pt' }
- self.html_body(tag, attrs)
-
-
- def e_office_presentation(self, tag, attrs):
- self.generate_footnotes()
- self.closetag('body')
-
-
- def s_office_spreadsheet(self, tag, attrs):
- self.html_body(tag, attrs)
-
-
- def e_office_spreadsheet(self, tag, attrs):
- self.generate_footnotes()
- self.closetag('body')
-
-
- def s_office_styles(self, tag, attrs):
- self.autoprefix = ''
-
-
- def s_office_text(self, tag, attrs):
- self.styledict['frame'] = {
- (STYLENS, 'wrap'): u'parallel' }
- self.html_body(tag, attrs)
-
-
- def e_office_text(self, tag, attrs):
- self.generate_footnotes()
- self.closetag('body')
-
-
- def s_style_handle_properties(self, tag, attrs):
- if self.currentstyle is None:
- return None
- for key, attr in attrs.items():
- self.styledict[self.currentstyle][key] = attr
-
-
- familymap = {
- 'frame': 'frame',
- 'paragraph': 'p',
- 'presentation': 'presentation',
- 'text': 'span',
- 'section': 'div',
- 'table': 'table',
- 'table-cell': 'td',
- 'table-column': 'col',
- 'table-row': 'tr',
- 'graphic': 'graphic' }
-
- def s_style_default_style(self, tag, attrs):
- family = attrs[(STYLENS, 'family')]
- htmlfamily = self.familymap.get(family, 'unknown')
- self.currentstyle = htmlfamily
- self.styledict[self.currentstyle] = { }
-
-
- def e_style_default_style(self, tag, attrs):
- self.currentstyle = None
-
-
- def s_style_font_face(self, tag, attrs):
- name = attrs[(STYLENS, 'name')]
- family = attrs[(SVGNS, 'font-family')]
- generic = attrs.get((STYLENS, 'font-family-generic'), '')
- self.cs.save_font(name, family, generic)
-
-
- def s_style_footer(self, tag, attrs):
- self.opentag('div', {
- 'id': 'footer' })
- self.purgedata()
-
-
- def e_style_footer(self, tag, attrs):
- self.writedata()
- self.closetag('div')
- self.purgedata()
-
-
- def s_style_footer_style(self, tag, attrs):
- self.currentstyle = '@print #footer'
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
-
-
- def s_style_header(self, tag, attrs):
- self.opentag('div', {
- 'id': 'header' })
- self.purgedata()
-
-
- def e_style_header(self, tag, attrs):
- self.writedata()
- self.closetag('div')
- self.purgedata()
-
-
- def s_style_header_style(self, tag, attrs):
- self.currentstyle = '@print #header'
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
-
-
- def s_style_default_page_layout(self, tag, attrs):
- self.currentstyle = '@page'
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
-
-
- def s_style_page_layout(self, tag, attrs):
- name = attrs[(STYLENS, 'name')]
- name = name.replace('.', '_')
- self.currentstyle = '@page ' + name
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
-
-
- def e_style_page_layout(self, tag, attrs):
- self.currentstyle = None
-
-
- def s_style_master_page(self, tag, attrs):
- name = attrs[(STYLENS, 'name')]
- name = name.replace('.', '_')
- self.currentstyle = '.MP-' + name
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = {
- ('', 'position'): 'relative' }
- pagelayout = attrs.get((STYLENS, 'page-layout-name'), None)
- if pagelayout:
- pagelayout = '.PL-' + pagelayout
- if self.styledict.has_key(pagelayout):
- styles = self.styledict[pagelayout]
- for style, val in styles.items():
- self.styledict[self.currentstyle][style] = val
-
- else:
- self.styledict[self.currentstyle]['__parent-style-name'] = pagelayout
-
- self.s_ignorexml(tag, attrs)
-
- familyshort = {
- 'drawing-page': 'DP',
- 'paragraph': 'P',
- 'presentation': 'PR',
- 'text': 'S',
- 'section': 'D',
- 'table': 'T',
- 'table-cell': 'TD',
- 'table-column': 'TC',
- 'table-row': 'TR',
- 'graphic': 'G' }
-
- def s_style_style(self, tag, attrs):
- name = attrs[(STYLENS, 'name')]
- name = name.replace('.', '_')
- family = attrs[(STYLENS, 'family')]
- htmlfamily = self.familymap.get(family, 'unknown')
- sfamily = self.familyshort.get(family, 'X')
- name = '%s%s-%s' % (self.autoprefix, sfamily, name)
- parent = attrs.get((STYLENS, 'parent-style-name'))
- self.currentstyle = special_styles.get(name, '.' + name)
- self.stylestack.append(self.currentstyle)
- if not self.styledict.has_key(self.currentstyle):
- self.styledict[self.currentstyle] = { }
-
- self.styledict[self.currentstyle]['__style-family'] = htmlfamily
- if parent:
- parent = '%s-%s' % (sfamily, parent)
- parent = special_styles.get(parent, '.' + parent)
- if self.styledict.has_key(parent):
- styles = self.styledict[parent]
- for style, val in styles.items():
- self.styledict[self.currentstyle][style] = val
-
- else:
- self.styledict[self.currentstyle]['__parent-style-name'] = parent
-
-
-
- def e_style_style(self, tag, attrs):
- self.currentstyle = None
-
-
- def s_table_table(self, tag, attrs):
- c = attrs.get((TABLENS, 'style-name'), None)
- if c and self.generate_css:
- c = c.replace('.', '_')
- self.opentag('table', {
- 'class': 'T-%s' % c })
- else:
- self.opentag('table')
- self.purgedata()
-
-
- def e_table_table(self, tag, attrs):
- self.writedata()
- self.closetag('table')
- self.purgedata()
-
-
- def s_table_table_cell(self, tag, attrs):
- htmlattrs = { }
- rowspan = attrs.get((TABLENS, 'number-rows-spanned'))
- if rowspan:
- htmlattrs['rowspan'] = rowspan
-
- colspan = attrs.get((TABLENS, 'number-columns-spanned'))
- if colspan:
- htmlattrs['colspan'] = colspan
-
- c = attrs.get((TABLENS, 'style-name'))
- if c:
- htmlattrs['class'] = 'TD-%s' % c.replace('.', '_')
-
- self.opentag('td', htmlattrs)
- self.purgedata()
-
-
- def e_table_table_cell(self, tag, attrs):
- self.writedata()
- self.closetag('td')
- self.purgedata()
-
-
- def s_table_table_column(self, tag, attrs):
- c = attrs.get((TABLENS, 'style-name'), None)
- repeated = int(attrs.get((TABLENS, 'number-columns-repeated'), 1))
- htmlattrs = { }
- if c:
- htmlattrs['class'] = 'TC-%s' % c.replace('.', '_')
-
- for x in xrange(repeated):
- self.emptytag('col', htmlattrs)
-
- self.purgedata()
-
-
- def s_table_table_row(self, tag, attrs):
- c = attrs.get((TABLENS, 'style-name'), None)
- htmlattrs = { }
- if c:
- htmlattrs['class'] = 'TR-%s' % c.replace('.', '_')
-
- self.opentag('tr', htmlattrs)
- self.purgedata()
-
-
- def e_table_table_row(self, tag, attrs):
- self.writedata()
- self.closetag('tr')
- self.purgedata()
-
-
- def s_text_a(self, tag, attrs):
- self.writedata()
- href = attrs[(XLINKNS, 'href')].split('|')[0]
- if href[0] == '#':
- href = '#' + self.get_anchor(href[1:])
-
- self.opentag('a', {
- 'href': href })
- self.purgedata()
-
-
- def e_text_a(self, tag, attrs):
- self.writedata()
- self.closetag('a', False)
- self.purgedata()
-
-
- def s_text_h(self, tag, attrs):
- level = int(attrs[(TEXTNS, 'outline-level')])
- if level > 6:
- level = 6
-
- if level < 1:
- level = 1
-
- self.headinglevels[level] = self.headinglevels[level] + 1
- name = self.classname(attrs)
- for x in range(level + 1, 10):
- self.headinglevels[x] = 0
-
- special = special_styles.get('P-' + name)
- if special or not (self.generate_css):
- self.opentag('h%s' % level)
- else:
- self.opentag('h%s' % level, {
- 'class': 'P-%s' % name })
- self.purgedata()
-
-
- def e_text_h(self, tag, attrs):
- self.writedata()
- level = int(attrs[(TEXTNS, 'outline-level')])
- if level > 6:
- level = 6
-
- if level < 1:
- level = 1
-
- lev = self.headinglevels[1:level + 1]
- outline = '.'.join(map(str, lev))
- anchor = self.get_anchor('%s.%s' % (outline, ''.join(self.data)))
- self.opentag('a', {
- 'id': anchor })
- self.closetag('a', False)
- self.closetag('h%s' % level)
- self.purgedata()
-
-
- def s_text_line_break(self, tag, attrs):
- self.writedata()
- self.emptytag('br')
- self.purgedata()
-
-
- def s_text_list(self, tag, attrs):
- name = attrs.get((TEXTNS, 'style-name'))
- level = self.tagstack.count_tags(tag) + 1
- if name:
- name = name.replace('.', '_')
- else:
- name = self.tagstack.rfindattr((TEXTNS, 'style-name'))
- list_class = '%s_%d' % (name, level)
- if self.generate_css:
- self.opentag('%s' % self.listtypes.get(list_class, 'UL'), {
- 'class': list_class })
- else:
- self.opentag('%s' % self.listtypes.get(list_class, 'UL'))
- self.purgedata()
-
-
- def e_text_list(self, tag, attrs):
- self.writedata()
- name = attrs.get((TEXTNS, 'style-name'))
- level = self.tagstack.count_tags(tag) + 1
- if name:
- name = name.replace('.', '_')
- else:
- name = self.tagstack.rfindattr((TEXTNS, 'style-name'))
- list_class = '%s_%d' % (name, level)
- self.closetag(self.listtypes.get(list_class, 'UL'))
- self.purgedata()
-
-
- def s_text_list_item(self, tag, attrs):
- self.opentag('li')
- self.purgedata()
-
-
- def e_text_list_item(self, tag, attrs):
- self.writedata()
- self.closetag('li')
- self.purgedata()
-
-
- def s_text_list_level_style_bullet(self, tag, attrs):
- name = self.tagstack.rfindattr((STYLENS, 'name'))
- level = attrs[(TEXTNS, 'level')]
- self.prevstyle = self.currentstyle
- list_class = '%s_%s' % (name, level)
- self.listtypes[list_class] = 'ul'
- self.currentstyle = '.%s_%s' % (name.replace('.', '_'), level)
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
- level = int(level)
- listtype = ('square', 'disc', 'circle')[level % 3]
- self.styledict[self.currentstyle][('', 'list-style-type')] = listtype
-
-
- def e_text_list_level_style_bullet(self, tag, attrs):
- self.currentstyle = self.prevstyle
- del self.prevstyle
-
-
- def s_text_list_level_style_number(self, tag, attrs):
- name = self.tagstack.stackparent()[(STYLENS, 'name')]
- level = attrs[(TEXTNS, 'level')]
- num_format = attrs.get((STYLENS, 'name'), '1')
- list_class = '%s_%s' % (name, level)
- self.prevstyle = self.currentstyle
- self.currentstyle = '.%s_%s' % (name.replace('.', '_'), level)
- self.listtypes[list_class] = 'ol'
- self.stylestack.append(self.currentstyle)
- self.styledict[self.currentstyle] = { }
- if num_format == '1':
- listtype = 'decimal'
- elif num_format == 'I':
- listtype = 'upper-roman'
- elif num_format == 'i':
- listtype = 'lower-roman'
- elif num_format == 'A':
- listtype = 'upper-alpha'
- elif num_format == 'a':
- listtype = 'lower-alpha'
- else:
- listtype = 'decimal'
- self.styledict[self.currentstyle][('', 'list-style-type')] = listtype
-
-
- def e_text_list_level_style_number(self, tag, attrs):
- self.currentstyle = self.prevstyle
- del self.prevstyle
-
-
- def s_text_note(self, tag, attrs):
- self.writedata()
- self.purgedata()
- self.currentnote = self.currentnote + 1
- self.notedict[self.currentnote] = { }
- self.notebody = []
-
-
- def e_text_note(self, tag, attrs):
- pass
-
-
- def collectnote(self, s):
- if s != '':
- self.notebody.append(s)
-
-
-
- def s_text_note_body(self, tag, attrs):
- self._orgwfunc = self._wfunc
- self._wfunc = self.collectnote
-
-
- def e_text_note_body(self, tag, attrs):
- self._wfunc = self._orgwfunc
- self.notedict[self.currentnote]['body'] = ''.join(self.notebody)
- self.notebody = ''
- del self._orgwfunc
-
-
- def e_text_note_citation(self, tag, attrs):
- mark = ''.join(self.data)
- self.notedict[self.currentnote]['citation'] = mark
- self.opentag('a', {
- 'href': '#footnote-%s' % self.currentnote })
- self.opentag('sup')
- self.writeout(unicode(self.currentnote))
- self.closetag('sup')
- self.closetag('a')
-
-
- def s_text_p(self, tag, attrs):
- htmlattrs = { }
- specialtag = 'p'
- c = attrs.get((TEXTNS, 'style-name'), None)
- if c:
- c = c.replace('.', '_')
- specialtag = special_styles.get('P-' + c)
- if specialtag is None:
- specialtag = 'p'
- if self.generate_css:
- htmlattrs['class'] = 'P-%s' % c
-
-
-
- self.opentag(specialtag, htmlattrs)
- self.purgedata()
-
-
- def e_text_p(self, tag, attrs):
- specialtag = 'p'
- c = attrs.get((TEXTNS, 'style-name'), None)
- if c:
- c = c.replace('.', '_')
- specialtag = special_styles.get('P-' + c)
- if specialtag is None:
- specialtag = 'p'
-
-
- self.writedata()
- if not self.data:
- self.writeout(' ')
-
- self.closetag(specialtag)
- self.purgedata()
-
-
- def s_text_s(self, tag, attrs):
- c = attrs.get((TEXTNS, 'c'), '1')
- for x in xrange(int(c)):
- self.writeout(' ')
-
-
-
- def s_text_span(self, tag, attrs):
- self.writedata()
- c = attrs.get((TEXTNS, 'style-name'), None)
- htmlattrs = { }
- if c:
- c = c.replace('.', '_')
- special = special_styles.get('S-' + c)
- if special is None and self.generate_css:
- htmlattrs['class'] = 'S-%s' % c
-
-
- self.opentag('span', htmlattrs)
- self.purgedata()
-
-
- def e_text_span(self, tag, attrs):
- self.writedata()
- self.closetag('span', False)
- self.purgedata()
-
-
- def s_text_tab(self, tag, attrs):
- self.writedata()
- self.writeout(' ')
- self.purgedata()
-
-
- def s_text_x_source(self, tag, attrs):
- self.writedata()
- self.purgedata()
- self.s_ignorexml(tag, attrs)
-
-
- def e_text_x_source(self, tag, attrs):
- self.writedata()
- self.purgedata()
-
-
- def load(self, odffile):
- self._odffile = odffile
-
-
- def parseodf(self):
- self.xmlfile = ''
- self.title = ''
- self.data = []
- self.tagstack = TagStack()
- self.pstack = []
- self.processelem = True
- self.processcont = True
- self.listtypes = { }
- self.headinglevels = [
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0]
- self.cs = StyleToCSS()
- self.anchors = { }
- self.stylestack = []
- self.styledict = { }
- self.currentstyle = None
- self.notedict = { }
- self.currentnote = 0
- self.notebody = ''
- self.metatags = []
- z = zipfile.ZipFile(self._odffile)
- parser = expatreader.create_parser()
- parser.setFeature(handler.feature_namespaces, 1)
- parser.setContentHandler(self)
- parser.setErrorHandler(handler.ErrorHandler())
- inpsrc = InputSource()
- for xmlfile in ('meta.xml', 'styles.xml', 'content.xml'):
- self.xmlfile = xmlfile
- content = z.read(xmlfile)
- inpsrc.setByteStream(StringIO(content))
- parser.parse(inpsrc)
-
- z.close()
-
-
- def odf2xhtml(self, odffile):
- self.load(odffile)
- return self.xhtml()
-
-
- def _wlines(self, s):
- if s != '':
- self.lines.append(s)
-
-
-
- def xhtml(self):
- self.lines = []
- self._wfunc = self._wlines
- self.parseodf()
- return ''.join(self.lines)
-
-
- def _writecss(self, s):
- if s != '':
- self._csslines.append(s)
-
-
-
- def _writenothing(self, s):
- pass
-
-
- def css(self):
- self._wfunc = self._writenothing
- self.parseodf()
- self._csslines = []
- self._wfunc = self._writecss
- self.generate_stylesheet()
- res = ''.join(self._csslines)
- del self._csslines
- return res
-
-
-