aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mammoth/documents.py7
-rw-r--r--mammoth/docx/comments_xml.py19
-rw-r--r--tests/docx/comments_xml_tests.py18
3 files changed, 44 insertions, 0 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py
index 36e3458..ea376d4 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -173,4 +173,11 @@ class NoteReference(Element):
note_reference = NoteReference
+@cobble.data
+class Comment(object):
+ comment_id = cobble.field()
+ body = cobble.field()
+
+comment = Comment
+
ElementVisitor = cobble.visitor(Element)
diff --git a/mammoth/docx/comments_xml.py b/mammoth/docx/comments_xml.py
new file mode 100644
index 0000000..9a92552
--- /dev/null
+++ b/mammoth/docx/comments_xml.py
@@ -0,0 +1,19 @@
+from .. import lists
+from .. import documents
+from .. import results
+
+
+def create_comments_reader(body_reader):
+ def read_comments_xml_element(element):
+ comment_elements = element.find_children("w:comment")
+ return results.combine(lists.map(_read_comment_element, comment_elements))
+
+
+ def _read_comment_element(element):
+ return body_reader.read_all(element.children).map(lambda body:
+ documents.comment(
+ comment_id=element.attributes["w:id"],
+ body=body
+ ))
+
+ return read_comments_xml_element
diff --git a/tests/docx/comments_xml_tests.py b/tests/docx/comments_xml_tests.py
new file mode 100644
index 0000000..2970922
--- /dev/null
+++ b/tests/docx/comments_xml_tests.py
@@ -0,0 +1,18 @@
+from nose.tools import istest, assert_equal
+
+from mammoth import documents
+from mammoth.docx.xmlparser import element as xml_element
+from mammoth.docx.comments_xml import create_comments_reader
+from mammoth.docx import body_xml
+
+
+@istest
+def id_and_body_of_comment_is_read():
+ body = [xml_element("w:p")]
+ comments = create_comments_reader(body_xml.reader())(xml_element("w:comments", {}, [
+ xml_element("w:comment", {"w:id": "1"}, body),
+ ]))
+ assert_equal(1, len(comments.value))
+ assert_equal(comments.value[0].body, [documents.paragraph(children=[])])
+ assert_equal("1", comments.value[0].comment_id)
+