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('<p>Tip</p>', result.value)
Пример #2
0
def footnote_reference_is_converted_to_superscript_intra_page_link():
    footnote_reference = documents.note_reference("footnote", "4")
    result = convert_document_element_to_html(footnote_reference,
                                              id_prefix="doc-42-")
    assert_equal(
        '<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup>',
        result.value)
def comment_references_are_linked_to_comment_after_main_body():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
        author_name="The Piemaker",
        author_initials="TP",
    )
    document = documents.document(
        [documents.paragraph([
            _run_with_text("Knock knock"),
            documents.run([reference])
        ])],
        comments=[comment],
    )
    result = convert_document_element_to_html(
        document,
        id_prefix="doc-42-",
        style_map=[
            _style_mapping("comment-reference => sup")
        ],
    )
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-comment-4" id="doc-42-comment-ref-4">[TP1]</a></sup></p>' +
        '<dl><dt id="doc-42-comment-4">Comment [TP1]</dt><dd><p>Who\'s there? <a href="#doc-42-comment-ref-4">↑</a></p></dd></dl>'
    )
    assert_equal(expected_html, result.value)
Пример #4
0
def images_have_alt_tags_if_available():
    image = documents.image(alt_text="It's a hat",
                            content_type="image/png",
                            open=lambda: io.BytesIO(b"abc"))
    result = convert_document_element_to_html(image)
    image_html = parse_xml(io.StringIO(result.value))
    assert_equal('It\'s a hat', image_html.attributes["alt"])
Пример #5
0
def bulleted_styles_dont_match_plain_paragraph():
    result = convert_document_element_to_html(
        documents.paragraph(children=[_run_with_text("Hello")]),
        style_map=[
            style_reader.read_style("p:unordered-list(1) => ul > li:fresh")
        ])
    assert_equal('<p>Hello</p>', result.value)
def unexpected_table_children_do_not_cause_error():
    table = documents.table([
        documents.tab(),
    ])
    result = convert_document_element_to_html(table)
    expected_html = "<table>\t</table>"
    assert_equal(expected_html, result.value)
Пример #7
0
def footnote_reference_is_converted_to_superscript_intra_page_link():
    footnote_reference = documents.footnote_reference("4")
    result = convert_document_element_to_html(footnote_reference,
                                              generate_uniquifier=lambda: 42)
    assert_equal(
        '<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup>',
        result.value)
Пример #8
0
def docx_table_is_converted_to_table_in_html():
    table = documents.table(
        [
            documents.table_row(
                [
                    documents.table_cell([_paragraph_with_text("Top left")]),
                    documents.table_cell([_paragraph_with_text("Top right")]),
                ]
            ),
            documents.table_row(
                [
                    documents.table_cell([_paragraph_with_text("Bottom left")]),
                    documents.table_cell([_paragraph_with_text("Bottom right")]),
                ]
            ),
        ]
    )
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>"
        + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>"
        + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>"
        + "</table>"
    )
    assert_equal(expected_html, result.value)
Пример #9
0
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('<p>Hello</p><p>there</p>', result.value)
Пример #10
0
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('<p class="tip">Tip</p>', result.value)
Пример #11
0
def unexpected_table_children_do_not_cause_error():
    table = documents.table([
        documents.tab(),
    ])
    result = convert_document_element_to_html(table)
    expected_html = "<table>\t</table>"
    assert_equal(expected_html, result.value)
Пример #12
0
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)
Пример #13
0
def subscript_runs_are_wrapped_in_sub_tags():
    result = convert_document_element_to_html(
        documents.run(
            children=[documents.text("Hello")],
            vertical_alignment=documents.VerticalAlignment.subscript,
        ), )
    assert_equal("<sub>Hello</sub>", result.value)
Пример #14
0
def docx_hyperlink_with_internal_anchor_reference_is_converted_to_anchor_tag():
    result = convert_document_element_to_html(
        documents.hyperlink(anchor="start",
                            children=[documents.Text("Hello")]),
        id_prefix="doc-42-",
    )
    assert_equal('<a href="#doc-42-start">Hello</a>', result.value)
Пример #15
0
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('<ul><li>Hello</li></ul>', result.value)
Пример #16
0
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("<strong><em>Hello</em></strong>", result.value)
Пример #17
0
def comment_references_are_linked_to_comment_after_main_body():
    reference = documents.comment_reference("4")
    comment = documents.comment(
        comment_id="4",
        body=[_paragraph_with_text("Who's there?")],
        author_name="The Piemaker",
        author_initials="TP",
    )
    document = documents.document(
        [
            documents.paragraph(
                [_run_with_text("Knock knock"),
                 documents.run([reference])])
        ],
        comments=[comment],
    )
    result = convert_document_element_to_html(
        document,
        id_prefix="doc-42-",
        style_map=[_style_mapping("comment-reference => sup")],
    )
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-comment-4" id="doc-42-comment-ref-4">[TP1]</a></sup></p>'
        +
        '<dl><dt id="doc-42-comment-4">Comment [TP1]</dt><dd><p>Who\'s there? <a href="#doc-42-comment-ref-4">↑</a></p></dd></dl>'
    )
    assert_equal(expected_html, result.value)
Пример #18
0
def footnote_reference_is_converted_to_superscript_intra_page_link():
    footnote_reference = documents.note_reference("footnote", "4")
    result = convert_document_element_to_html(
        footnote_reference,
        id_prefix="doc-42-"
    )
    assert_equal('<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup>', result.value)
Пример #19
0
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('<p>Tip</p>', result.value)
Пример #20
0
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('<p class="tip">Tip</p>', result.value)
Пример #21
0
def tbody_is_omitted_if_all_rows_are_headers():
    table = documents.table([
        documents.table_row([documents.table_cell([])], is_header=True),
    ])
    result = convert_document_element_to_html(table)
    expected_html = ("<table>" + "<thead><tr><th></th></tr></thead>" +
                     "</table>")
    assert_equal(expected_html, result.value)
Пример #22
0
def hyperlink_target_frame_is_used_as_anchor_target():
    result = convert_document_element_to_html(
        documents.hyperlink(
            anchor="start",
            target_frame="_blank",
            children=[documents.Text("Hello")],
        ), )
    assert_equal('<a href="#start" target="_blank">Hello</a>', result.value)
Пример #23
0
def can_define_custom_conversion_for_images():
    def convert_image(image, html_generator):
        with image.open() as image_file:
            html_generator.self_closing("img", {"alt": image_file.read().decode("ascii")})
        
    image = documents.image(alt_text=None, content_type="image/png", open=lambda: io.BytesIO(b"abc"))
    result = convert_document_element_to_html(image, convert_image=convert_image)
    assert_equal('<img alt="abc" />', result.value)
Пример #24
0
def runs_are_converted_by_satisfying_matching_paths():
    result = convert_document_element_to_html(
        documents.run(style_id="TipsRun", children=[documents.Text("Tip")]),
        style_map=[
            style_reader.read_style("r.TipsRun => span.tip")
        ]
    )
    assert_equal('<span class="tip">Tip</span>', result.value)
Пример #25
0
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_reader.read_style("p:unordered-list(1) => ul > li:fresh")],
    )
    assert_equal("<ul><li>Hello</li></ul>", result.value)
Пример #26
0
def small_caps_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_small_caps=True),
        style_map=[
            _style_mapping("small-caps => span")
        ]
    )
    assert_equal("<span>Hello</span>", result.value)
Пример #27
0
def breaks_can_be_mapped_using_style_mappings():
    result = convert_document_element_to_html(
        documents.page_break,
        style_map=[
            _style_mapping("br[type='page'] => hr")
        ],
    )
    assert_equal("<hr />", result.value)
Пример #28
0
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("<em>Hello</em>", result.value)
Пример #29
0
def strikethrough_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_strikethrough=True),
        style_map=[
            _style_mapping("strike => del")
        ]
    )
    assert_equal("<del>Hello</del>", result.value)
Пример #30
0
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("<em>Hello</em>", result.value)
Пример #31
0
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("<strong><em>Hello</em></strong>", result.value)
Пример #32
0
def subscript_runs_are_wrapped_in_sub_tags():
    result = convert_document_element_to_html(
        documents.run(
            children=[documents.text("Hello")],
            vertical_alignment=documents.VerticalAlignment.subscript,
        ),
    )
    assert_equal("<sub>Hello</sub>", result.value)
Пример #33
0
def small_caps_runs_can_be_mapped_using_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_small_caps=True),
        style_map=[
            _style_mapping("small-caps => span")
        ]
    )
    assert_equal("<span>Hello</span>", result.value)
Пример #34
0
def can_define_custom_conversion_for_images():
    def convert_image(image):
        with image.open() as image_file:
            return [html.element("img", {"alt": image_file.read().decode("ascii")})]

    image = documents.image(alt_text=None, content_type="image/png", open=lambda: io.BytesIO(b"abc"))
    result = convert_document_element_to_html(image, convert_image=convert_image)
    assert_equal('<img alt="abc" />', result.value)
Пример #35
0
def runs_are_converted_by_satisfying_matching_paths():
    result = convert_document_element_to_html(
        documents.run(style_id="TipsRun", children=[documents.Text("Tip")]),
        style_map=[
            _style_mapping("r.TipsRun => span.tip")
        ]
    )
    assert_equal('<span class="tip">Tip</span>', result.value)
Пример #36
0
def strikethrough_runs_can_be_configured_with_style_mapping():
    result = convert_document_element_to_html(
        documents.run(children=[documents.text("Hello")], is_strikethrough=True),
        style_map=[
            _style_mapping("strike => del")
        ]
    )
    assert_equal("<del>Hello</del>", result.value)
Пример #37
0
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('<p>Hello</p><p>there</p>', result.value)
Пример #38
0
def breaks_can_be_mapped_using_style_mappings():
    result = convert_document_element_to_html(
        documents.page_break,
        style_map=[
            _style_mapping("br[type='page'] => hr")
        ],
    )
    assert_equal("<hr />", result.value)
Пример #39
0
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)
Пример #40
0
def docx_hyperlinks_can_be_collapsed():
    result = convert_document_element_to_html(
        documents.document(children=[
            documents.hyperlink(href="http://example.com",
                                children=[documents.Text("Hello ")]),
            documents.hyperlink(href="http://example.com",
                                children=[documents.Text("world")]),
        ]))
    assert_equal('<a href="http://example.com">Hello world</a>', result.value)
Пример #41
0
def hyperlink_target_frame_is_used_as_anchor_target():
    result = convert_document_element_to_html(
        documents.hyperlink(
            anchor="start",
            target_frame="_blank",
            children=[documents.Text("Hello")],
        ),
    )
    assert_equal('<a href="#start" target="_blank">Hello</a>', result.value)
Пример #42
0
def bulleted_styles_dont_match_plain_paragraph():
    result = convert_document_element_to_html(
        documents.paragraph(children=[
            _run_with_text("Hello")
        ]),
        style_map=[
            _style_mapping("p:unordered-list(1) => ul > li:fresh")
        ]
    )
    assert_equal('<p>Hello</p>', result.value)
Пример #43
0
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_reader.read_style("p => p.tip")
        ]
    )
    assert_equal('<p class="tip">Tip</p>', result.value)
Пример #44
0
def paragraphs_are_converted_by_satisfying_matching_paths():
    result = convert_document_element_to_html(
        documents.paragraph(style_name="TipsParagraph", children=[
            _run_with_text("Tip")
        ]),
        style_map=[
            style_reader.read_style("p.TipsParagraph => p.tip")
        ]
    )
    assert_equal('<p class="tip">Tip</p>', result.value)
Пример #45
0
def table_style_mappings_can_be_used_to_map_tables():
    table = documents.table([], style_name="Normal Table")
    result = convert_document_element_to_html(
        table,
        style_map=[
            _style_mapping("table[style-name='Normal Table'] => table.normal-table:fresh"),
        ],
    )
    expected_html = '<table class="normal-table"></table>'
    assert_equal(expected_html, result.value)
Пример #46
0
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('<p class="tip">Tip</p>', result.value)
Пример #47
0
def table_cells_are_written_with_rowspan_if_not_equal_to_one():
    table = documents.table([
        documents.table_row([
            documents.table_cell([], rowspan=2),
        ]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = ("<table>" + "<tr><td rowspan=\"2\"></td></tr>" +
                     "</table>")
    assert_equal(expected_html, result.value)
Пример #48
0
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('<p class="tip">Tip</p>', result.value)
Пример #49
0
def table_style_mappings_can_be_used_to_map_tables():
    table = documents.table([], style_name="Normal Table")
    result = convert_document_element_to_html(
        table,
        style_map=[
            _style_mapping("table[style-name='Normal Table'] => table.normal-table:fresh"),
        ],
    )
    expected_html = '<table class="normal-table"></table>'
    assert_equal(expected_html, result.value)
Пример #50
0
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('<p class="tip">Tip</p>', result.value)
Пример #51
0
def tbody_is_omitted_if_all_rows_are_headers():
    table = documents.table([
        documents.table_row([documents.table_cell([])], is_header=True),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<thead><tr><th></th></tr></thead>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #52
0
def header_rows_are_wrapped_in_thead():
    table = documents.table([
        documents.table_row([documents.table_cell([])], is_header=True),
        documents.table_row([documents.table_cell([])], is_header=True),
        documents.table_row([documents.table_cell([])], is_header=False),
    ])
    result = convert_document_element_to_html(table)
    expected_html = ("<table>" +
                     "<thead><tr><th></th></tr><tr><th></th></tr></thead>" +
                     "<tbody><tr><td></td></tr></tbody>" + "</table>")
    assert_equal(expected_html, result.value)
Пример #53
0
def empty_rows_are_preserved_in_table():
    table = documents.table([
        documents.table_row([
            documents.table_cell([_paragraph_with_text("Row 1")]),
        ]),
        documents.table_row([]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = ("<table>" + "<tr><td><p>Row 1</p></td></tr><tr></tr>" +
                     "</table>")
    assert_equal(expected_html, result.value)
Пример #54
0
def footnotes_are_included_after_the_main_body():
    footnote_reference = documents.note_reference("footnote", "4")
    document = documents.document(
        [documents.paragraph([_run_with_text("Knock knock"), documents.run([footnote_reference])])],
        notes=documents.notes([documents.note("footnote", "4", [_paragraph_with_text("Who's there?")])]),
    )
    result = convert_document_element_to_html(document, id_prefix="doc-42")
    expected_html = (
        '<p>Knock knock<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup></p>'
        + '<ol><li id="doc-42-footnote-4"><p>Who\'s there? <a href="#doc-42-footnote-ref-4">↑</a></p></li></ol>'
    )
    assert_equal(expected_html, result.value)
Пример #55
0
def table_cells_are_written_with_rowspan_if_not_equal_to_one():
    table = documents.table([
        documents.table_row([
            documents.table_cell([], rowspan=2),
        ]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<tr><td rowspan=\"2\"></td></tr>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #56
0
def table_cells_are_written_with_colspan_if_not_equal_to_one():
    table = documents.table([
        documents.table_row([
            documents.table_cell([_paragraph_with_text("Top left")], colspan=2),
            documents.table_cell([_paragraph_with_text("Top right")]),
        ]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<tr><td colspan=\"2\"><p>Top left</p></td><td><p>Top right</p></td></tr>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #57
0
def table_cells_are_written_with_colspan_if_not_equal_to_one():
    table = documents.table([
        documents.table_row([
            documents.table_cell([_paragraph_with_text("Top left")], colspan=2),
            documents.table_cell([_paragraph_with_text("Top right")]),
        ]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<tr><td colspan=\"2\"><p>Top left</p></td><td><p>Top right</p></td></tr>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #58
0
def empty_rows_are_preserved_in_table():
    table = documents.table([
        documents.table_row([
            documents.table_cell([_paragraph_with_text("Row 1")]),
        ]),
        documents.table_row([]),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<tr><td><p>Row 1</p></td></tr><tr></tr>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #59
0
def header_rows_are_wrapped_in_thead():
    table = documents.table([
        documents.table_row([documents.table_cell([])], is_header=True),
        documents.table_row([documents.table_cell([])], is_header=True),
        documents.table_row([documents.table_cell([])], is_header=False),
    ])
    result = convert_document_element_to_html(table)
    expected_html = (
        "<table>" +
        "<thead><tr><th></th></tr><tr><th></th></tr></thead>" +
        "<tbody><tr><td></td></tr></tbody>" +
        "</table>")
    assert_equal(expected_html, result.value)
Пример #60
0
def empty_cells_are_preserved_in_table():
    table = documents.table(
        [
            documents.table_row(
                [
                    documents.table_cell([_paragraph_with_text("")]),
                    documents.table_cell([_paragraph_with_text("Top right")]),
                ]
            )
        ]
    )
    result = convert_document_element_to_html(table)
    expected_html = "<table>" + "<tr><td></td><td><p>Top right</p></td></tr>" + "</table>"
    assert_equal(expected_html, result.value)