aboutsummaryrefslogtreecommitdiff
path: root/mammoth
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-03 15:26:15 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-03 15:26:15 +0100
commitb249ef56971405e7b1d0b4bb3b9cfb4e779583ab (patch)
tree26d26bed8596a52613ee90a2ac1f745e86ef6a64 /mammoth
parent63974a9b4d026eed3b7a8b8c0ef2278ff28c4d18 (diff)
Wire up code for converting comments
Diffstat (limited to 'mammoth')
-rw-r--r--mammoth/docx/__init__.py31
-rw-r--r--mammoth/docx/body_xml.py2
-rw-r--r--mammoth/docx/document_xml.py14
-rw-r--r--mammoth/options.py2
4 files changed, 38 insertions, 11 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py
index 47e7a21..8fb150a 100644
--- a/mammoth/docx/__init__.py
+++ b/mammoth/docx/__init__.py
@@ -7,39 +7,56 @@ from .relationships_xml import read_relationships_xml_element, Relationships
from .numbering_xml import read_numbering_xml_element, Numbering
from .styles_xml import read_styles_xml_element
from .notes_xml import create_footnotes_reader, create_endnotes_reader
+from .comments_xml import create_comments_reader
from .files import Files
from . import body_xml, office_xml
from ..zips import open_zip
+_empty_result = results.success([])
+
+
def read(fileobj):
zip_file = open_zip(fileobj, "r")
body_readers = _body_readers(getattr(fileobj, "name", None), zip_file)
- return _read_notes(zip_file, body_readers).bind(lambda notes:
- _read_document(zip_file, body_readers, notes))
+ return results.combine([
+ _read_notes(zip_file, body_readers),
+ _read_comments(zip_file, body_readers),
+ ]).bind(lambda referents:
+ _read_document(zip_file, body_readers, notes=referents[0], comments=referents[1])
+ )
def _read_notes(zip_file, body_readers):
- empty_result = results.success([])
-
read_footnotes_xml = create_footnotes_reader(body_readers("footnotes"))
footnotes = _try_read_entry_or_default(
- zip_file, "word/footnotes.xml", read_footnotes_xml, default=empty_result)
+ zip_file, "word/footnotes.xml", read_footnotes_xml, default=_empty_result)
read_endnotes_xml = create_endnotes_reader(body_readers("endnotes"))
endnotes = _try_read_entry_or_default(
- zip_file, "word/endnotes.xml", read_endnotes_xml, default=empty_result)
+ zip_file, "word/endnotes.xml", read_endnotes_xml, default=_empty_result)
return results.combine([footnotes, endnotes]).map(lists.flatten)
+
+
+def _read_comments(zip_file, body_readers):
+ return _try_read_entry_or_default(
+ zip_file,
+ "word/comments.xml",
+ create_comments_reader(body_readers("comments")),
+ default=_empty_result,
+ )
+
-def _read_document(zip_file, body_readers, notes):
+def _read_document(zip_file, body_readers, notes, comments):
with zip_file.open("word/document.xml") as document_fileobj:
document_xml = office_xml.read(document_fileobj)
return read_document_xml_element(
document_xml,
body_reader=body_readers("document"),
notes=notes,
+ comments=comments,
)
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index fe75f7c..b1be2e5 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -52,13 +52,13 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
"office-word:wrap",
"v:shadow",
"v:shapetype",
+ "w:annotationRef",
"w:bookmarkEnd",
"w:sectPr",
"w:proofErr",
"w:lastRenderedPageBreak",
"w:commentRangeStart",
"w:commentRangeEnd",
- "w:commentReference",
"w:del",
"w:footnoteRef",
"w:endnoteRef",
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index 1c15001..e0d19a4 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -1,13 +1,21 @@
from .. import documents
-def read_document_xml_element(element,
+def read_document_xml_element(
+ element,
body_reader,
- notes=None):
+ notes=None,
+ comments=None):
if notes is None:
notes = []
+ if comments is None:
+ comments = []
body_element = element.find_child("w:body")
return body_reader.read_all(body_element.children) \
- .map(lambda children: documents.document(children, documents.notes(notes)))
+ .map(lambda children: documents.document(
+ children,
+ notes=documents.notes(notes),
+ comments=comments
+ ))
diff --git a/mammoth/options.py b/mammoth/options.py
index 3bc6eb7..d337ec2 100644
--- a/mammoth/options.py
+++ b/mammoth/options.py
@@ -46,6 +46,8 @@ p[style-name='footnote text'] => p
r[style-name='footnote reference'] =>
p[style-name='endnote text'] => p
r[style-name='endnote reference'] =>
+p[style-name='annotation text'] => p
+r[style-name='annotation reference'] =>
# LibreOffice
p[style-name='Footnote'] => p