diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-07-04 13:29:23 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-07-04 13:29:23 +0100 |
| commit | 044398430b84b73fa6fce57405254f4a63c42424 (patch) | |
| tree | 61812a72d68dcbb7cd09f921b9088c2d41299342 /mammoth | |
| parent | 8fa1fcd70c807dfae3de83e52c273857e6a9db50 (diff) | |
Split out body reader construction
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/docx/__init__.py | 43 | ||||
| -rw-r--r-- | mammoth/docx/document_xml.py | 17 |
2 files changed, 30 insertions, 30 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 6e45882..3e7c0e0 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -8,6 +8,7 @@ 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 .notes_xml import read_footnotes_xml_element, read_endnotes_xml_element +from . import body_xml _namespaces = [ @@ -25,17 +26,8 @@ _namespaces = [ def read(fileobj): zip_file = zipfile.ZipFile(fileobj) - with _open_entry(zip_file, "[Content_Types].xml") as content_types_fileobj: - content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj)) - - with _open_entry(zip_file, "word/_rels/document.xml.rels") as relationships_fileobj: - relationships = read_relationships_xml_element(_parse_docx_xml(relationships_fileobj)) - - numbering = _try_read_entry_or_default( - zip_file, "word/numbering.xml", read_numbering_xml_element, default=Numbering({})) - - with _open_entry(zip_file, "word/styles.xml") as styles_fileobj: - styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj)) + body_readers = _body_readers(zip_file) + body_reader = body_readers("document") footnote_elements = _try_read_entry_or_default( zip_file, "word/footnotes.xml", read_footnotes_xml_element, default=[]) @@ -47,14 +39,35 @@ def read(fileobj): document_xml = _parse_docx_xml(document_fileobj) return read_document_xml_element( document_xml, + body_reader=body_reader, + footnote_elements=footnote_elements, + endnote_elements=endnote_elements, + ) + + +def _body_readers(zip_file): + with _open_entry(zip_file, "[Content_Types].xml") as content_types_fileobj: + content_types = read_content_types_xml_element(_parse_docx_xml(content_types_fileobj)) + + numbering = _try_read_entry_or_default( + zip_file, "word/numbering.xml", read_numbering_xml_element, default=Numbering({})) + + with _open_entry(zip_file, "word/styles.xml") as styles_fileobj: + styles = read_styles_xml_element(_parse_docx_xml(styles_fileobj)) + + def for_name(name): + with _open_entry(zip_file, "word/_rels/{0}.xml.rels".format(name)) as relationships_fileobj: + relationships = read_relationships_xml_element(_parse_docx_xml(relationships_fileobj)) + + return body_xml.reader( + numbering=numbering, content_types=content_types, relationships=relationships, - docx_file=zip_file, - numbering=numbering, styles=styles, - footnote_elements=footnote_elements, - endnote_elements=endnote_elements, + docx_file=zip_file, ) + + return for_name def _parse_docx_xml(fileobj): diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index 7288dc2..3cdbec2 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -1,27 +1,14 @@ from .. import documents from .. import results -from . import body_xml def read_document_xml_element(element, - numbering=None, - content_types=None, - relationships=None, - styles=None, + body_reader, footnote_elements=None, - endnote_elements=None, - docx_file=None): + endnote_elements=None): note_elements = (footnote_elements or []) + (endnote_elements or []) - body_reader = body_xml.reader( - numbering=numbering, - content_types=content_types, - relationships=relationships, - styles=styles, - docx_file=docx_file, - ) - def _read_note(element): return body_reader.read_all(element.body).map( lambda body: documents.note(element.note_type, element.id, body)) |
