<s> import sys , zipfile , xml . dom . minidom <EOL> from . namespaces import nsdict <EOL> from . elementtypes import * <EOL> IGNORED_TAGS = [ <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] + [ nsdict [ item [ <NUM_LIT:0> ] ] + "<STR_LIT::>" + item [ <NUM_LIT:1> ] for item in empty_elements ] <EOL> INLINE_TAGS = [ nsdict [ item [ <NUM_LIT:0> ] ] + "<STR_LIT::>" + item [ <NUM_LIT:1> ] for item in inline_elements ] <EOL> class TextProps : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . italic = False <EOL> self . bold = False <EOL> self . fixed = False <EOL> self . underlined = False <EOL> self . strikethrough = False <EOL> self . superscript = False <EOL> self . subscript = False <EOL> def setItalic ( self , value ) : <EOL> if value == "<STR_LIT>" : <EOL> self . italic = True <EOL> elif value == "<STR_LIT>" : <EOL> self . italic = False <EOL> def setBold ( self , value ) : <EOL> if value == "<STR_LIT>" : <EOL> self . bold = True <EOL> elif value == "<STR_LIT>" : <EOL> self . bold = False <EOL> def setFixed ( self , value ) : <EOL> self . fixed = value <EOL> def setUnderlined ( self , value ) : <EOL> if value and value != "<STR_LIT:none>" : <EOL> self . underlined = True <EOL> def setStrikethrough ( self , value ) : <EOL> if value and value != "<STR_LIT:none>" : <EOL> self . strikethrough = True <EOL> def setPosition ( self , value ) : <EOL> if value is None or value == '<STR_LIT>' : <EOL> return <EOL> posisize = value . split ( '<STR_LIT:U+0020>' ) <EOL> textpos = posisize [ <NUM_LIT:0> ] <EOL> if textpos . find ( '<STR_LIT:%>' ) == - <NUM_LIT:1> : <EOL> if textpos == "<STR_LIT>" : <EOL> self . superscript = False <EOL> self . subscript = True <EOL> elif textpos == "<STR_LIT>" : <EOL> self . superscript = True <EOL> self . subscript = False <EOL> else : <EOL> itextpos = int ( textpos [ : textpos . find ( '<STR_LIT:%>' ) ] ) <EOL> if itextpos > <NUM_LIT:10> : <EOL> self . superscript = False <EOL> self . subscript = True <EOL> elif itextpos < - <NUM_LIT:10> : <EOL> self . superscript = True <EOL> self . subscript = False <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self . italic ) , <EOL> str ( self . bold ) , <EOL> str ( self . fixed ) ) <EOL> class ParagraphProps : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . blockquote = False <EOL> self . headingLevel = <NUM_LIT:0> <EOL> self . code = False <EOL> self . title = False <EOL> self . indented = <NUM_LIT:0> <EOL> def setIndented ( self , value ) : <EOL> self . indented = value <EOL> def setHeading ( self , level ) : <EOL> self . headingLevel = level <EOL> def setTitle ( self , value ) : <EOL> self . title = value <EOL> def setCode ( self , value ) : <EOL> self . code = value <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self . blockquote ) , <EOL> self . headingLevel , <EOL> str ( self . code ) ) <EOL> class ListProperties : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . ordered = False <EOL> def setOrdered ( self , value ) : <EOL> self . ordered = value <EOL> class ODF2MoinMoin ( object ) : <EOL> def __init__ ( self , filepath ) : <EOL> self . footnotes = [ ] <EOL> self . footnoteCounter = <NUM_LIT:0> <EOL> self . textStyles = { "<STR_LIT>" : TextProps ( ) } <EOL> self . paragraphStyles = { "<STR_LIT>" : ParagraphProps ( ) } <EOL> self . listStyles = { } <EOL> self . fixedFonts = [ ] <EOL> self . hasTitle = <NUM_LIT:0> <EOL> self . lastsegment = None <EOL> self . elements = { <EOL> '<STR_LIT>' : self . textToString , <EOL> '<STR_LIT>' : self . textToString , <EOL> '<STR_LIT>' : self . draw_image , <EOL> '<STR_LIT>' : self . textToString , <EOL> '<STR_LIT>' : self . text_a , <EOL> '<STR_LIT>' : self . text_note , <EOL> } <EOL> for tag in IGNORED_TAGS : <EOL> self . elements [ tag ] = self . do_nothing <EOL> for tag in INLINE_TAGS : <EOL> self . elements [ tag ] = self . inline_markup <EOL> self . elements [ '<STR_LIT>' ] = self . text_line_break <EOL> self . elements [ '<STR_LIT>' ] = self . text_s <EOL> self . elements [ '<STR_LIT>' ] = self . text_tab <EOL> self . load ( filepath ) <EOL> def processFontDeclarations ( self , fontDecl ) : <EOL> """<STR_LIT>""" <EOL> for fontFace in fontDecl . getElementsByTagName ( "<STR_LIT>" ) : <EOL> if fontFace . getAttribute ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> self . fixedFonts . append ( fontFace . getAttribute ( "<STR_LIT>" ) ) <EOL> def extractTextProperties ( self , style , parent = None ) : <EOL> """<STR_LIT>""" <EOL> textProps = TextProps ( ) <EOL> if parent : <EOL> parentProp = self . textStyles . get ( parent , None ) <EOL> if parentProp : <EOL> textProp = parentProp <EOL> textPropEl = style . getElementsByTagName ( "<STR_LIT>" ) <EOL> if not textPropEl : return textProps <EOL> textPropEl = textPropEl [ <NUM_LIT:0> ] <EOL> textProps . setItalic ( textPropEl . getAttribute ( "<STR_LIT>" ) ) <EOL> textProps . setBold ( textPropEl . getAttribute ( "<STR_LIT>" ) ) <EOL> textProps . setUnderlined ( textPropEl . getAttribute ( "<STR_LIT>" ) ) <EOL> textProps . setStrikethrough ( textPropEl . getAttribute ( "<STR_LIT>" ) ) <EOL> textProps . setPosition ( textPropEl . getAttribute ( "<STR_LIT>" ) ) <EOL> if textPropEl . getAttribute ( "<STR_LIT>" ) in self . fixedFonts : <EOL> textProps . setFixed ( True ) <EOL> return textProps <EOL> def extractParagraphProperties ( self , style , parent = None ) : <EOL> """<STR_LIT>""" <EOL> paraProps = ParagraphProps ( ) <EOL> name = style . getAttribute ( "<STR_LIT>" ) <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> level = name [ <NUM_LIT:11> : ] <EOL> try : <EOL> level = int ( level ) <EOL> paraProps . setHeading ( level ) <EOL> except : <EOL> level = <NUM_LIT:0> <EOL> if name == "<STR_LIT>" : <EOL> paraProps . setTitle ( True ) <EOL> paraPropEl = style . getElementsByTagName ( "<STR_LIT>" ) <EOL> if paraPropEl : <EOL> paraPropEl = paraPropEl [ <NUM_LIT:0> ] <EOL> leftMargin = paraPropEl . getAttribute ( "<STR_LIT>" ) <EOL> if leftMargin : <EOL> try : <EOL> leftMargin = float ( leftMargin [ : - <NUM_LIT:2> ] ) <EOL> if leftMargin > <NUM_LIT> : <EOL> paraProps . setIndented ( True ) <EOL> except : <EOL> pass <EOL> textProps = self . extractTextProperties ( style ) <EOL> if textProps . fixed : <EOL> paraProps . setCode ( True ) <EOL> return paraProps <EOL> def processStyles ( self , styleElements ) : <EOL> """<STR_LIT>""" <EOL> for style in styleElements : <EOL> name = style . getAttribute ( "<STR_LIT>" ) <EOL> if name == "<STR_LIT>" : continue <EOL> family = style . getAttribute ( "<STR_LIT>" ) <EOL> parent = style . getAttribute ( "<STR_LIT>" ) <EOL> if family == "<STR_LIT:text>" : <EOL> self . textStyles [ name ] = self . extractTextProperties ( style , parent ) <EOL> elif family == "<STR_LIT>" : <EOL> self . paragraphStyles [ name ] = self . extractParagraphProperties ( style , parent ) <EOL> self . textStyles [ name ] = self . extractTextProperties ( style , parent ) <EOL> def processListStyles ( self , listStyleElements ) : <EOL> for style in listStyleElements : <EOL> name = style . getAttribute ( "<STR_LIT>" ) <EOL> prop = ListProperties ( ) <EOL> if style . hasChildNodes ( ) : <EOL> subitems = [ el for el in style . childNodes <EOL> if el . nodeType == xml . dom . Node . ELEMENT_NODE <EOL> and el . tagName == "<STR_LIT>" ] <EOL> if len ( subitems ) > <NUM_LIT:0> : <EOL> prop . setOrdered ( True ) <EOL> self . listStyles [ name ] = prop <EOL> def load ( self , filepath ) : <EOL> """<STR_LIT>""" <EOL> zip = zipfile . ZipFile ( filepath ) <EOL> styles_doc = xml . dom . minidom . parseString ( zip . read ( "<STR_LIT>" ) ) <EOL> fontfacedecls = styles_doc . getElementsByTagName ( "<STR_LIT>" ) <EOL> if fontfacedecls : <EOL> self . processFontDeclarations ( fontfacedecls [ <NUM_LIT:0> ] ) <EOL> self . processStyles ( styles_doc . getElementsByTagName ( "<STR_LIT>" ) ) <EOL> self . processListStyles ( styles_doc . getElementsByTagName ( "<STR_LIT>" ) ) <EOL> self . content = xml . dom . minidom . parseString ( zip . read ( "<STR_LIT>" ) ) <EOL> fontfacedecls = self . content . getElementsByTagName ( "<STR_LIT>" ) <EOL> if fontfacedecls : <EOL> self . processFontDeclarations ( fontfacedecls [ <NUM_LIT:0> ] ) <EOL> self . processStyles ( self . content . getElementsByTagName ( "<STR_LIT>" ) ) <EOL> self . processListStyles ( self . content . getElementsByTagName ( "<STR_LIT>" ) ) <EOL> def compressCodeBlocks ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return text <EOL> lines = text . split ( "<STR_LIT:\n>" ) <EOL> buffer = [ ] <EOL> numLines = len ( lines ) <EOL> for i in range ( numLines ) : <EOL> if ( lines [ i ] . strip ( ) or i == numLines - <NUM_LIT:1> or i == <NUM_LIT:0> or <EOL> not ( lines [ i - <NUM_LIT:1> ] . startswith ( "<STR_LIT:U+0020>" ) <EOL> and lines [ i + <NUM_LIT:1> ] . startswith ( "<STR_LIT:U+0020>" ) ) ) : <EOL> buffer . append ( "<STR_LIT:\n>" + lines [ i ] ) <EOL> return '<STR_LIT>' . join ( buffer ) <EOL> def do_nothing ( self , node ) : <EOL> return '<STR_LIT>' <EOL> def draw_image ( self , node ) : <EOL> """<STR_LIT:U+0020>""" <EOL> link = node . getAttribute ( "<STR_LIT>" ) <EOL> if link and link [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> return "<STR_LIT>" % link <EOL> if link and link [ : <NUM_LIT:9> ] == '<STR_LIT>' : <EOL> link = link [ <NUM_LIT:9> : ] <EOL> return "<STR_LIT>" % link <EOL> def text_a ( self , node ) : <EOL> text = self . textToString ( node ) <EOL> link = node . getAttribute ( "<STR_LIT>" ) <EOL> if link . strip ( ) == text . strip ( ) : <EOL> return "<STR_LIT>" % link . strip ( ) <EOL> else : <EOL> return "<STR_LIT>" % ( link . strip ( ) , text . strip ( ) ) <EOL> def text_line_break ( self , node ) : <EOL> return "<STR_LIT>" <EOL> def text_note ( self , node ) : <EOL> cite = ( node . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> body = ( node . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> . childNodes [ <NUM_LIT:0> ] ) <EOL> self . footnotes . append ( ( cite , self . textToString ( body ) ) ) <EOL> return "<STR_LIT>" % cite <EOL> def text_s ( self , node ) : <EOL> try : <EOL> num = int ( node . getAttribute ( "<STR_LIT>" ) ) <EOL> return "<STR_LIT:U+0020>" * num <EOL> except : <EOL> return "<STR_LIT:U+0020>" <EOL> def text_tab ( self , node ) : <EOL> return "<STR_LIT:U+0020>" <EOL> def inline_markup ( self , node ) : <EOL> text = self . textToString ( node ) <EOL> if not text . strip ( ) : <EOL> return '<STR_LIT>' <EOL> styleName = node . getAttribute ( "<STR_LIT>" ) <EOL> style = self . textStyles . get ( styleName , TextProps ( ) ) <EOL> if style . fixed : <EOL> return "<STR_LIT>" + text + "<STR_LIT>" <EOL> mark = [ ] <EOL> if style : <EOL> if style . italic : <EOL> mark . append ( "<STR_LIT>" ) <EOL> if style . bold : <EOL> mark . append ( "<STR_LIT>" ) <EOL> if style . underlined : <EOL> mark . append ( "<STR_LIT>" ) <EOL> if style . strikethrough : <EOL> mark . append ( "<STR_LIT>" ) <EOL> if style . superscript : <EOL> mark . append ( "<STR_LIT>" ) <EOL> if style . subscript : <EOL> mark . append ( "<STR_LIT>" ) <EOL> revmark = mark [ : ] <EOL> revmark . reverse ( ) <EOL> return "<STR_LIT>" % ( '<STR_LIT>' . join ( mark ) , text , '<STR_LIT>' . join ( revmark ) ) <EOL> def listToString ( self , listElement , indent = <NUM_LIT:0> ) : <EOL> self . lastsegment = listElement . tagName <EOL> buffer = [ ] <EOL> styleName = listElement . getAttribute ( "<STR_LIT>" ) <EOL> props = self . listStyles . get ( styleName , ListProperties ( ) ) <EOL> i = <NUM_LIT:0> <EOL> for item in listElement . childNodes : <EOL> buffer . append ( "<STR_LIT:U+0020>" * indent ) <EOL> i += <NUM_LIT:1> <EOL> if props . ordered : <EOL> number = str ( i ) <EOL> number = "<STR_LIT:U+0020>" + number + "<STR_LIT>" <EOL> buffer . append ( "<STR_LIT>" ) <EOL> else : <EOL> buffer . append ( "<STR_LIT>" ) <EOL> subitems = [ el for el in item . childNodes <EOL> if el . tagName in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> for subitem in subitems : <EOL> if subitem . tagName == "<STR_LIT>" : <EOL> buffer . append ( "<STR_LIT:\n>" ) <EOL> buffer . append ( self . listToString ( subitem , indent + <NUM_LIT:3> ) ) <EOL> else : <EOL> buffer . append ( self . paragraphToString ( subitem , indent + <NUM_LIT:3> ) ) <EOL> self . lastsegment = subitem . tagName <EOL> self . lastsegment = item . tagName <EOL> buffer . append ( "<STR_LIT:\n>" ) <EOL> return '<STR_LIT>' . join ( buffer ) <EOL> def tableToString ( self , tableElement ) : <EOL> """<STR_LIT>""" <EOL> self . lastsegment = tableElement . tagName <EOL> buffer = [ ] <EOL> for item in tableElement . childNodes : <EOL> self . lastsegment = item . tagName <EOL> if item . tagName == "<STR_LIT>" : <EOL> buffer . append ( self . tableToString ( item ) ) <EOL> if item . tagName == "<STR_LIT>" : <EOL> buffer . append ( "<STR_LIT>" ) <EOL> for cell in item . childNodes : <EOL> buffer . append ( self . inline_markup ( cell ) ) <EOL> buffer . append ( "<STR_LIT>" ) <EOL> self . lastsegment = cell . tagName <EOL> return '<STR_LIT>' . join ( buffer ) <EOL> def toString ( self ) : <EOL> """<STR_LIT>""" <EOL> body = self . content . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> text = body . childNodes [ <NUM_LIT:0> ] <EOL> buffer = [ ] <EOL> paragraphs = [ el for el in text . childNodes <EOL> if el . tagName in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> for paragraph in paragraphs : <EOL> if paragraph . tagName == "<STR_LIT>" : <EOL> text = self . listToString ( paragraph ) <EOL> elif paragraph . tagName == "<STR_LIT>" : <EOL> text = self . textToString ( paragraph ) <EOL> elif paragraph . tagName == "<STR_LIT>" : <EOL> text = self . tableToString ( paragraph ) <EOL> else : <EOL> text = self . paragraphToString ( paragraph ) <EOL> if text : <EOL> buffer . append ( text ) <EOL> if self . footnotes : <EOL> buffer . append ( "<STR_LIT>" ) <EOL> for cite , body in self . footnotes : <EOL> buffer . append ( "<STR_LIT>" % ( cite , body ) ) <EOL> buffer . append ( "<STR_LIT>" ) <EOL> return self . compressCodeBlocks ( '<STR_LIT:\n>' . join ( buffer ) ) <EOL> def textToString ( self , element ) : <EOL> buffer = [ ] <EOL> for node in element . childNodes : <EOL> if node . nodeType == xml . dom . Node . TEXT_NODE : <EOL> buffer . append ( node . nodeValue ) <EOL> elif node . nodeType == xml . dom . Node . ELEMENT_NODE : <EOL> tag = node . tagName <EOL> if tag in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> buffer . append ( self . textToString ( node ) ) <EOL> elif tag in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> text = self . paragraphToString ( node ) <EOL> if text : <EOL> buffer . append ( text ) <EOL> elif tag == "<STR_LIT>" : <EOL> buffer . append ( self . listToString ( node ) ) <EOL> else : <EOL> method = self . elements . get ( tag ) <EOL> if method : <EOL> buffer . append ( method ( node ) ) <EOL> else : <EOL> buffer . append ( "<STR_LIT>" + tag + "<STR_LIT>" ) <EOL> return '<STR_LIT>' . join ( buffer ) <EOL> def paragraphToString ( self , paragraph , indent = <NUM_LIT:0> ) : <EOL> dummyParaProps = ParagraphProps ( ) <EOL> style_name = paragraph . getAttribute ( "<STR_LIT>" ) <EOL> paraProps = self . paragraphStyles . get ( style_name , dummyParaProps ) <EOL> text = self . inline_markup ( paragraph ) <EOL> if paraProps and not paraProps . code : <EOL> text = text . strip ( ) <EOL> if paragraph . tagName == "<STR_LIT>" and self . lastsegment == "<STR_LIT>" : <EOL> text = "<STR_LIT:\n>" + text <EOL> self . lastsegment = paragraph . tagName <EOL> if paraProps . title : <EOL> self . hasTitle = <NUM_LIT:1> <EOL> return "<STR_LIT>" + text + "<STR_LIT>" <EOL> outlinelevel = paragraph . getAttribute ( "<STR_LIT>" ) <EOL> if outlinelevel : <EOL> level = int ( outlinelevel ) <EOL> if self . hasTitle : level += <NUM_LIT:1> <EOL> if level >= <NUM_LIT:1> : <EOL> return "<STR_LIT:=>" * level + "<STR_LIT:U+0020>" + text + "<STR_LIT:U+0020>" + "<STR_LIT:=>" * level + "<STR_LIT:\n>" <EOL> elif paraProps . code : <EOL> return "<STR_LIT>" + text + "<STR_LIT>" <EOL> if paraProps . indented : <EOL> return self . wrapParagraph ( text , indent = indent , blockquote = True ) <EOL> else : <EOL> return self . wrapParagraph ( text , indent = indent ) <EOL> def wrapParagraph ( self , text , indent = <NUM_LIT:0> , blockquote = False ) : <EOL> counter = <NUM_LIT:0> <EOL> buffer = [ ] <EOL> LIMIT = <NUM_LIT:50> <EOL> if blockquote : <EOL> buffer . append ( "<STR_LIT:U+0020>" ) <EOL> return '<STR_LIT>' . join ( buffer ) + text <EOL> for token in text . split ( ) : <EOL> if counter > LIMIT - indent : <EOL> buffer . append ( "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * indent ) <EOL> if blockquote : <EOL> buffer . append ( "<STR_LIT:U+0020>" ) <EOL> counter = <NUM_LIT:0> <EOL> buffer . append ( token + "<STR_LIT:U+0020>" ) <EOL> counter += len ( token ) <EOL> return '<STR_LIT>' . join ( buffer ) </s>
