diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-08-15 18:11:46 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-08-15 18:11:46 +0100 |
| commit | 2c9955f0590d93968b4959e3622d01610e398700 (patch) | |
| tree | 886f42a3c647d8f0716a871685e925b95bd2bc6f /mammoth | |
| parent | 724f9f01d83865ae586e7a8f65545e1a11d2cdae (diff) | |
Continue with warning style mapping cannot be parsed
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/__init__.py | 12 | ||||
| -rw-r--r-- | mammoth/options.py | 16 | ||||
| -rw-r--r-- | mammoth/style_reader/__init__.py | 11 |
3 files changed, 26 insertions, 13 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py index f67f890..297cf24 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -15,11 +15,13 @@ def convert_to_markdown(*args, **kwargs): def convert(fileobj, transform_document=None, id_prefix=None, **kwargs): if transform_document is None: transform_document = lambda x: x - return docx.read(fileobj).map(transform_document).bind(lambda document: - conversion.convert_document_element_to_html( - document, - id_prefix=id_prefix, - **options.read_options(kwargs) + return options.read_options(kwargs).bind(lambda convert_options: + docx.read(fileobj).map(transform_document).bind(lambda document: + conversion.convert_document_element_to_html( + document, + id_prefix=id_prefix, + **convert_options + ) ) ) diff --git a/mammoth/options.py b/mammoth/options.py index 9d09146..1909b86 100644 --- a/mammoth/options.py +++ b/mammoth/options.py @@ -1,19 +1,20 @@ -from . import style_reader, lists +from . import style_reader, lists, results def read_options(options): - custom_style_map = _read_style_map(options.get("style_map") or "") + custom_style_map_result = _read_style_map(options.get("style_map") or "") include_default_style_map = options.get("include_default_style_map", True) options["ignore_empty_paragraphs"] = options.get("ignore_empty_paragraphs", True) - options["style_map"] = custom_style_map + \ + options["style_map"] = custom_style_map_result.value + \ (_default_style_map if include_default_style_map else []) - return options + return custom_style_map_result.map(lambda _: options) def _read_style_map(style_text): lines = filter(None, map(_get_line, style_text.split("\n"))) - return lists.map(style_reader.read_style, lines) + return results.combine(lists.map(style_reader.read_style, lines)) \ + .map(lambda style_mappings: filter(None, style_mappings)) def _get_line(line): @@ -24,7 +25,7 @@ def _get_line(line): return line -_default_style_map = _read_style_map(""" +_default_style_map_result = _read_style_map(""" p[style-name='Normal'] => p:fresh p.Heading1 => h1:fresh @@ -68,3 +69,6 @@ p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li r[style-name='Hyperlink'] => """) + +assert not _default_style_map_result.messages +_default_style_map = _default_style_map_result.value diff --git a/mammoth/style_reader/__init__.py b/mammoth/style_reader/__init__.py index 28281a8..aca07df 100644 --- a/mammoth/style_reader/__init__.py +++ b/mammoth/style_reader/__init__.py @@ -1,16 +1,23 @@ from parsimonious.grammar import Grammar +from parsimonious.exceptions import ParseError from .html_path_reader import read_html_path, grammar_text as html_path_grammar, read_html_path_node from .document_matcher_reader import read_document_matcher, grammar_text as document_matcher_grammar, read_document_matcher_node from ..styles import Style +from .. import results __all__ = ["read_style", "read_html_path", "read_document_matcher"] def read_style(string): - style_node = _grammar.parse(string) - return _read_style_node(style_node) + try: + style_node = _grammar.parse(string) + except ParseError: + warning = "Did not understand this style mapping, so ignored it: " + string + return results.Result(None, [results.warning(warning)]) + + return results.success(_read_style_node(style_node)) def _read_style_node(style_node): |
