aboutsummaryrefslogtreecommitdiff
path: root/mammoth/html
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-29 22:05:08 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-29 22:05:08 +0000
commit1b5c951358623ddb97a237efab72479d9dd9c4ca (patch)
treec66c0fbdf9b3a2333de0c2418f4acb86618e1298 /mammoth/html
parent475077337689abeed1db21a832d26dbc2ae88672 (diff)
Add separator html.Element
Diffstat (limited to 'mammoth/html')
-rw-r--r--mammoth/html/__init__.py26
-rw-r--r--mammoth/html/nodes.py1
2 files changed, 19 insertions, 8 deletions
diff --git a/mammoth/html/__init__.py b/mammoth/html/__init__.py
index 889ec68..cf84043 100644
--- a/mammoth/html/__init__.py
+++ b/mammoth/html/__init__.py
@@ -6,14 +6,14 @@ def text(value):
return TextNode(value)
-def element(tag_names, attributes=None, children=None, collapsible=False):
+def element(tag_names, attributes=None, children=None, collapsible=False, separator=None):
if not isinstance(tag_names, list):
tag_names = [tag_names]
if attributes is None:
attributes = {}
if children is None:
children = []
- return Element(tag_names, attributes, children, collapsible=collapsible)
+ return Element(tag_names, attributes, children, collapsible=collapsible, separator=separator)
def collapsible_element(tag_names, attributes=None, children=None):
@@ -50,11 +50,15 @@ class StripEmpty(NodeVisitor):
if len(children) == 0:
return []
else:
- return [Element(
- element.tag_names,
- element.attributes,
- children,
- collapsible=element.collapsible)]
+ return [
+ Element(
+ element.tag_names,
+ element.attributes,
+ children,
+ collapsible=element.collapsible,
+ separator=element.separator,
+ )
+ ]
def visit_self_closing_element(self, element):
return [element]
@@ -80,7 +84,9 @@ class _CollapseNode(NodeVisitor):
element.tag_names,
element.attributes,
collapse(element.children),
- collapsible=element.collapsible)
+ collapsible=element.collapsible,
+ separator=element.separator,
+ )
def visit_self_closing_element(self, element):
return element
@@ -110,8 +116,12 @@ def _try_collapse(collapsed, node):
if not _is_match(last, node):
return False
+ if node.separator:
+ last.children.append(text(node.separator))
+
for child in node.children:
_collapsing_add(last.children, child)
+
return True
def _is_match(first, second):
diff --git a/mammoth/html/nodes.py b/mammoth/html/nodes.py
index a5a5bde..3b961f2 100644
--- a/mammoth/html/nodes.py
+++ b/mammoth/html/nodes.py
@@ -16,6 +16,7 @@ class Element(Node):
attributes = cobble.field()
children = cobble.field()
collapsible = cobble.field()
+ separator = cobble.field()
@property
def tag_name(self):