diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-29 21:42:49 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-29 21:42:49 +0000 |
| commit | 85235a9cf69f847c2451eacaf44a242e6433937a (patch) | |
| tree | f87a13301a134dc10f3190ecbb7a6d45e23f89ae | |
| parent | 15984eb246379f80050ee2bd816dc9bb2a1ffc5f (diff) | |
Introduce string matchers for matching style name
| -rw-r--r-- | mammoth/conversion.py | 2 | ||||
| -rw-r--r-- | mammoth/document_matchers.py | 21 | ||||
| -rw-r--r-- | mammoth/styles/parser/document_matcher_parser.py | 2 | ||||
| -rw-r--r-- | tests/styles/parser/document_matcher_parser_tests.py | 4 |
4 files changed, 25 insertions, 4 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 2c2afa6..bbb4f86 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -307,7 +307,7 @@ def _document_matcher_matches(matcher, element, element_type): matcher.style_id == element.style_id ) and ( matcher.style_name is None or - element.style_name is not None and (matcher.style_name.upper() == element.style_name.upper()) + element.style_name is not None and (matcher.style_name.matches(element.style_name)) ) and ( element_type != "paragraph" or matcher.numbering is None or diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index 2efd73f..af1fd47 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -1,5 +1,7 @@ import collections +import cobble + def paragraph(style_id=None, style_name=None, numbering=None): return ParagraphMatcher(style_id, style_name, numbering) @@ -35,3 +37,22 @@ class strikethrough(object): class comment_reference(object): element_type = "comment_reference" + + +def equal_to(value): + return StringMatcher(_operator_equal_to, value) + + +def _operator_equal_to(first, second): + return first.upper() == second.upper() + + +@cobble.data +class StringMatcher(object): + operator = cobble.field() + value = cobble.field() + + def matches(self, other): + return self.operator(self.value, other) + + diff --git a/mammoth/styles/parser/document_matcher_parser.py b/mammoth/styles/parser/document_matcher_parser.py index 5f85a18..95f3359 100644 --- a/mammoth/styles/parser/document_matcher_parser.py +++ b/mammoth/styles/parser/document_matcher_parser.py @@ -49,7 +49,7 @@ def _parse_style_name(tokens): tokens.skip(TokenType.SYMBOL, "=") name = parse_string(tokens) tokens.skip(TokenType.SYMBOL, "]") - return name + return document_matchers.equal_to(name) else: return None diff --git a/tests/styles/parser/document_matcher_parser_tests.py b/tests/styles/parser/document_matcher_parser_tests.py index e34bb79..bec2368 100644 --- a/tests/styles/parser/document_matcher_parser_tests.py +++ b/tests/styles/parser/document_matcher_parser_tests.py @@ -33,7 +33,7 @@ def reads_paragraph_with_style_id(): @istest def reads_paragraph_with_exact_style_name(): assert_equal( - document_matchers.paragraph(style_name="Heading 1"), + document_matchers.paragraph(style_name=document_matchers.equal_to("Heading 1")), read_document_matcher("p[style-name='Heading 1']") ) @@ -79,7 +79,7 @@ def reads_run_with_style_id(): @istest def reads_run_with_style_name(): assert_equal( - document_matchers.run(style_name="Emphasis"), + document_matchers.run(style_name=document_matchers.equal_to("Emphasis")), read_document_matcher("r[style-name='Emphasis']") ) |
