From f49ef5f80a0d20bebd5155f7d8f99b5e6366e35f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 29 Nov 2016 22:23:49 +0000 Subject: Introduce tag object for HTML elements --- mammoth/html/__init__.py | 30 +++++++++++------------------- mammoth/html/nodes.py | 29 +++++++++++++++++++++++++++-- mammoth/html_paths.py | 29 +++++++++++++---------------- 3 files changed, 51 insertions(+), 37 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() @@ -23,6 +22,32 @@ class Element(Node): return self.tag_names[0] +@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() diff --git a/mammoth/html_paths.py b/mammoth/html_paths.py index 8f79d72..c91813a 100644 --- a/mammoth/html_paths.py +++ b/mammoth/html_paths.py @@ -12,7 +12,16 @@ def element(names, class_names=None, fresh=None, separator=None): class_names = [] if fresh is None: fresh = False - return HtmlPathElement(names, class_names, fresh, separator=separator) + if class_names: + attributes = {"class": " ".join(class_names)} + else: + attributes = {} + return HtmlPathElement(html.tag( + tag_names=names, + attributes=attributes, + collapsible=not fresh, + separator=separator, + )) @cobble.data @@ -27,28 +36,16 @@ class HtmlPath(object): return nodes + @cobble.data class HtmlPathElement(object): - names = cobble.field() - class_names = cobble.field() - fresh = cobble.field() - separator = cobble.field() + tag = cobble.field() def wrap(self, generate_nodes): return self.wrap_nodes(generate_nodes()) def wrap_nodes(self, nodes): - if self.class_names: - attributes = {"class": " ".join(self.class_names)} - else: - attributes = {} - element = html.element( - self.names, - attributes, - nodes, - collapsible=not self.fresh, - separator=self.separator, - ) + element = html.Element(self.tag, nodes) return [element] empty = path([]) -- cgit v1.2.3