diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-11-04 11:25:51 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-04 11:25:51 +0000 |
| commit | ade2f7706813d785345563c57e7baf902ea2a7f4 (patch) | |
| tree | 5003acc0e03415dbe3eb71e2f35cbd8285661975 /mammoth | |
| parent | 1498397f4ede71d81ec79f380d43ec041438a047 (diff) | |
Add style mappings for tables
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/conversion.py | 19 | ||||
| -rw-r--r-- | mammoth/document_matchers.py | 8 | ||||
| -rw-r--r-- | mammoth/styles/parser/document_matcher_parser.py | 11 |
3 files changed, 30 insertions, 8 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 066544e..eb514ec 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -169,9 +169,14 @@ class _DocumentConverter(documents.element_visitor(args=1)): def visit_tab(self, tab, context): return [html.text("\t")] - - + + _default_table_path = html_paths.path([html_paths.element(["table"], fresh=True)]) + def visit_table(self, table, context): + return self._find_html_path(table, "table", self._default_table_path) \ + .wrap(lambda: self._convert_table_children(table, context)) + + def _convert_table_children(self, table, context): body_index = find_index( lambda child: not isinstance(child, documents.TableRow) or not child.is_header, table.children, @@ -189,7 +194,7 @@ class _DocumentConverter(documents.element_visitor(args=1)): html.element("tbody", {}, body_rows), ] - return [html.element("table", {}, children)] + return [html.force_write] + children def visit_table_row(self, table_row, context): @@ -306,18 +311,18 @@ class _DocumentConverter(documents.element_visitor(args=1)): def _find_html_path_for_paragraph(self, paragraph): default = html_paths.path([html_paths.element("p", fresh=True)]) - return self._find_html_path(paragraph, "paragraph", default) + return self._find_html_path(paragraph, "paragraph", default, warn_unrecognised=True) def _find_html_path_for_run(self, run): - return self._find_html_path(run, "run", default=html_paths.empty) + return self._find_html_path(run, "run", default=html_paths.empty, warn_unrecognised=True) - def _find_html_path(self, element, element_type, default): + def _find_html_path(self, element, element_type, default, warn_unrecognised=False): style = self._find_style(element, element_type) if style is not None: return style.html_path - if getattr(element, "style_id", None) is not None: + if warn_unrecognised and getattr(element, "style_id", None) is not None: self._messages.append(results.warning( "Unrecognised {0} style: {1} (Style ID: {2})".format( element_type, element.style_name, element.style_id) diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index 6429911..91adf3f 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -19,6 +19,14 @@ RunMatcher = collections.namedtuple("RunMatcher", ["style_id", "style_name"]) RunMatcher.element_type = "run" +def table(style_id=None, style_name=None): + return TableMatcher(style_id, style_name) + + +TableMatcher = collections.namedtuple("TableMatcher", ["style_id", "style_name"]) +TableMatcher.element_type = "table" + + class bold(object): element_type = "bold" diff --git a/mammoth/styles/parser/document_matcher_parser.py b/mammoth/styles/parser/document_matcher_parser.py index 034c59c..b9e09f3 100644 --- a/mammoth/styles/parser/document_matcher_parser.py +++ b/mammoth/styles/parser/document_matcher_parser.py @@ -24,7 +24,16 @@ def parse_document_matcher(tokens): style_id=style_id, style_name=style_name, ) - + + elif tokens.try_skip(TokenType.IDENTIFIER, "table"): + style_id = try_parse_class_name(tokens) + style_name = _parse_style_name(tokens) + + return document_matchers.table( + style_id=style_id, + style_name=style_name, + ) + elif tokens.try_skip(TokenType.IDENTIFIER, "b"): return document_matchers.bold |
