From ce5a4bef9f49f37b934c32da45031d9be4f0f67b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Aug 2014 11:24:39 +0100 Subject: Read footnotes and store in Document --- mammoth/docx/document_xml.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'mammoth/docx/document_xml.py') diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index f96098e..68b4bb3 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -7,17 +7,28 @@ from .. import lists from .xmlparser import node_types -def read_document_xml_element(element, numbering=None, content_types=None, relationships=None, styles=None, docx_file=None): +def read_document_xml_element(element, + numbering=None, + content_types=None, + relationships=None, + styles=None, + footnote_elements=None, + docx_file=None): + + if footnote_elements is None: + footnote_elements = [] + reader = _create_reader( numbering=numbering, content_types=content_types, relationships=relationships, styles=styles, + footnote_elements=footnote_elements, docx_file=docx_file, ) return reader(element) -def _create_reader(numbering, content_types, relationships, styles, docx_file): +def _create_reader(numbering, content_types, relationships, styles, footnote_elements, docx_file): _handlers = {} _ignored_elements = set([ "w:bookmarkStart", @@ -115,8 +126,21 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file): @handler("w:document") def document(element): body_element = _find_child(element, "w:body") - return _read_xml_elements(body_element.children) \ - .map(documents.document) + children_result = _read_xml_elements(body_element.children) + footnotes_result = results.combine(map(_read_footnote, footnote_elements)) + + return results.map( + lambda children, footnotes: documents.document( + children, + documents.footnotes(dict((footnote.id, footnote) for footnote in footnotes)) + ), + children_result, footnotes_result + ) + + + def _read_footnote(element): + return _read_xml_elements(element.body).map( + lambda body: documents.footnote(element.id, body)) @handler("w:tab") -- cgit v1.2.3