From 8fde170b06becb51d2650b60dd28e23eaf862e25 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 5 Nov 2020 19:43:01 +0000 Subject: Add style mapping for all caps --- mammoth/conversion.py | 4 +++- mammoth/document_matchers.py | 10 +++++++--- mammoth/documents.py | 3 +++ mammoth/docx/body_xml.py | 2 ++ mammoth/styles/parser/document_matcher_parser.py | 21 ++++++++++++--------- 5 files changed, 27 insertions(+), 13 deletions(-) (limited to 'mammoth') diff --git a/mammoth/conversion.py b/mammoth/conversion.py index d463116..bdc8687 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -111,6 +111,8 @@ class _DocumentConverter(documents.element_visitor(args=1)): paths = [] if run.is_small_caps: paths.append(self._find_style_for_run_property("small_caps")) + if run.is_all_caps: + paths.append(self._find_style_for_run_property("all_caps")) if run.is_strikethrough: paths.append(self._find_style_for_run_property("strikethrough", default="s")) if run.is_underline: @@ -353,7 +355,7 @@ class _DocumentConverter(documents.element_visitor(args=1)): def _document_matcher_matches(matcher, element, element_type): - if matcher.element_type in ["underline", "strikethrough", "small_caps", "bold", "italic", "comment_reference"]: + if matcher.element_type in ["underline", "strikethrough", "all_caps", "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 91adf3f..f831b8a 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -43,6 +43,10 @@ class strikethrough(object): element_type = "strikethrough" +class all_caps(object): + element_type = "all_caps" + + class small_caps(object): element_type = "small_caps" @@ -66,8 +70,8 @@ def equal_to(value): def _operator_equal_to(first, second): return first.upper() == second.upper() - - + + def starts_with(value): return StringMatcher(_operator_starts_with, value) @@ -79,6 +83,6 @@ def _operator_starts_with(first, second): class StringMatcher(object): operator = cobble.field() value = cobble.field() - + def matches(self, other): return self.operator(self.value, other) diff --git a/mammoth/documents.py b/mammoth/documents.py index e1e1e58..7559d40 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -48,6 +48,7 @@ class Run(HasChildren): is_italic = cobble.field() is_underline = cobble.field() is_strikethrough = cobble.field() + is_all_caps = cobble.field() is_small_caps = cobble.field() vertical_alignment = cobble.field() font = cobble.field() @@ -122,6 +123,7 @@ def run( is_italic=None, is_underline=None, is_strikethrough=None, + is_all_caps=None, is_small_caps=None, vertical_alignment=None, font=None, @@ -137,6 +139,7 @@ def run( is_italic=bool(is_italic), is_underline=bool(is_underline), is_strikethrough=bool(is_strikethrough), + is_all_caps=bool(is_all_caps), 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 dcc704b..17cf31b 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -91,6 +91,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_underline_element(properties.find_child("w:u")) is_strikethrough = read_boolean_element(properties.find_child("w:strike")) + is_all_caps = read_boolean_element(properties.find_child("w:caps")) is_small_caps = read_boolean_element(properties.find_child("w:smallCaps")) def add_complex_field_hyperlink(children): @@ -111,6 +112,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_all_caps=is_all_caps, 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 b9e09f3..9891e4b 100644 --- a/mammoth/styles/parser/document_matcher_parser.py +++ b/mammoth/styles/parser/document_matcher_parser.py @@ -9,17 +9,17 @@ def parse_document_matcher(tokens): style_id = try_parse_class_name(tokens) style_name = _parse_style_name(tokens) numbering = _parse_numbering(tokens) - + return document_matchers.paragraph( style_id=style_id, style_name=style_name, numbering=numbering, ) - + elif tokens.try_skip(TokenType.IDENTIFIER, "r"): style_id = try_parse_class_name(tokens) style_name = _parse_style_name(tokens) - + return document_matchers.run( style_id=style_id, style_name=style_name, @@ -36,19 +36,22 @@ def parse_document_matcher(tokens): elif tokens.try_skip(TokenType.IDENTIFIER, "b"): return document_matchers.bold - + elif tokens.try_skip(TokenType.IDENTIFIER, "i"): return document_matchers.italic - + elif tokens.try_skip(TokenType.IDENTIFIER, "u"): return document_matchers.underline - + elif tokens.try_skip(TokenType.IDENTIFIER, "strike"): return document_matchers.strikethrough - + + elif tokens.try_skip(TokenType.IDENTIFIER, "all-caps"): + return document_matchers.all_caps + 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 @@ -101,7 +104,7 @@ def _parse_break(tokens): tokens.skip(TokenType.SYMBOL, "=") type_name = parse_string(tokens) tokens.skip(TokenType.SYMBOL, "]"); - + if type_name == "line": return document_matchers.line_break elif type_name == "page": -- cgit v1.2.3