From f4df40207f5ff873ac4f2e155373e7a10ee0ddca Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Aug 2014 12:02:49 +0100 Subject: Support footnotes --- mammoth/__init__.py | 3 ++- mammoth/docx/__init__.py | 8 ++++++++ tests/mammoth_tests.py | 19 +++++++++++++++++++ tests/test-data/footnotes.docx | Bin 0 -> 15936 bytes 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100755 tests/test-data/footnotes.docx 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'

Ouch' + + u'[1].' + + u'[2]

' + + u'
  1. A tachyon walks into a bar. ↑

  2. ' + + u'
  3. Fin. ↑

') + + 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 new file mode 100755 index 0000000..3228e74 Binary files /dev/null and b/tests/test-data/footnotes.docx differ -- cgit v1.2.3