From 8dce33362f5338953ed2a614e736c197e36b9984 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Jul 2016 12:31:50 +0100 Subject: Read comment references --- mammoth/conversion.py | 3 +++ mammoth/documents.py | 6 ++++++ mammoth/docx/body_xml.py | 4 ++++ tests/docx/body_xml_tests.py | 6 ++++++ 4 files changed, 19 insertions(+) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index e05cccc..2262392 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -201,6 +201,9 @@ class _DocumentConverter(documents.ElementVisitor): self._html_generator.append(note_generator) self._html_generator.end() + def visit_comment_reference(self, reference): + return [] + def _visit_all(self, elements): return lists.flat_map(self.visit, elements) diff --git a/mammoth/documents.py b/mammoth/documents.py index 93b080c..5d9830e 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -182,4 +182,10 @@ class Comment(object): comment = Comment +@cobble.data +class CommentReference(Element): + comment_id = cobble.field() + +comment_reference = CommentReference + ElementVisitor = cobble.visitor(Element) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index c493c4b..fe75f7c 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -334,6 +334,9 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return note_reference + def read_comment_reference(element): + return _success(documents.comment_reference(element.attributes["w:id"])) + def alternate_content(element): return read_child_elements(element.find_child("mc:Fallback")) @@ -364,6 +367,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f "v:imagedata": read_imagedata, "w:footnoteReference": note_reference_reader("footnote"), "w:endnoteReference": note_reference_reader("endnote"), + "w:commentReference": read_comment_reference, "mc:AlternateContent": alternate_content, "w:sdt": read_sdt } diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index e35110f..c56cfc5 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -576,6 +576,12 @@ class ReadXmlElementTests(object): footnote = _read_and_get_document_xml_element(footnote_xml) assert_equal("4", footnote.note_id) + @istest + def comment_reference_has_id_read(self): + comment_reference_xml = xml_element("w:commentReference", {"w:id": "4"}) + comment_reference = _read_and_get_document_xml_element(comment_reference_xml) + assert_equal(documents.CommentReference("4"), comment_reference) + @istest def ignored_elements_are_ignored_without_message(self): element = xml_element("w:bookmarkEnd") -- cgit v1.2.3