diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-14 20:46:13 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-14 20:46:13 +0000 |
| commit | 2c57c1014ae8e44e62021ea2cc36c54352389632 (patch) | |
| tree | 323783d8b772535dddfd54e1342fce8fe4e4d636 /mammoth/html_paths.py | |
| parent | b34f7280c0754f9fc88725358e22ac46d9a2a598 (diff) | |
Implement collapsing using HTML AST
Diffstat (limited to 'mammoth/html_paths.py')
| -rw-r--r-- | mammoth/html_paths.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/mammoth/html_paths.py b/mammoth/html_paths.py index 1428015..e043e23 100644 --- a/mammoth/html_paths.py +++ b/mammoth/html_paths.py @@ -1,4 +1,6 @@ -import collections +import cobble + +from . import html def path(elements): @@ -13,7 +15,32 @@ def element(names, class_names=None, fresh=None): return HtmlPathElement(names, class_names, fresh) -HtmlPath = collections.namedtuple("HtmlPath", ["elements"]) -HtmlPathElement = collections.namedtuple("HtmlPathElement", ["names", "class_names", "fresh"]) +@cobble.data +class HtmlPath(object): + elements = cobble.field() + + def wrap(self, nodes): + for element in reversed(self.elements): + nodes = element.wrap(nodes) + + return nodes + +@cobble.data +class HtmlPathElement(object): + names = cobble.field() + class_names = cobble.field() + fresh = cobble.field() + + def wrap(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) + return [element] empty = path([]) |
