diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-29 22:23:49 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-29 22:23:49 +0000 |
| commit | f49ef5f80a0d20bebd5155f7d8f99b5e6366e35f (patch) | |
| tree | 68c18f3ec1f84e31d5ebeab596c3edce6030ff34 /mammoth/html | |
| parent | 2f8bd9edc77b8508203e221ad96c79f516205d92 (diff) | |
Introduce tag object for HTML elements
Diffstat (limited to 'mammoth/html')
| -rw-r--r-- | mammoth/html/__init__.py | 30 | ||||
| -rw-r--r-- | mammoth/html/nodes.py | 29 |
2 files changed, 38 insertions, 21 deletions
diff --git a/mammoth/html/__init__.py b/mammoth/html/__init__.py index cf84043..6e97ead 100644 --- a/mammoth/html/__init__.py +++ b/mammoth/html/__init__.py @@ -1,19 +1,25 @@ from ..lists import flat_map -from .nodes import TextNode, Element, SelfClosingElement, ForceWrite, NodeVisitor +from .nodes import TextNode, Tag, Element, SelfClosingElement, ForceWrite, NodeVisitor def text(value): return TextNode(value) -def element(tag_names, attributes=None, children=None, collapsible=False, separator=None): +def tag(tag_names, attributes=None, collapsible=None, separator=None): if not isinstance(tag_names, list): tag_names = [tag_names] if attributes is None: attributes = {} + return Tag(tag_names=tag_names, attributes=attributes, collapsible=bool(collapsible), separator=separator) + + +def element(tag_names, attributes=None, children=None, collapsible=None, separator=None): if children is None: children = [] - return Element(tag_names, attributes, children, collapsible=collapsible, separator=separator) + + element_tag = tag(tag_names=tag_names, attributes=attributes, collapsible=collapsible, separator=separator) + return Element(element_tag, children) def collapsible_element(tag_names, attributes=None, children=None): @@ -50,15 +56,7 @@ class StripEmpty(NodeVisitor): if len(children) == 0: return [] else: - return [ - Element( - element.tag_names, - element.attributes, - children, - collapsible=element.collapsible, - separator=element.separator, - ) - ] + return [Element(element.tag, children)] def visit_self_closing_element(self, element): return [element] @@ -80,13 +78,7 @@ class _CollapseNode(NodeVisitor): return node def visit_element(self, element): - return Element( - element.tag_names, - element.attributes, - collapse(element.children), - collapsible=element.collapsible, - separator=element.separator, - ) + return Element(element.tag, collapse(element.children)) def visit_self_closing_element(self, element): return element diff --git a/mammoth/html/nodes.py b/mammoth/html/nodes.py index 3b961f2..ff31c8c 100644 --- a/mammoth/html/nodes.py +++ b/mammoth/html/nodes.py @@ -11,10 +11,9 @@ class TextNode(Node): @cobble.data -class Element(Node): +class Tag(object): tag_names = cobble.field() attributes = cobble.field() - children = cobble.field() collapsible = cobble.field() separator = cobble.field() @@ -24,6 +23,32 @@ class Element(Node): @cobble.data +class Element(Node): + tag = cobble.field() + children = cobble.field() + + @property + def tag_name(self): + return self.tag.tag_name + + @property + def tag_names(self): + return self.tag.tag_names + + @property + def attributes(self): + return self.tag.attributes + + @property + def collapsible(self): + return self.tag.collapsible + + @property + def separator(self): + return self.tag.separator + + +@cobble.data class SelfClosingElement(Node): tag_name = cobble.field() attributes = cobble.field() |
