From 6752668de501328c25d59acdf9a44e25f76ac2ca Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 8 Jun 2017 19:26:10 +0100 Subject: Support non breaking hyphen elements --- NEWS | 4 ++++ mammoth/docx/body_xml.py | 9 +++++++++ tests/docx/body_xml_tests.py | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/NEWS b/NEWS index dcda33c..12dc364 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +# 1.4.1 + +* Read w:noBreakHyphen elements as non-breaking hyphen characters. + # 1.4.0 * Extract the default data URI image converter to the images module. diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index a087191..db3da4e 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -1,5 +1,6 @@ import contextlib import re +import sys from .. import documents from .. import results @@ -9,6 +10,9 @@ from .xmlparser import node_types, XmlElement from .styles_xml import Styles from .uris import replace_fragment, uri_to_zip_entry_name +if sys.version_info >= (3, ): + unichr = chr + def reader( numbering=None, @@ -202,6 +206,10 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return _success(documents.tab()) + def no_break_hyphen(element): + return _success(documents.text(unichr(0x2011))) + + def table(element): return _read_xml_elements(element.children) \ .flat_map(calculate_row_spans) \ @@ -432,6 +440,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f "w:fldChar": read_fld_char, "w:instrText": read_instr_text, "w:tab": tab, + "w:noBreakHyphen": no_break_hyphen, "w:tbl": table, "w:tr": table_row, "w:tc": table_cell, diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 98e93ed..a701978 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -1,4 +1,5 @@ import io +import sys from precisely import assert_that, is_sequence from nose.tools import istest, assert_equal @@ -21,6 +22,9 @@ from .document_matchers import ( is_row, ) +if sys.version_info >= (3, ): + unichr = chr + @istest def text_from_text_element_is_read(): @@ -445,6 +449,13 @@ def can_read_tab_element(): element = xml_element("w:tab") tab = _read_and_get_document_xml_element(element) assert_equal(documents.tab(), tab) + + +@istest +def no_break_hyphen_element_is_read_as_non_breaking_hyphen_character(): + element = xml_element("w:noBreakHyphen") + tab = _read_and_get_document_xml_element(element) + assert_equal(documents.text(unichr(0x2011)), tab) @istest -- cgit v1.2.3