diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-08-30 12:02:49 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-08-30 12:02:49 +0100 |
| commit | f4df40207f5ff873ac4f2e155373e7a10ee0ddca (patch) | |
| tree | a2923addccd51977727745026ea3a4f5969a18cd | |
| parent | cc671a920c1ce5886fcc6629965000bfc469fea1 (diff) | |
Support footnotes
| -rw-r--r-- | mammoth/__init__.py | 3 | ||||
| -rw-r--r-- | mammoth/docx/__init__.py | 8 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 19 | ||||
| -rwxr-xr-x | tests/test-data/footnotes.docx | bin | 0 -> 15936 bytes |
4 files changed, 29 insertions, 1 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py index e70e8ca..b0ffa12 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -3,12 +3,13 @@ from . import docx, conversion, options, documents, images, underline __all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] -def convert_to_html(fileobj, transform_document=None, **kwargs): +def convert_to_html(fileobj, transform_document=None, generate_uniquifier=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, + generate_uniquifier=generate_uniquifier, **options.read_options(kwargs) ) ) diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index df3071d..2c3e780 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -7,6 +7,7 @@ from .content_types_xml import read_content_types_xml_element from .relationships_xml import read_relationships_xml_element from .numbering_xml import read_numbering_xml_element, Numbering from .styles_xml import read_styles_xml_element +from .footnotes_xml import read_footnotes_xml_element _namespaces = [ @@ -37,6 +38,12 @@ def read(fileobj): with _open_entry(zip_file, "word/styles.xml") as styles_fileobj: styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj)) + if _has_entry(zip_file, "word/footnotes.xml"): + with _open_entry(zip_file, "word/footnotes.xml") as footnotes_fileobj: + footnote_elements = read_footnotes_xml_element(_parse_docx_xml(footnotes_fileobj)) + else: + footnote_elements = [] + with _open_entry(zip_file, "word/document.xml") as document_fileobj: document_xml = _parse_docx_xml(document_fileobj) return read_document_xml_element( @@ -46,6 +53,7 @@ def read(fileobj): docx_file=zip_file, numbering=numbering, styles=styles, + footnote_elements=footnote_elements, ) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 7ef8049..f744711 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -1,3 +1,5 @@ +# coding=utf-8 + from nose.tools import istest, assert_equal from .testing import test_path @@ -52,6 +54,23 @@ def word_tables_are_converted_to_html_tables(): 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 = (u'<p>Ouch' + + u'<sup><a href="#footnote-42-1" id="footnote-ref-42-1">[1]</a></sup>.' + + u'<sup><a href="#footnote-42-2" id="footnote-ref-42-2">[2]</a></sup></p>' + + u'<ol><li id="footnote-42-1"><p> A tachyon walks into a bar. <a href="#footnote-ref-42-1">↑</a></p></li>' + + u'<li id="footnote-42-2"><p> Fin. <a href="#footnote-ref-42-2">↑</a></p></li></ol>') + + with open(test_path("footnotes.docx"), "rb") as fileobj: + result = mammoth.convert_to_html(fileobj=fileobj, generate_uniquifier=lambda: 42) + # TODO: get rid of warnings + #~ assert_equal([], result.messages) + assert_equal(expected_html, result.value) + + @istest def transform_document_is_applied_to_document_before_conversion(): def transform_document(document): diff --git a/tests/test-data/footnotes.docx b/tests/test-data/footnotes.docx Binary files differnew file mode 100755 index 0000000..3228e74 --- /dev/null +++ b/tests/test-data/footnotes.docx |
