diff --git a/helpcontent2/to-wiki/wikiconv2.py b/helpcontent2/to-wiki/wikiconv2.py index d419f1ff60..a4615ab06b 100755 --- a/helpcontent2/to-wiki/wikiconv2.py +++ b/helpcontent2/to-wiki/wikiconv2.py @@ -143,17 +143,49 @@ def replace_text(text): text = text.replace(i[0],i[1]) return text -def heading(level): - str="" - for i in range(0,level): - str = str+"=" - return str +# Base class for all the elements +# +# self.name - name of the element, to drop the self.child_parsing flag +# self.objects - collects the child objects that are constructed during +# parsing of the child elements +# self.child_parsing - flag whether we are parsing a child, or the object +# itself +# self.parent - parent object +class ElementBase: + def __init__(self, name, parent): + self.name = name + self.objects = [] + self.child_parsing = False + self.parent = parent -class cxml: + def start_element(self, name, attrs): + pass + + def end_element(self, name): + if name == self.name: + self.parent.child_parsing = False + + def char_data(self, data): + pass + + # construct the wiki representation of this object, including the objects + # held in self.objects (here only the text of the objects) + def get_all(self): + text = '' + for i in self.objects: + text = text + i.get_all() + return text + + def get_curobj(self): + if self.child_parsing: + return self.objects[len(self.objects)-1].get_curobj() + return self + +class cxml(ElementBase): def __init__(self, sectionid): + ElementBase.__init__(self, None, None) + self.filter_section=sectionid - self.objects=[] - self.child_parsing=False self.parser_state=True self.depth=1 if sectionid != "": @@ -212,26 +244,12 @@ class cxml: if self.filter_section != "" and name == 'section': self.parser_state=False - def char_data(self, data): - pass - - def get_curobj(self): - if self.child_parsing: - return self.objects[len(self.objects)-1].get_curobj() - return self - - def get_all(self): - text = "" - for i in self.objects: - text = text + i.get_all() - return text - -class cbookmark: +class cbookmark(ElementBase): bookmarks_list = [] current_bookmark = "" def __init__(self, attrs, parent): - self.parent = parent + ElementBase.__init__(self, 'bookmark', parent) self.bookmark = "" text = attrs['branch'].encode('ascii','replace') # text = text.upper() @@ -240,24 +258,10 @@ class cbookmark: self.bookmark = text[text.rfind("/")+1:].replace(":","_") break - def start_element(self, name, attrs): - pass - - def end_element(self, name): - # Assumes no nested bookmark entries - if name == "bookmark": - self.parent.child_parsing = False - - def char_data(self, data): - pass - def get_all(self): cbookmark.current_bookmark = self.bookmark return "" - def get_curobj(self): - return self - @staticmethod def set_heading(data): global help_file_name @@ -280,8 +284,9 @@ class cbookmark: file.write(i.encode('ascii','replace')+"\n") file.close() -class cimage: +class cimage(ElementBase): def __init__(self, attrs, parent): + ElementBase.__init__(self, 'image', parent) self.src = attrs['src'] try: self.width = attrs['width'] @@ -291,19 +296,17 @@ class cimage: self.align = 'left' self.alt = False self.alttext = "" - self.parent = parent def start_element(self, name, attrs): if name == 'alt': self.alt = True def end_element(self, name): + ElementBase.end_element(self, name) + if name == 'alt': self.alt = False - if name == 'image': - self.parent.child_parsing = False - def char_data(self, data): if self.alt: self.alttext = self.alttext + data @@ -325,12 +328,9 @@ class ctext: def get_all(self): return self.wikitext -class ctabcell: +class ctabcell(ElementBase): def __init__(self, attrs, parent): - # TODO: colspan rowspan - self.objects = [] - self.child_parsing = False - self.parent = parent + ElementBase.__init__(self, 'tablecell', parent) def start_element(self, name, attrs): if name == 'paragraph': @@ -338,29 +338,9 @@ class ctabcell: self.child_parsing = True self.objects.append(para) - def end_element(self, name): - if name == 'tablecell': - self.parent.child_parsing = False - - def char_data(self, data): - return - - def get_all(self): - text = "" - for i in self.objects: - text = text + i.get_all() - return text - - def get_curobj(self): - if self.child_parsing: - return self.objects[len(self.objects)-1].get_curobj() - return self - -class ctabrow: +class ctabrow(ElementBase): def __init__(self, attrs, parent): - self.objects = [] - self.child_parsing = False - self.parent = parent + ElementBase.__init__(self, 'tablerow', parent) def start_element(self, name, attrs): if name == 'tablecell': @@ -368,29 +348,13 @@ class ctabrow: self.child_parsing = True self.objects.append(tabcell) - def end_element(self, name): - if name == 'tablerow': - self.parent.child_parsing = False - - def char_data(self, data): - return - def get_all(self): - text = '|-\n' - for i in self.objects: - text = text + i.get_all() + text = '|-\n' + ElementBase.get_all(self) return text - def get_curobj(self): - if self.child_parsing: - return self.objects[len(self.objects)-1].get_curobj() - return self - -class ctable: +class ctable(ElementBase): def __init__(self, attrs, parent): - self.objects = [] - self.child_parsing = False - self.parent = parent + ElementBase.__init__(self, 'table', parent) def start_element(self, name, attrs): if name == 'tablerow': @@ -398,31 +362,16 @@ class ctable: self.child_parsing = True self.objects.append(tabrow) - def end_element(self, name): - if name == 'table': - self.parent.child_parsing = False - - def char_data(self, data): - return - def get_all(self): - text = '{| border="1"\n' # + ' align="left"' - for i in self.objects: - text = text + i.get_all() - text = text + '|}\n\n' + # + ' align="left"' etc.? + text = '{| border="1"\n' + \ + ElementBase.get_all(self) + \ + '|}\n\n' return text - def get_curobj(self): - if self.child_parsing: - return self.objects[len(self.objects)-1].get_curobj() - return self - -class clistitem: +class clistitem(ElementBase): def __init__(self, attrs, parent): - # TODO: colspan rowspan - self.objects = [] - self.child_parsing = False - self.parent = parent + ElementBase.__init__(self, 'listitem', parent) def start_element(self, name, attrs): if name == 'paragraph': @@ -430,13 +379,6 @@ class clistitem: self.child_parsing = True self.objects.append(para) - def end_element(self, name): - if name == 'listitem': - self.parent.child_parsing = False - - def char_data(self, data): - return - def get_all(self): text = "" prefix = '*' @@ -449,20 +391,14 @@ class clistitem: # add the text itself for i in self.objects: - text = text + prefix + i.get_all() + postfix + text = text + prefix + ElementBase.get_all(self) return text - def get_curobj(self): - if self.child_parsing: - return self.objects[len(self.objects)-1].get_curobj() - return self - -class clist: +class clist(ElementBase): def __init__(self, attrs, parent): - self.objects = [] - self.child_parsing = False - self.parent = parent + ElementBase.__init__(self, 'list', parent) + self.type = attrs['type'] try: self.startwith = int(attrs['startwith']) @@ -475,20 +411,12 @@ class clist: self.child_parsing = True self.objects.append(listitem) - def end_element(self, name): - if name == 'list': - self.parent.child_parsing = False - - def char_data(self, data): - return - def get_all(self): text = "" if self.startwith > 0: text = text + '