# coding=utf-8 from __future__ import unicode_literals import io from nose.tools import istest, assert_equal from mammoth import documents, style_reader, results, html from mammoth.conversion import convert_document_element_to_html from mammoth.docx.xmlparser import parse_xml @istest def plain_paragraph_is_converted_to_plain_paragraph(): result = convert_document_element_to_html( documents.paragraph(children=[_run_with_text("Hello")]) ) assert_equal('
Hello
', result.value) @istest def multiple_paragraphs_are_converted_to_multiple_paragraphs(): result = convert_document_element_to_html( documents.document([ documents.paragraph(children=[_run_with_text("Hello")]), documents.paragraph(children=[_run_with_text("there")]), ]) ) assert_equal('Hello
there
', result.value) @istest def empty_paragraphs_are_ignored(): result = convert_document_element_to_html( documents.paragraph(children=[_run_with_text("")]) ) assert_equal('', result.value) @istest def style_mappings_using_style_ids_can_be_used_to_map_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), style_map=[ _style_mapping("p.TipsParagraph => p.tip") ] ) assert_equal('Tip
', result.value) @istest def style_mappings_using_style_names_can_be_used_to_map_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", style_name="Tips Paragraph", children=[ _run_with_text("Tip") ]), style_map=[ _style_mapping("p[style-name='Tips Paragraph'] => p.tip") ] ) assert_equal('Tip
', result.value) @istest def style_names_in_style_mappings_are_case_insensitive(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", style_name="Tips Paragraph", children=[ _run_with_text("Tip") ]), style_map=[ _style_mapping("p[style-name='tips paragraph'] => p.tip") ] ) assert_equal('Tip
', result.value) @istest def default_paragraph_style_is_used_if_no_matching_style_is_found(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), ) assert_equal('Tip
', result.value) @istest def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), style_map=[ _style_mapping("p => p.tip") ] ) assert_equal('Tip
', result.value) @istest def warning_is_emitted_if_paragraph_style_is_unrecognised(): result = convert_document_element_to_html( documents.paragraph( style_id="Heading1", style_name="Heading 1", children=[_run_with_text("Tip")] ), ) assert_equal([results.warning("Unrecognised paragraph style: Heading 1 (Style ID: Heading1)")], 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) @istest def bulleted_paragraphs_are_converted_using_matching_styles(): result = convert_document_element_to_html( documents.paragraph(children=[ _run_with_text("Hello") ], numbering=documents.numbering_level(level_index=0, is_ordered=False)), style_map=[ _style_mapping("p:unordered-list(1) => ul > li:fresh") ] ) assert_equal('Hello
', result.value) @istest def bold_runs_are_wrapped_in_strong_tags_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_bold=True), ) assert_equal("Hello", result.value) @istest def bold_runs_can_be_configured_with_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_bold=True), style_map=[_style_mapping("b => em")] ) assert_equal("Hello", result.value) @istest def italic_runs_are_wrapped_in_emphasis_tags_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_italic=True), ) assert_equal("Hello", result.value) @istest def italic_runs_can_be_configured_with_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_italic=True), style_map=[_style_mapping("i => strong")] ) assert_equal("Hello", result.value) @istest def underline_runs_are_ignored_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True), ) assert_equal("Hello", result.value) @istest def underline_runs_can_be_mapped_using_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True), style_map=[ _style_mapping("u => em") ] ) assert_equal("Hello", result.value) @istest def style_mapping_for_underline_runs_does_not_close_parent_elements(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True, is_bold=True), style_map=[ _style_mapping("u => em") ] ) assert_equal("Hello", result.value) @istest def strikethrough_runs_are_wrapped_in_s_elements_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_strikethrough=True), ) assert_equal("Top left | Top right |
Bottom left | Bottom right |
Top right |
Knock knock[1]
' + 'Who\'s there? ↑