diff options
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | README.md | 11 | ||||
| -rw-r--r-- | mammoth/conversion.py | 4 | ||||
| -rw-r--r-- | mammoth/document_matchers.py | 4 | ||||
| -rw-r--r-- | mammoth/documents.py | 13 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 2 | ||||
| -rw-r--r-- | mammoth/styles/parser/document_matcher_parser.py | 3 | ||||
| -rw-r--r-- | tests/conversion_tests.py | 19 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 11 | ||||
| -rw-r--r-- | tests/styles/parser/document_matcher_parser_tests.py | 7 |
10 files changed, 75 insertions, 3 deletions
@@ -1,3 +1,7 @@ +# 1.4.3 + +* Add style mapping for small caps. + # 1.4.2 * Read children of v:group elements. @@ -581,6 +581,17 @@ strike Note that this matches text that has had strikethrough explicitly applied to it. It will not match any text that is struckthrough because of its paragraph or run style. +#### Small caps + +Match explicitly small caps text: + +``` +small-caps +``` + +Note that this matches text that has had small caps explicitly applied to it. +It will not match any text that is small caps because of its paragraph or run style. + ### HTML paths #### Single elements diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 0fc3d58..066544e 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -109,6 +109,8 @@ class _DocumentConverter(documents.element_visitor(args=1)): def visit_run(self, run, context): nodes = lambda: self._visit_all(run.children, context) paths = [] + if run.is_small_caps: + paths.append(self._find_style_for_run_property("small_caps")) if run.is_strikethrough: paths.append(self._find_style_for_run_property("strikethrough", default="s")) if run.is_underline: @@ -346,7 +348,7 @@ class _DocumentConverter(documents.element_visitor(args=1)): def _document_matcher_matches(matcher, element, element_type): - if matcher.element_type in ["underline", "strikethrough", "bold", "italic", "comment_reference"]: + if matcher.element_type in ["underline", "strikethrough", "small_caps", "bold", "italic", "comment_reference"]: return matcher.element_type == element_type elif matcher.element_type == "break": return ( diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index faccb50..6429911 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -35,6 +35,10 @@ class strikethrough(object): element_type = "strikethrough" +class small_caps(object): + element_type = "small_caps" + + class comment_reference(object): element_type = "comment_reference" diff --git a/mammoth/documents.py b/mammoth/documents.py index 04e9d8e..d89de3f 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -30,6 +30,7 @@ class Run(HasChildren): is_italic = cobble.field() is_underline = cobble.field() is_strikethrough = cobble.field() + is_small_caps = cobble.field() vertical_alignment = cobble.field() font = cobble.field() @@ -95,14 +96,22 @@ def run( is_italic=None, is_underline=None, is_strikethrough=None, + is_small_caps=None, vertical_alignment=None, font=None, ): if vertical_alignment is None: vertical_alignment = VerticalAlignment.baseline return Run( - children, style_id, style_name, bool(is_bold), bool(is_italic), - bool(is_underline), bool(is_strikethrough), vertical_alignment, + children=children, + style_id=style_id, + style_name=style_name, + is_bold=bool(is_bold), + is_italic=bool(is_italic), + is_underline=bool(is_underline), + is_strikethrough=bool(is_strikethrough), + is_small_caps=bool(is_small_caps), + vertical_alignment=vertical_alignment, font=font, ) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 312e9de..12951c9 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -84,6 +84,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f is_italic = read_boolean_element(properties.find_child("w:i")) is_underline = read_boolean_element(properties.find_child("w:u")) is_strikethrough = read_boolean_element(properties.find_child("w:strike")) + is_small_caps = read_boolean_element(properties.find_child("w:smallCaps")) def add_complex_field_hyperlink(children): hyperlink_href = current_hyperlink_href() @@ -103,6 +104,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f is_italic=is_italic, is_underline=is_underline, is_strikethrough=is_strikethrough, + is_small_caps=is_small_caps, vertical_alignment=vertical_alignment, font=font, )) diff --git a/mammoth/styles/parser/document_matcher_parser.py b/mammoth/styles/parser/document_matcher_parser.py index 36999d0..034c59c 100644 --- a/mammoth/styles/parser/document_matcher_parser.py +++ b/mammoth/styles/parser/document_matcher_parser.py @@ -37,6 +37,9 @@ def parse_document_matcher(tokens): elif tokens.try_skip(TokenType.IDENTIFIER, "strike"): return document_matchers.strikethrough + elif tokens.try_skip(TokenType.IDENTIFIER, "small-caps"): + return document_matchers.small_caps + elif tokens.try_skip(TokenType.IDENTIFIER, "comment-reference"): return document_matchers.comment_reference diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 77b0a30..38aa1a8 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -228,6 +228,25 @@ def strikethrough_runs_can_be_configured_with_style_mapping(): ] ) assert_equal("<del>Hello</del>", result.value) + + +@istest +def small_caps_runs_are_ignored_by_default(): + result = convert_document_element_to_html( + documents.run(children=[documents.text("Hello")], is_small_caps=True), + ) + assert_equal("Hello", result.value) + + +@istest +def small_caps_runs_can_be_mapped_using_style_mapping(): + result = convert_document_element_to_html( + documents.run(children=[documents.text("Hello")], is_small_caps=True), + style_map=[ + _style_mapping("small-caps => span") + ] + ) + assert_equal("<span>Hello</span>", result.value) @istest diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index bde96e6..c81fade 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -213,12 +213,23 @@ class RunTests(object): def run_is_struckthrough_if_strikethrough_element_is_present(self): run = self._read_run_with_properties([xml_element("w:strike")]) assert_equal(True, run.is_strikethrough) + + @istest + def run_is_not_small_caps_if_small_caps_element_is_not_present(self): + run = self._read_run_with_properties([]) + assert_equal(False, run.is_small_caps) + + @istest + def run_is_small_caps_if_small_caps_element_is_present(self): + run = self._read_run_with_properties([xml_element("w:smallCaps")]) + assert_equal(True, run.is_small_caps) run_boolean_property_test = lambda func: parameterized([ param(attr_name="is_bold", tag_name="w:b"), param(attr_name="is_underline", tag_name="w:u"), param(attr_name="is_italic", tag_name="w:i"), param(attr_name="is_strikethrough", tag_name="w:strike"), + param(attr_name="is_small_caps", tag_name="w:smallCaps"), ])(istest(func)) @run_boolean_property_test diff --git a/tests/styles/parser/document_matcher_parser_tests.py b/tests/styles/parser/document_matcher_parser_tests.py index 0286e7b..62f3181 100644 --- a/tests/styles/parser/document_matcher_parser_tests.py +++ b/tests/styles/parser/document_matcher_parser_tests.py @@ -126,6 +126,13 @@ def reads_strikethrough(): ) @istest +def reads_small_caps(): + assert_equal( + document_matchers.small_caps, + read_document_matcher("small-caps") + ) + +@istest def reads_comment_reference(): assert_equal( document_matchers.comment_reference, |
