diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 19:54:06 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 19:54:06 -0500 |
commit | 5633f2dec75c5518da7082695877e76863c08ad5 (patch) | |
tree | cebfbdfb6d35ac79a00a6dc2135503672d7df51b | |
parent | fd36788f96b9a102fd56dd537e36329bc96458fc (diff) |
Finished converting the structure to docbook.
-rwxr-xr-x | ooo-help-parser.py | 19 | ||||
-rw-r--r-- | source/docbook.py | 39 | ||||
-rw-r--r-- | source/node.py | 12 | ||||
-rw-r--r-- | source/tree.py | 11 |
4 files changed, 64 insertions, 17 deletions
diff --git a/ooo-help-parser.py b/ooo-help-parser.py index 0a297b6..bba303e 100755 --- a/ooo-help-parser.py +++ b/ooo-help-parser.py @@ -77,6 +77,8 @@ def main (): parser.set_defaults(output=None) parser.add_option("-o", "--output", dest="output", help="write output to FILE", metavar="FILE") parser.add_option("-t", "--tree-dir", dest="tree_dir", help="Directory where the tree files are located. Tree files are expected to have .tree extension.") + parser.add_option("--debug-tree", action="store_true", dest="debug_tree", + help="Output the tree structure for debugging purposes.", default=False) parser.add_option("--no-convert", action="store_false", dest="convert", help="Don't convert to docbook but simply output the parsed raw xhp structure", default=True) options, args = parser.parse_args() @@ -89,12 +91,6 @@ def main (): parser.print_help() sys.exit(1) - # Process the tree files first - treeroot = processTreeFiles(options.tree_dir) - - # walk all directories and collect all files. - filepaths = walkDirs(args) - fd = sys.stdout if options.output != None: if os.path.isdir(options.output): @@ -102,6 +98,17 @@ def main (): sys.exit(1) fd = open(options.output, 'w') + # Process the tree files first + treeroot = processTreeFiles(options.tree_dir) + if options.debug_tree: + node.prettyPrint(fd, treeroot) + fd.close() + sys.exit(0) + + # walk all directories and collect all files. + filepaths = walkDirs(args) + + xhproots, filesParsed = parseAllXHPFiles(filepaths) if options.convert: diff --git a/source/docbook.py b/source/docbook.py index cf2d648..2d69c33 100644 --- a/source/docbook.py +++ b/source/docbook.py @@ -101,14 +101,51 @@ 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: + def __init__ (self, treeroot, xhproots): self.treeroot = treeroot self.xhproots = xhproots self.root = node.Root() def convert (self): - pass + book = self.root.appendElement('book') + bookinfo = book.appendElement('bookinfo') + title = bookinfo.appendElement('title') + title.appendContent("OpenOffice.org Help") + + for section in self.treeroot.getChildNodes(): + self.__walk(section, book) + + def __walk (self, src, dest): + # src element is either 'section' or 'content'. + if src.name == 'section': + title = src.getAttr('title') + elem = appendDocBookElement(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') + elem = appendDocBookElement(dest) + elem.appendElement('title').appendContent(title) + def prettyPrint (self, fd): node.prettyPrint(fd, self.root) diff --git a/source/node.py b/source/node.py index dc37dcb..8dcbb27 100644 --- a/source/node.py +++ b/source/node.py @@ -1,3 +1,4 @@ +import sys class NodeType: Unknown = 0 @@ -78,7 +79,7 @@ class Element(NodeBase): text += child.getContent() return text - def getAttrByName (self, name): + def getAttr (self, name): if not self.attrs.has_key(name): return None return self.attrs[name] @@ -89,7 +90,9 @@ class Element(NodeBase): encodeTable = { '>': 'gt', '<': 'lt', - '&': 'amp' + '&': 'amp', + '"': 'quot', + '\'': 'apos' } def encodeString (sin): @@ -132,9 +135,8 @@ def printNode (fd, node, level): keys = node.attrs.keys() keys.sort() for key in keys: - line += " " + key + '="' + node.attrs[key] + '"' + line += " " + key + '="' + encodeString(node.attrs[key]) + '"' if hasChildren:# and not nodesToSkip.has_key(node.name): - line = encodeString(line) line = "<%s>\n"%line fd.write (indent + line) for child in node.getChildNodes(): @@ -142,12 +144,10 @@ def printNode (fd, node, level): line = "</%s>\n"%node.name fd.write (indent + line) else: - line = encodeString(line) line = "<%s/>\n"%line fd.write (indent + line) elif node.nodeType == NodeType.Content: content = node.content.strip() if len(content) > 0: - content = encodeString(content) fd.write (indent + content + "\n") diff --git a/source/tree.py b/source/tree.py index f39a626..2d91637 100644 --- a/source/tree.py +++ b/source/tree.py @@ -18,7 +18,8 @@ class TreeBuilder: continue # help_section element has a 'title' attribute. - title = helpsection.getAttrByName('title') + title = helpsection.getAttr('title') + title = node.encodeString(title) elem = self.root.appendElement('section') elem.setAttr('title', title) for child in helpsection.getChildNodes(): @@ -32,14 +33,16 @@ class TreeBuilder: 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') + xhppath = src.getAttr('id') title = src.getContent() - elem = dest.appendElement('section') + title = node.encodeString(title) + elem = dest.appendElement('content') 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') + title = src.getAttr('title') + title = node.encodeString(title) elem = dest.appendElement('section') elem.setAttr('title', title) for child in src.getChildNodes(): |