From 7a7d1a88427af1e87688ff6dd8637af9c7c1681a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 29 Nov 2016 22:10:12 +0000 Subject: Add parsing for separators --- mammoth/html_paths.py | 5 +++-- mammoth/styles/parser/html_path_parser.py | 18 +++++++++++++++++- tests/styles/parser/html_path_parser_tests.py | 8 ++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/mammoth/html_paths.py b/mammoth/html_paths.py index e201fef..e4f6c31 100644 --- a/mammoth/html_paths.py +++ b/mammoth/html_paths.py @@ -7,12 +7,12 @@ def path(elements): return HtmlPath(elements) -def element(names, class_names=None, fresh=None): +def element(names, class_names=None, fresh=None, separator=None): if class_names is None: class_names = [] if fresh is None: fresh = False - return HtmlPathElement(names, class_names, fresh) + return HtmlPathElement(names, class_names, fresh, separator=separator) @cobble.data @@ -32,6 +32,7 @@ class HtmlPathElement(object): names = cobble.field() class_names = cobble.field() fresh = cobble.field() + separator = cobble.field() def wrap(self, generate_nodes): return self.wrap_nodes(generate_nodes()) diff --git a/mammoth/styles/parser/html_path_parser.py b/mammoth/styles/parser/html_path_parser.py index e8f448e..9e155b6 100644 --- a/mammoth/styles/parser/html_path_parser.py +++ b/mammoth/styles/parser/html_path_parser.py @@ -1,6 +1,6 @@ from ... import html_paths from .tokeniser import TokenType -from .token_parser import parse_identifier, try_parse_class_name +from .token_parser import parse_identifier, parse_string, try_parse_class_name def parse_html_path(tokens): @@ -27,11 +27,13 @@ def _parse_element(tokens): tag_names = _parse_tag_names(tokens) class_names = _parse_class_names(tokens) is_fresh = _parse_is_fresh(tokens) + separator = _parse_separator(tokens) return html_paths.element( tag_names, class_names=class_names, fresh=is_fresh, + separator=separator, ) @@ -62,3 +64,17 @@ def _parse_is_fresh(tokens): (TokenType.SYMBOL, ":"), (TokenType.IDENTIFIER, "fresh"), )) + + +def _parse_separator(tokens): + is_separator = tokens.try_skip_many(( + (TokenType.SYMBOL, ":"), + (TokenType.IDENTIFIER, "separator"), + )) + if is_separator: + tokens.skip(TokenType.SYMBOL, "(") + value = parse_string(tokens) + tokens.skip(TokenType.SYMBOL, ")") + return value + else: + return None diff --git a/tests/styles/parser/html_path_parser_tests.py b/tests/styles/parser/html_path_parser_tests.py index feaa343..1e34ea1 100644 --- a/tests/styles/parser/html_path_parser_tests.py +++ b/tests/styles/parser/html_path_parser_tests.py @@ -69,6 +69,14 @@ def can_read_when_element_must_be_fresh(): ) +@istest +def can_read_separator_for_elements(): + assert_equal( + html_paths.path([html_paths.element(["p"], separator="x")]), + read_html_path("p:separator('x')") + ) + + @istest def can_read_ignore_element(): assert_equal( -- cgit v1.2.3