summaryrefslogtreecommitdiff
path: root/tests/mammoth_tests.py
blob: d749b4ccbff95696a89c5543d5a110c1057865e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# coding=utf-8

from __future__ import unicode_literals

import io
import shutil
import os

import tempman
from nose.tools import istest, assert_equal

from .testing import test_path

import mammoth
from mammoth import results


@istest
def docx_containing_one_paragraph_is_converted_to_single_p_element():
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("<p>Walking on imported air</p>", result.value)
        assert_equal([], result.messages)


@istest
def can_read_xml_files_with_utf8_bom():
    with open(test_path("utf8-bom.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("<p>This XML has a byte order mark.</p>", result.value)
        assert_equal([], result.messages)


@istest
def empty_paragraphs_are_ignored_by_default():
    with open(test_path("empty.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("", result.value)
        assert_equal([], result.messages)


@istest
def empty_paragraphs_are_preserved_if_ignore_empty_paragraphs_is_false():
    with open(test_path("empty.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False)
        assert_equal("<p></p>", result.value)
        assert_equal([], result.messages)


@istest
def embedded_style_map_is_used_if_present():
    with _copy_of_test_data("single-paragraph.docx") as fileobj:
        mammoth.embed_style_map(fileobj, "p => h1")
        result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False)
        assert_equal("<h1>Walking on imported air</h1>", result.value)
        assert_equal([], result.messages)


@istest
def embedded_style_map_can_be_retrieved():
    with _copy_of_test_data("single-paragraph.docx") as fileobj:
        mammoth.embed_style_map(fileobj, "p => h1")
        assert_equal("p => h1", mammoth.read_embedded_style_map(fileobj))


@istest
def warning_if_style_mapping_is_not_understood():
    style_map = """
!!!!
p => h1"""
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, style_map=style_map)
        assert_equal("<h1>Walking on imported air</h1>", result.value)
        warning = "Did not understand this style mapping, so ignored it: !!!!"
        assert_equal([results.warning(warning)], result.messages)


@istest
def inline_images_are_included_in_output():
    with open(test_path("tiny-picture.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
        assert_equal([], result.messages)


@istest
def images_stored_outside_of_document_are_included_in_output():
    with open(test_path("external-picture.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
        assert_equal([], result.messages)


@istest
def warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name():
    fileobj = io.BytesIO()
    with open(test_path("external-picture.docx"), "rb") as source_fileobj:
        shutil.copyfileobj(source_fileobj, fileobj)
    
    result = mammoth.convert_to_html(fileobj=fileobj)
    assert_equal("", result.value)
    assert_equal([results.warning("could not find external image 'tiny-picture.png', fileobj has no name")], result.messages)


@istest
def warn_if_images_stored_outside_of_document_are_not_found():
    with tempman.create_temp_dir() as temp_dir:
        document_path = os.path.join(temp_dir.path, "document.docx")
        with open(document_path, "wb") as fileobj:
            with open(test_path("external-picture.docx"), "rb") as source_fileobj:
                shutil.copyfileobj(source_fileobj, fileobj)
    
        with open(document_path, "rb") as fileobj:
            result = mammoth.convert_to_html(fileobj=fileobj)
            assert_equal("", result.value)
            expected_warning = "could not open external image: 'tiny-picture.png'"
            assert_equal("warning", result.messages[0].type)
            assert result.messages[0].message.startswith(expected_warning), "message was: " + result.messages[0].message
            assert_equal(1, len(result.messages))
        

@istest
def simple_list_is_converted_to_list_elements():
    with open(test_path("simple-list.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal([], result.messages)
        assert_equal("<ul><li>Apple</li><li>Banana</li></ul>", result.value)


@istest
def word_tables_are_converted_to_html_tables():
    expected_html = ("<p>Above</p>" +
        "<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>" +
        "<p>Below</p>")
    
    
    with open(test_path("tables.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal([], result.messages)
        assert_equal(expected_html, result.value)


@istest
def footnotes_are_appended_to_text():
    # TODO: don't duplicate footnotes with multiple references
    expected_html = ('<p>Ouch' +
        '<sup><a href="#doc-42-footnote-1" id="doc-42-footnote-ref-1">[1]</a></sup>.' +
        '<sup><a href="#doc-42-footnote-2" id="doc-42-footnote-ref-2">[2]</a></sup></p>' +
        '<ol><li id="doc-42-footnote-1"><p> A tachyon walks into a bar. <a href="#doc-42-footnote-ref-1">↑</a></p></li>' +
        '<li id="doc-42-footnote-2"><p> Fin. <a href="#doc-42-footnote-ref-2">↑</a></p></li></ol>')
    
    with open(test_path("footnotes.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42")
        assert_equal([], result.messages)
        assert_equal(expected_html, result.value)


@istest
def endnotes_are_appended_to_text():
    expected_html = ('<p>Ouch' +
        '<sup><a href="#doc-42-endnote-2" id="doc-42-endnote-ref-2">[1]</a></sup>.' +
        '<sup><a href="#doc-42-endnote-3" id="doc-42-endnote-ref-3">[2]</a></sup></p>' +
        '<ol><li id="doc-42-endnote-2"><p> A tachyon walks into a bar. <a href="#doc-42-endnote-ref-2">↑</a></p></li>' +
        '<li id="doc-42-endnote-3"><p> Fin. <a href="#doc-42-endnote-ref-3">↑</a></p></li></ol>')
    
    with open(test_path("endnotes.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42")
        assert_equal([], result.messages)
        assert_equal(expected_html, result.value)


@istest
def relationships_are_handled_properly_in_footnotes():
    expected_html = (
        '<p><sup><a href="#doc-42-footnote-1" id="doc-42-footnote-ref-1">[1]</a></sup></p>' +
        '<ol><li id="doc-42-footnote-1"><p> <a href="http://www.example.com">Example</a> <a href="#doc-42-footnote-ref-1">↑</a></p></li></ol>')
    
    with open(test_path("footnote-hyperlink.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42")
        assert_equal([], result.messages)
        assert_equal(expected_html, result.value)


@istest
def text_boxes_are_read():
    with open(test_path("text-box.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal('<p>Datum plane</p>', result.value)


@istest
def underline_is_ignored_by_default():
    with open(test_path("underline.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal('<p><strong>The </strong><strong>Sunset</strong><strong> Tree</strong></p>', result.value)


@istest
def underline_can_be_configured_with_convert_underline_option():
    with open(test_path("underline.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, convert_underline=mammoth.underline.element("em"))
        assert_equal('<p><strong>The </strong><strong><em>Sunset</em></strong><strong> Tree</strong></p>', result.value)


@istest
def underline_can_be_configured_with_style_mapping():
    with open(test_path("underline.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, style_map="u => em")
        assert_equal('<p><strong>The </strong><strong><em>Sunset</em></strong><strong> Tree</strong></p>', result.value)


@istest
def strikethrough_is_converted_to_s_element_by_default():
    with open(test_path("strikethrough.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj)
        assert_equal("<p><s>Today's Special: Salmon</s> Sold out</p>", result.value)


@istest
def strikethrough_conversion_can_be_configured_with_style_mapping():
    with open(test_path("strikethrough.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, style_map="strike => del")
        assert_equal("<p><del>Today's Special: Salmon</del> Sold out</p>", result.value)


@istest
def transform_document_is_applied_to_document_before_conversion():
    def transform_document(document):
        document.children[0].style_id = "Heading1"
        return document
    
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_html(fileobj=fileobj, transform_document=transform_document)
        assert_equal("<h1>Walking on imported air</h1>", result.value)
        assert_equal([], result.messages)


@istest
def docx_containing_one_paragraph_can_be_converted_to_markdown():
    with open(test_path("single-paragraph.docx"), "rb") as fileobj:
        result = mammoth.convert_to_markdown(fileobj=fileobj)
        assert_equal("Walking on imported air\n\n", result.value)
        assert_equal([], result.messages)


@istest
def can_extract_raw_text():
    with open(test_path("simple-list.docx"), "rb") as fileobj:
        result = mammoth.extract_raw_text(fileobj=fileobj)
        assert_equal([], result.messages)
        assert_equal("Apple\n\nBanana\n\n", result.value)
        

def _copy_of_test_data(path):
    destination = io.BytesIO()
    with open(test_path(path), "rb") as source:
        shutil.copyfileobj(source, destination)
    return destination