diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 18:51:19 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 18:51:19 -0500 |
commit | 0ab5dd74d986ec69db3d6ea8ccea1908601fd338 (patch) | |
tree | 75db2b978e45989b807320929adbae1662dac9e7 | |
parent | 2e2fd65997b0029571905f07ecd9ad7c77ca10ca (diff) |
Finished parsing the 'tree' xml files.
The '.tree' files define the structure of the help contents.
-rwxr-xr-x | ooo-help-parser.py | 9 | ||||
-rw-r--r-- | source/node.py | 11 | ||||
-rw-r--r-- | source/tree.py | 49 |
3 files changed, 66 insertions, 3 deletions
diff --git a/ooo-help-parser.py b/ooo-help-parser.py index 165b511..0525156 100755 --- a/ooo-help-parser.py +++ b/ooo-help-parser.py @@ -3,7 +3,7 @@ import sys, os, os.path, optparse sys.path.append(sys.path[0]+"/source") -import globals, expatimpl, docbook, node +import globals, expatimpl, docbook, node, tree def processTreeFiles (tree_dir): if not os.path.isdir(tree_dir): @@ -13,6 +13,7 @@ def processTreeFiles (tree_dir): if tree_dir[-1] == '/': tree_dir = tree_dir[:-1] + # Parse the tree files to build dom structures. rootNodes = {} for entry in os.listdir(tree_dir): entry = tree_dir + '/' + entry @@ -31,7 +32,11 @@ def processTreeFiles (tree_dir): p = expatimpl.TreeParser(strm) p.parse() rootNodes[basename] = p.root - node.prettyPrint(sys.stdout, p.root) + + # Build document tree. + builder = tree.TreeBuilder(rootNodes) + builder.build() + def main (): parser = optparse.OptionParser() diff --git a/source/node.py b/source/node.py index 0296993..dc37dcb 100644 --- a/source/node.py +++ b/source/node.py @@ -57,10 +57,12 @@ class Content(NodeBase): self.content = content class Element(NodeBase): - def __init__ (self, name, attrs={}): + def __init__ (self, name, attrs=None): NodeBase.__init__(self, NodeType.Element) self.name = name self.attrs = attrs + if self.attrs == None: + self.attrs = {} def getContent (self): text = '' @@ -76,6 +78,13 @@ class Element(NodeBase): text += child.getContent() return text + def getAttrByName (self, name): + if not self.attrs.has_key(name): + return None + return self.attrs[name] + + def setAttr (self, name, val): + self.attrs[name] = val encodeTable = { '>': 'gt', diff --git a/source/tree.py b/source/tree.py new file mode 100644 index 0000000..b859e6e --- /dev/null +++ b/source/tree.py @@ -0,0 +1,49 @@ + +import sys +import globals, node + +class TreeBuilder: + def __init__ (self, treeroots): + self.treeroots = treeroots + self.root = node.Root() + + def build (self): + + for key in self.treeroots.keys(): + treeroot = self.treeroots[key] + treeview = treeroot.firstChild() + # tree_view node has one or more help_section elements. + for helpsection in treeview.getChildNodes(): + if helpsection.nodeType != node.NodeType.Element or helpsection.name != 'help_section': + continue + + # help_section element has a 'title' attribute. + title = helpsection.getAttrByName('title') + elem = self.root.appendElement('section') + elem.setAttr('title', title) + for child in helpsection.getChildNodes(): + self.__walkNode(child, elem) + + node.prettyPrint(sys.stdout, self.root) + + def __walkNode (self, src, dest): + if src.nodeType != node.NodeType.Element: + return + + # 'topic' is a leaf node while 'node' is a non-leaf node. + if src.name == 'topic': + # the 'id' attribute contains the corresponding xhp file path, while + # the content of this element is the title of this section. + xhppath = src.getAttrByName('id') + title = src.getContent() + elem = dest.appendElement('section') + elem.setAttr('title', title) + elem.setAttr('path', xhppath) + elif src.name == 'node': + # A 'node' element is just a branch point; it doesn't have any content. + title = src.getAttrByName('title') + elem = dest.appendElement('section') + elem.setAttr('title', title) + for child in src.getChildNodes(): + self.__walkNode(child, elem) + |