diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-12-10 22:01:20 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-12-10 22:01:20 +0000 |
| commit | 8661cad2a82b66254fff564f4a514cbd67ad371d (patch) | |
| tree | 83cf43f5127b7e93575f3fd40ddeb8a1f41e7ee2 /mammoth | |
| parent | b870478788c35f9aa7902977938f253197b6d6d8 (diff) | |
Remove self-closing elements from AST
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/conversion.py | 2 | ||||
| -rw-r--r-- | mammoth/html/__init__.py | 29 | ||||
| -rw-r--r-- | mammoth/html/nodes.py | 7 | ||||
| -rw-r--r-- | mammoth/images.py | 2 |
4 files changed, 13 insertions, 27 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index bbb4f86..9398c5c 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -179,7 +179,7 @@ class _DocumentConverter(documents.ElementVisitor): def visit_line_break(self, line_break): - return [html.self_closing_element("br")] + return [html.element("br")] def visit_note_reference(self, note_reference): self._note_references.append(note_reference) diff --git a/mammoth/html/__init__.py b/mammoth/html/__init__.py index 6e97ead..b397a81 100644 --- a/mammoth/html/__init__.py +++ b/mammoth/html/__init__.py @@ -1,5 +1,5 @@ from ..lists import flat_map -from .nodes import TextNode, Tag, Element, SelfClosingElement, ForceWrite, NodeVisitor +from .nodes import TextNode, Tag, Element, ForceWrite, NodeVisitor def text(value): @@ -24,13 +24,6 @@ def element(tag_names, attributes=None, children=None, collapsible=None, separat def collapsible_element(tag_names, attributes=None, children=None): return element(tag_names, attributes, children, collapsible=True) - - -def self_closing_element(tag_name, attributes=None): - if attributes is None: - attributes = {} - - return SelfClosingElement(tag_name, attributes) force_write = ForceWrite() @@ -53,14 +46,11 @@ class StripEmpty(NodeVisitor): def visit_element(self, element): children = strip_empty(element.children) - if len(children) == 0: + if len(children) == 0 and not element.is_void(): return [] else: return [Element(element.tag, children)] - def visit_self_closing_element(self, element): - return [element] - def visit_force_write(self, node): return [node] @@ -80,9 +70,6 @@ class _CollapseNode(NodeVisitor): def visit_element(self, element): return Element(element.tag, collapse(element.children)) - def visit_self_closing_element(self, element): - return element - def visit_force_write(self, node): return node @@ -133,12 +120,12 @@ class _NodeWriter(NodeVisitor): self._writer.text(node.value) def visit_element(self, element): - self._writer.start(element.tag_name, element.attributes) - self.visit_all(element.children) - self._writer.end(element.tag_name) - - def visit_self_closing_element(self, element): - self._writer.self_closing(element.tag_name, element.attributes) + if element.is_void(): + self._writer.self_closing(element.tag_name, element.attributes) + else: + self._writer.start(element.tag_name, element.attributes) + self.visit_all(element.children) + self._writer.end(element.tag_name) def visit_force_write(self, element): pass diff --git a/mammoth/html/nodes.py b/mammoth/html/nodes.py index ff31c8c..9c07259 100644 --- a/mammoth/html/nodes.py +++ b/mammoth/html/nodes.py @@ -47,11 +47,10 @@ class Element(Node): def separator(self): return self.tag.separator + _VOID_TAG_NAMES = set(["br", "hr", "img"]) -@cobble.data -class SelfClosingElement(Node): - tag_name = cobble.field() - attributes = cobble.field() + def is_void(self): + return not self.children and self.tag_name in self._VOID_TAG_NAMES @cobble.visitable diff --git a/mammoth/images.py b/mammoth/images.py index 4dc095a..297b0a2 100644 --- a/mammoth/images.py +++ b/mammoth/images.py @@ -7,7 +7,7 @@ def img_element(func): if image.alt_text: attributes["alt"] = image.alt_text - return [html.self_closing_element("img", attributes)] + return [html.element("img", attributes)] return convert_image |
