diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2010-01-17 00:34:46 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2010-01-17 00:34:46 -0500 |
commit | 7da62bdaaa3cde7fa4729337936feb310ca64826 (patch) | |
tree | 92b98fc57727ff4f9199cdfbbf36770177369ef3 | |
parent | 97a03c4d92b71b222cbe123d8517239043af6e50 (diff) |
No line breaks or indents when the element has one or more content nodes.
-rw-r--r-- | source/docbook.py | 2 | ||||
-rw-r--r-- | source/node.py | 41 |
2 files changed, 29 insertions, 14 deletions
diff --git a/source/docbook.py b/source/docbook.py index 0d2b8be..262262c 100644 --- a/source/docbook.py +++ b/source/docbook.py @@ -184,7 +184,7 @@ class DocBookConverter: if src.nodeType == node.NodeType.Content: # content node. - content = src.content.strip() + content = src.content if len(content) > 0: dest.appendContent(content) diff --git a/source/node.py b/source/node.py index 35158dc..6b1d62e 100644 --- a/source/node.py +++ b/source/node.py @@ -17,9 +17,11 @@ class NodeType: class NodeBase: def __init__ (self, nodeType = NodeType.Unknown): self.parent = None - self.__children = [] self.nodeType = nodeType + self.__children = [] + self.__hasContent = False + def appendChild (self, node): self.__children.append(node) node.parent = self @@ -29,9 +31,13 @@ class NodeBase: self.appendChild(node) return node + def hasContent (self): + return self.__hasContent + def appendContent (self, text): node = Content(text) self.appendChild(node) + self.__hasContent = True return node def firstChild (self): @@ -134,15 +140,19 @@ def convertAttrValue (val): return val def prettyPrint (fd, node): - printNode(fd, node, 0) - -def printNode (fd, node, level): - singleIndent = ' '*4 + printNode(fd, node, 0, True) + +def printNode (fd, node, level, breakLine): + singleIndent = '' + lf = '' + if breakLine: + singleIndent = ' '*4 + lf = "\n" indent = singleIndent*level if node.nodeType == NodeType.Root: # root node itself only contains child nodes. for child in node.getChildNodes(): - printNode(fd, child, level) + printNode(fd, child, level, True) elif node.nodeType == NodeType.Element: hasChildren = len(node.getChildNodes()) > 0 @@ -160,18 +170,23 @@ def printNode (fd, node, level): line += " " + key + '="' + encodeString(val) + '"' if hasChildren: - line = "<%s>\n"%line + breakChildren = breakLine and not node.hasContent() + line = "<%s>"%line + if breakChildren: + line += "\n" fd.write (indent + line) for child in node.getChildNodes(): - printNode(fd, child, level+1) - line = "</%s>\n"%node.name - fd.write (indent + line) + printNode(fd, child, level+1, breakChildren) + line = "</%s>%s"%(node.name, lf) + if breakChildren: + line = indent + line + fd.write (line) else: - line = "<%s/>\n"%line + line = "<%s/>%s"%(line, lf) fd.write (indent + line) elif node.nodeType == NodeType.Content: - content = node.content.strip() + content = node.content content = encodeString(content) if len(content) > 0: - fd.write (indent + content + "\n") + fd.write (indent + content + lf) |