diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 23:14:32 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 23:14:32 -0500 |
commit | 447a8eec9d93aecf25114b4664f50fb5e0d57c17 (patch) | |
tree | edfe8bedda92ec5e944ee62d91854e46f19f7a70 | |
parent | a105d266b66a3ec50f8648b16730427c719956a3 (diff) |
More on converting xhp to docbook.
-rw-r--r-- | source/docbook.py | 81 | ||||
-rw-r--r-- | source/globals.py | 1 |
2 files changed, 59 insertions, 23 deletions
diff --git a/source/docbook.py b/source/docbook.py index 4668726..2d61390 100644 --- a/source/docbook.py +++ b/source/docbook.py @@ -91,33 +91,35 @@ class FilePathSorter: self.__walkToContent(child) -def appendDocBookElement (src): - if src.name == 'book': - return src.appendElement('chapter') - elif src.name == 'chapter': - return src.appendElement('sect1') - elif src.name == 'sect1': - return src.appendElement('sect2') - elif src.name == 'sect2': - return src.appendElement('sect3') - elif src.name == 'sect3': - return src.appendElement('sect4') - else: - return None - - class DocBookConverter: + elemRelTable = { + 'book': 'chapter', + 'chapter': 'sect1', + 'sect1': 'sect2', + 'sect2': 'sect3', + 'sect3': 'sect4', + 'sect4': 'sect5' + } + + @staticmethod + def startSubSection (src): + if DocBookConverter.elemRelTable.has_key(src.name): + return src.appendElement(DocBookConverter.elemRelTable[src.name]) + else: + return None + def __init__ (self, treeroot, xhproots): self.treeroot = treeroot self.xhproots = xhproots self.root = node.Root() + self.headingParent = None def convert (self): book = self.root.appendElement('book') bookinfo = book.appendElement('bookinfo') title = bookinfo.appendElement('title') - title.appendContent("OpenOffice.org Help") + title.appendContent(globals.productName + " Help") for section in self.treeroot.getChildNodes(): self.__walk(section, book) @@ -126,14 +128,14 @@ class DocBookConverter: # src element is either 'section' or 'content'. if src.name == 'section': title = src.getAttr('title') - elem = appendDocBookElement(dest) + elem = DocBookConverter.startSubSection(dest) elem.appendElement('title').appendContent(title) for child in src.getChildNodes(): self.__walk(child, elem) elif src.name == 'content': # this element has 'title' and 'path' attributes, and has no more child nodes. title = src.getAttr('title') - sect = appendDocBookElement(dest) + sect = DocBookConverter.startSubSection(dest) sect.appendElement('title').appendContent(title) xhppath = src.getAttr('path') if self.xhproots.has_key(xhppath): @@ -144,21 +146,54 @@ class DocBookConverter: def __getContentFromXHP (self, xhppath, dest): xhproot = self.xhproots[xhppath] xhpbody = xhproot.firstChild().firstChildByName('body') + # parent element for the xhp 'heading' paragraphs which should start a + # new sub-section when converting to docbook. + self.headingParent = None for xhpelem in xhpbody.getChildNodes(): - self.__walkXHP(xhpelem, dest) + dest = self.__walkXHP(xhpelem, dest) def __walkXHP (self, src, dest): + if src.nodeType == node.NodeType.Content: + # content node. if len(src.content.strip()) > 0: dest.appendElement('warning').appendContent("unhandled content : '%s'"%src.content.strip()) - return - if src.name == 'paragraph': - para = dest.appendElement('para') - para.appendContent(src.getContent()) + elif src.name == 'comment': + # we can ignore these elements. + pass + elif src.name == 'embed': + # embed takes its content from another xhp file. + href = src.getAttr('href') + dest.appendElement('warning').appendContent('take embedded content from ' + href) + + elif src.name == 'paragraph': + # normal paragraph content. + role = src.getAttr('role') + if role != None and role == 'heading': + # finish the current sub-section, adn start a new one. + title = src.getContent() + if self.headingParent == None: + self.headingParent = dest + elem = DocBookConverter.startSubSection(self.headingParent) + elem.appendElement('title').appendContent(title) + return elem + else: + para = dest.appendElement('para') + para.appendContent(src.getContent()) + + elif src.name == 'section': + # we can probably just ignore this element for now. Section elements + # are used only to group multiple elements together. + for child in src.getChildNodes(): + dest = self.__walkXHP(child, dest) + else: + # unhandled element. My work is done when these elements go away. dest.appendElement('warning').appendContent("unhandled element '%s'"%src.name) + return dest + def prettyPrint (self, fd): node.prettyPrint(fd, self.root) diff --git a/source/globals.py b/source/globals.py index 4afb82c..0322bd4 100644 --- a/source/globals.py +++ b/source/globals.py @@ -13,3 +13,4 @@ class Exception: # global parameters productName = 'OpenOffice.org' +#productName = 'Go-OO' |