diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-21 01:51:44 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-21 01:51:44 -0500 |
commit | 7fe4aea75e8d7174b710713ba53f8fa8744a0c3c (patch) | |
tree | 8fc9bd4ce50e38b04753688a61781ef4075e132a /source/node.py | |
parent | 6a834d696d2eefe684593c29be6cdc4d4ac48a1f (diff) |
More on converting the parsed xhp contents to docbook.
Diffstat (limited to 'source/node.py')
-rw-r--r-- | source/node.py | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/source/node.py b/source/node.py index d956451..dcda870 100644 --- a/source/node.py +++ b/source/node.py @@ -2,7 +2,7 @@ class NodeType: Unknown = 0 Root = 1 - Node = 2 + Element = 2 Content = 3 class NodeBase: @@ -13,12 +13,32 @@ class NodeBase: def appendChild (self, node): self.children.append(node) - def setContent (self, content): - self.content = content + def appendElement (self, name): + node = Element(name) + self.appendChild(node) + return node + + def appendContent (self, text): + node = Content(text) + self.appendChild(node) + return node def firstChild (self): return self.children[0] + def firstChildByName (self, name): + for child in self.children: + if child.nodeType == NodeType.Element and child.name == name: + return child + return None + + def getChildByName (self, name): + children = [] + for child in self.children: + if child.nodeType == NodeType.Element and child.name == name: + children.append(child) + return children + class Root(NodeBase): def __init__ (self): NodeBase.__init__(self, NodeType.Root) @@ -28,12 +48,26 @@ class Content(NodeBase): NodeBase.__init__(self, NodeType.Content) self.content = content -class Node(NodeBase): +class Element(NodeBase): def __init__ (self, name, attrs={}): - NodeBase.__init__(self, NodeType.Node) + NodeBase.__init__(self, NodeType.Element) self.name = name self.attrs = attrs + def getContent (self): + text = '' + first = True + for child in self.children: + if first: + first = False + else: + text += ' ' + if child.nodeType == NodeType.Content: + text += child.content + elif child.nodeType == NodeType.Element: + text += child.getContent() + return text + encodeTable = { '>': 'gt', @@ -71,7 +105,7 @@ def printNode (fd, node, level): # root node itself only contains child nodes. for child in node.children: printNode(fd, child, level) - elif node.nodeType == NodeType.Node: + elif node.nodeType == NodeType.Element: hasChildren = len(node.children) > 0 # We add '<' and '>' (or '/>') after the element content gets |