diff options
| -rw-r--r-- | mammoth/styles/parser/style_mapping_parser.py | 15 | ||||
| -rw-r--r-- | mammoth/styles/parser/token_iterator.py | 4 | ||||
| -rw-r--r-- | mammoth/styles/parser/tokeniser.py | 3 | ||||
| -rw-r--r-- | tests/styles/parser/style_mapping_parser_tests.py | 18 |
4 files changed, 37 insertions, 3 deletions
diff --git a/mammoth/styles/parser/style_mapping_parser.py b/mammoth/styles/parser/style_mapping_parser.py new file mode 100644 index 0000000..8c84496 --- /dev/null +++ b/mammoth/styles/parser/style_mapping_parser.py @@ -0,0 +1,15 @@ +from .tokeniser import TokenType +from .document_matcher_parser import parse_document_matcher +from .html_path_parser import parse_html_path +from ...styles import Style + + +def parse_style_mapping(tokens): + document_matcher = parse_document_matcher(tokens) + tokens.skip(TokenType.WHITESPACE) + tokens.skip(TokenType.SYMBOL, "=>") + tokens.try_skip(TokenType.WHITESPACE) + html_path = parse_html_path(tokens) + tokens.skip(TokenType.END) + + return Style(document_matcher, html_path) diff --git a/mammoth/styles/parser/token_iterator.py b/mammoth/styles/parser/token_iterator.py index c6876e9..114ce28 100644 --- a/mammoth/styles/parser/token_iterator.py +++ b/mammoth/styles/parser/token_iterator.py @@ -27,9 +27,9 @@ class TokenIterator(object): else: raise self._unexpected_token_type(token_type, token) - def try_skip(self, token_type, token_value): + def try_skip(self, token_type, token_value=None): token = self._tokens[self._index] - if token.type == token_type and token.value == token_value: + if token.type == token_type and (token_value is None or token.value == token_value): self._index += 1 return True else: diff --git a/mammoth/styles/parser/tokeniser.py b/mammoth/styles/parser/tokeniser.py index c9eb4cf..7366cfe 100644 --- a/mammoth/styles/parser/tokeniser.py +++ b/mammoth/styles/parser/tokeniser.py @@ -12,6 +12,7 @@ class TokenType(object): STRING = "string" UNTERMINATED_STRING = "unterminated string" INTEGER = "integer" + END = "end" @@ -33,7 +34,7 @@ def regex_tokeniser(rules): # Should be impossible raise Exception("Remaining: " + value[index:]) - tokens.append(Token(index, "end", "")) + tokens.append(Token(index, TokenType.END, "")) return tokens diff --git a/tests/styles/parser/style_mapping_parser_tests.py b/tests/styles/parser/style_mapping_parser_tests.py new file mode 100644 index 0000000..7b5b299 --- /dev/null +++ b/tests/styles/parser/style_mapping_parser_tests.py @@ -0,0 +1,18 @@ +from nose.tools import istest, assert_equal + +from mammoth import html_paths, document_matchers, styles +from mammoth.styles.parser.style_mapping_parser import parse_style_mapping +from mammoth.styles.parser.tokeniser import tokenise +from mammoth.styles.parser.token_iterator import TokenIterator + + +@istest +def document_matcher_is_mapped_to_html_path_using_fat_arrow(): + assert_equal( + styles.style(document_matchers.paragraph(), html_paths.path([html_paths.element(["h1"])])), + read_style_mapping("p => h1") + ) + + +def read_style_mapping(string): + return parse_style_mapping(TokenIterator(tokenise(string))) |
