aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--tests/mammoth_tests.py16
-rwxr-xr-xtests/test-data/comments.docxbin0 -> 15526 bytes
6 files changed, 54 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
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index 201df70..8ad9b33 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -185,6 +185,22 @@ def relationships_are_handled_properly_in_footnotes():
@istest
+def when_style_mapping_is_defined_for_comment_references_then_comments_are_included():
+ expected_html = (
+ '<p>Ouch' +
+ '<sup><a href="#doc-42-comment-0" id="doc-42-comment-ref-0">[MW 1]</a></sup>.' +
+ '<sup><a href="#doc-42-comment-2" id="doc-42-comment-ref-2">[MW 2]</a></sup></p>' +
+ '<dl><dt id="doc-42-comment-0">MW 1</dt><dd><p>A tachyon walks into a bar. <a href="#doc-42-comment-ref-0">↑</a></p></dd>' +
+ '<dt id="doc-42-comment-2">MW 2</dt><dd><p>Fin. <a href="#doc-42-comment-ref-2">↑</a></p></dd></dl>'
+ )
+
+ with open(test_path("comments.docx"), "rb") as fileobj:
+ result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42-", style_map="comment-reference => sup")
+ assert_equal([], result.messages)
+ assert_equal(expected_html, result.value)
+
+
+@istest
def text_boxes_are_read():
with open(test_path("text-box.docx"), "rb") as fileobj:
result = mammoth.convert_to_html(fileobj=fileobj)
diff --git a/tests/test-data/comments.docx b/tests/test-data/comments.docx
new file mode 100755
index 0000000..32cdae1
--- /dev/null
+++ b/tests/test-data/comments.docx
Binary files differ