diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-06-13 12:16:25 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-06-13 12:16:25 +0100 |
| commit | 4a779b8d4fe83a80d99324a947b714c76468a88f (patch) | |
| tree | 71e9cd15e8a76beb4d7ec4145234ae70ec4c8dae /mammoth | |
| parent | 0d5b27652c119dfaf99fae5f4e624c9efec408b5 (diff) | |
Support style mapping for highlights
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/conversion.py | 14 | ||||
| -rw-r--r-- | mammoth/document_matchers.py | 7 | ||||
| -rw-r--r-- | mammoth/styles/parser/document_matcher_parser.py | 15 |
3 files changed, 36 insertions, 0 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index bdc8687..33f61cc 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -109,6 +109,10 @@ class _DocumentConverter(documents.element_visitor(args=1)): def visit_run(self, run, context): nodes = lambda: self._visit_all(run.children, context) paths = [] + if run.highlight is not None: + style = self._find_style(Highlight(color=run.highlight), "highlight") + if style is not None: + paths.append(style.html_path) if run.is_small_caps: paths.append(self._find_style_for_run_property("small_caps")) if run.is_all_caps: @@ -354,9 +358,19 @@ class _DocumentConverter(documents.element_visitor(args=1)): return "{0}{1}".format(self._id_prefix, suffix) +@cobble.data +class Highlight: + color = cobble.field() + + def _document_matcher_matches(matcher, element, element_type): 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 == "highlight": + return ( + matcher.element_type == element_type and + (matcher.color is None or matcher.color == element.color) + ) elif matcher.element_type == "break": return ( matcher.element_type == element_type and diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index f831b8a..5d07d39 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -51,6 +51,13 @@ class small_caps(object): element_type = "small_caps" +def highlight(color=None): + return HighlightMatcher(color=color) + + +HighlightMatcher = collections.namedtuple("HighlightMatcher", ["color"]) +HighlightMatcher.element_type = "highlight" + class comment_reference(object): element_type = "comment_reference" diff --git a/mammoth/styles/parser/document_matcher_parser.py b/mammoth/styles/parser/document_matcher_parser.py index 9891e4b..e0eb4e1 100644 --- a/mammoth/styles/parser/document_matcher_parser.py +++ b/mammoth/styles/parser/document_matcher_parser.py @@ -52,6 +52,9 @@ def parse_document_matcher(tokens): elif tokens.try_skip(TokenType.IDENTIFIER, "small-caps"): return document_matchers.small_caps + elif tokens.try_skip(TokenType.IDENTIFIER, "highlight"): + return _parse_highlight(tokens) + elif tokens.try_skip(TokenType.IDENTIFIER, "comment-reference"): return document_matchers.comment_reference @@ -98,6 +101,18 @@ def _parse_list_type(tokens): raise LineParseError("Unrecognised list type: {0}".format(list_type)) +def _parse_highlight(tokens): + if tokens.try_skip(TokenType.SYMBOL, "["): + tokens.skip(TokenType.IDENTIFIER, "color") + tokens.skip(TokenType.SYMBOL, "=") + color = parse_string(tokens) + tokens.skip(TokenType.SYMBOL, "]"); + else: + color = None + + return document_matchers.highlight(color=color) + + def _parse_break(tokens): tokens.skip(TokenType.SYMBOL, "[") tokens.skip(TokenType.IDENTIFIER, "type") |
