From 90a19c890a3634f9ba8d8435af04982f9a8b76da Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 14 Dec 2013 16:25:32 +0000 Subject: Emit warnings on recognised paragraph styles --- mammoth/conversion.py | 8 ++++++-- tests/conversion_tests.py | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 624f6cb..e3a6fe1 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -9,11 +9,12 @@ def convert_document_element_to_html(element, styles=None): converter = DocumentConverter(styles) converter.convert_element_to_html(element, html_generator) html_generator.end_all() - return results.success(html_generator.html_string()) + return results.Result(html_generator.html_string(), converter.messages) class DocumentConverter(object): def __init__(self, styles): + self.messages = [] self._styles = styles self._converters = { documents.Document: self._convert_document, @@ -57,5 +58,8 @@ class DocumentConverter(object): document_matcher.style_name is None or document_matcher.style_name == paragraph.style_name): return style.html_path - + + if paragraph.style_name is not None: + self.messages.append(results.warning("Unrecognised paragraph style: {0}".format(paragraph.style_name))) + return html_paths.path([html_paths.element("p")]) diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index d05a103..647f58c 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -1,6 +1,6 @@ from nose.tools import istest, assert_equal -from mammoth import documents, style_reader +from mammoth import documents, style_reader, results from mammoth.conversion import convert_document_element_to_html @@ -54,6 +54,24 @@ def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): ] ) assert_equal('

Tip

', result.value) + + +@istest +def warning_is_emitted_if_paragraph_style_is_unrecognised(): + result = convert_document_element_to_html( + documents.paragraph(style_name="TipsParagraph", children=[ + _run_with_text("Tip") + ]), + ) + assert_equal([results.warning("Unrecognised paragraph style: TipsParagraph")], result.messages) + + +@istest +def no_warning_if_there_is_no_style_for_plain_paragraphs(): + result = convert_document_element_to_html( + documents.paragraph(children=[_run_with_text("Tip")]), + ) + assert_equal([], result.messages) def _run_with_text(text): -- cgit v1.2.3