From 92b00aeb4c1d0ab49507cbd4ad9a25e30a1ac724 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Jul 2016 15:32:20 +0100 Subject: Handle comments without initials --- mammoth/conversion.py | 8 ++++++-- tests/conversion_tests.py | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index b449dd6..b40b0e2 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -214,14 +214,13 @@ class _DocumentConverter(documents.ElementVisitor): comment = self._comments[reference.comment_id] self._comment_count_by_initials[comment.author_initials] += 1 count = self._comment_count_by_initials[comment.author_initials] - label = "{0} {1}".format(comment.author_initials, count) + label = "{0} {1}".format(_comment_author_label(comment), count) self._referenced_comments.append((label, comment)) return [ # TODO: remove duplication with note references html.element("a", { "href": "#" + self._referent_html_id("comment", reference.comment_id), "id": self._reference_html_id("comment", reference.comment_id), - # TODO: handle comments without initials }, [html.text("[{0}]".format(label))]) ] @@ -315,4 +314,9 @@ def _document_matcher_matches(matcher, element, element_type): ) ) + +def _comment_author_label(comment): + return comment.author_initials or "Comment" + + _up_arrow = "↑" diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 300d1a9..2aec1c1 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -7,7 +7,7 @@ import io from nose.tools import istest, assert_equal from mammoth import documents, style_reader, results, html -from mammoth.conversion import convert_document_element_to_html +from mammoth.conversion import convert_document_element_to_html, _comment_author_label from mammoth.docx.xmlparser import parse_xml @@ -475,6 +475,24 @@ def comment_references_are_linked_to_comment_after_main_body(): assert_equal(expected_html, result.value) +@istest +def when_initials_are_not_blank_then_comment_author_label_is_initials(): + assert_equal("TP", _comment_author_label(documents.comment( + comment_id="0", + body=[], + author_initials="TP", + ))) + + +@istest +def when_initials_are_blank_then_comment_author_label_is_comment(): + assert_equal("Comment", _comment_author_label(documents.comment( + comment_id="0", + body=[], + author_initials=None, + ))) + + def _paragraph_with_text(text): return documents.paragraph(children=[_run_with_text(text)]) -- cgit v1.2.3