diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 23:48:18 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 23:48:18 -0500 |
commit | 5427d68bd598aedc336336781d5252904affbde9 (patch) | |
tree | d24debf048ca7cbd27faa2f4467640a943266c53 | |
parent | 447a8eec9d93aecf25114b4664f50fb5e0d57c17 (diff) |
Handle embed elements properly.
-rwxr-xr-x | ooo-help-parser.py | 8 | ||||
-rw-r--r-- | source/docbook.py | 27 | ||||
-rw-r--r-- | source/expatimpl.py | 9 |
3 files changed, 36 insertions, 8 deletions
diff --git a/ooo-help-parser.py b/ooo-help-parser.py index bba303e..31c0a88 100755 --- a/ooo-help-parser.py +++ b/ooo-help-parser.py @@ -56,6 +56,7 @@ def walkDirs (fpaths): def parseAllXHPFiles (filepaths): filesParsed = 0 rootNodes = {} + embedNodes = {} for fpath in filepaths: file = open(fpath, 'r') strm = file.read() @@ -69,8 +70,9 @@ def parseAllXHPFiles (filepaths): # We need to make this consistent. p.filename = p.filename[1:] rootNodes[p.filename] = p.root + embedNodes[p.filename] = p.ids filesParsed += 1 - return rootNodes, filesParsed + return rootNodes, embedNodes, filesParsed def main (): parser = optparse.OptionParser() @@ -109,10 +111,10 @@ def main (): filepaths = walkDirs(args) - xhproots, filesParsed = parseAllXHPFiles(filepaths) + xhproots, embeds, filesParsed = parseAllXHPFiles(filepaths) if options.convert: - converter = docbook.DocBookConverter(treeroot, xhproots) + converter = docbook.DocBookConverter(treeroot, xhproots, embeds) converter.convert() converter.prettyPrint(fd) else: diff --git a/source/docbook.py b/source/docbook.py index 2d61390..e19767b 100644 --- a/source/docbook.py +++ b/source/docbook.py @@ -109,9 +109,10 @@ class DocBookConverter: else: return None - def __init__ (self, treeroot, xhproots): + def __init__ (self, treeroot, xhproots, embeds): self.treeroot = treeroot self.xhproots = xhproots + self.embeds = embeds self.root = node.Root() self.headingParent = None @@ -152,11 +153,20 @@ class DocBookConverter: for xhpelem in xhpbody.getChildNodes(): dest = self.__walkXHP(xhpelem, dest) + def __getEmbedNode (self, href): + filename, id = href.split('#') + if filename.startswith('/'): + filename = filename[1:] + if self.embeds.has_key(filename) and self.embeds[filename].has_key(id): + return self.embeds[filename][id] + return None + def __walkXHP (self, src, dest): if src.nodeType == node.NodeType.Content: # content node. - if len(src.content.strip()) > 0: + content = src.content.strip() + if len(content) > 0: dest.appendElement('warning').appendContent("unhandled content : '%s'"%src.content.strip()) elif src.name == 'comment': @@ -165,7 +175,13 @@ class DocBookConverter: 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) + embed = self.__getEmbedNode(href) + if embed == None: + dest.appendElement('warning').appendContent('take embedded content from ' + href) + else: + dest = self.__walkXHP(embed, dest) + elif src.name == 'link': + dest.appendElement('para').appendContent(src.getContent()) elif src.name == 'paragraph': # normal paragraph content. @@ -182,9 +198,10 @@ class DocBookConverter: para = dest.appendElement('para') para.appendContent(src.getContent()) - elif src.name == 'section': + elif src.name == 'section' or src.name == 'variable': # we can probably just ignore this element for now. Section elements - # are used only to group multiple elements together. + # are used only to group multiple elements together. Same with + # the variable elements. for child in src.getChildNodes(): dest = self.__walkXHP(child, dest) diff --git a/source/expatimpl.py b/source/expatimpl.py index f9865d0..06d5375 100644 --- a/source/expatimpl.py +++ b/source/expatimpl.py @@ -55,6 +55,14 @@ class XHPParser(ParserBase): def __init__ (self, strm): ParserBase.__init__(self, strm) self.filename = None + self.ids = {} + + def startElement(self, name, attrs): + ParserBase.startElement(self, name, attrs) + if attrs.has_key('id'): + # associate this node with this ID. + val = attrs['id'] + self.ids[val] = self.nodestack[-1] def character(self, data): ParserBase.character(self, data) @@ -62,6 +70,7 @@ class XHPParser(ParserBase): # For now, I just assume that the filename element is always at the correct position. self.filename = self.char + class TreeParser(ParserBase): def __init__ (self, strm): |