From 65834fa39ee1ea77ef5581bc11e4e6046c6499a6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Mar 2019 21:20:58 +0000 Subject: Pass styles into read_numbering_xml_element --- mammoth/docx/__init__.py | 14 +++++++------- mammoth/docx/numbering_xml.py | 2 +- tests/docx/numbering_xml_tests.py | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 517683d..63a634c 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -142,13 +142,6 @@ def _part_with_body_reader(document_path, zip_file, part_paths): empty_content_types, ) - numbering = _try_read_entry_or_default( - zip_file, - part_paths.numbering, - read_numbering_xml_element, - default=Numbering(abstract_nums={}, nums={}), - ) - styles = _try_read_entry_or_default( zip_file, part_paths.styles, @@ -156,6 +149,13 @@ def _part_with_body_reader(document_path, zip_file, part_paths): Styles.EMPTY, ) + numbering = _try_read_entry_or_default( + zip_file, + part_paths.numbering, + lambda element: read_numbering_xml_element(element, styles=styles), + default=Numbering(abstract_nums={}, nums={}), + ) + def read_part(name, reader, default=_undefined): relationships = _read_relationships(zip_file, _find_relationships_path_for(name)) diff --git a/mammoth/docx/numbering_xml.py b/mammoth/docx/numbering_xml.py index 8d9af70..3a0645e 100644 --- a/mammoth/docx/numbering_xml.py +++ b/mammoth/docx/numbering_xml.py @@ -3,7 +3,7 @@ import cobble from ..documents import numbering_level -def read_numbering_xml_element(element): +def read_numbering_xml_element(element, styles): abstract_nums = _read_abstract_nums(element) nums = _read_nums(element) return Numbering(abstract_nums=abstract_nums, nums=nums) diff --git a/tests/docx/numbering_xml_tests.py b/tests/docx/numbering_xml_tests.py index 8fb59ff..467d406 100644 --- a/tests/docx/numbering_xml_tests.py +++ b/tests/docx/numbering_xml_tests.py @@ -2,11 +2,12 @@ from nose.tools import istest, assert_equal from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.numbering_xml import read_numbering_xml_element +from mammoth.docx.styles_xml import Styles @istest def find_level_returns_none_if_num_with_id_cannot_be_found(): - numbering = read_numbering_xml_element(xml_element("w:numbering")) + numbering = _read_numbering_xml_element(xml_element("w:numbering")) assert_equal(None, numbering.find_level("47", "0")) @@ -27,23 +28,30 @@ _sample_numbering_xml = xml_element("w:numbering", {}, [ @istest def level_includes_level_index(): - numbering = read_numbering_xml_element(_sample_numbering_xml) + numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal("0", numbering.find_level("47", "0").level_index) assert_equal("1", numbering.find_level("47", "1").level_index) @istest def list_is_not_ordered_if_formatted_as_bullet(): - numbering = read_numbering_xml_element(_sample_numbering_xml) + numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(False, numbering.find_level("47", "0").is_ordered) @istest def list_is_ordered_if_formatted_as_decimal(): - numbering = read_numbering_xml_element(_sample_numbering_xml) + numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(True, numbering.find_level("47", "1").is_ordered) @istest def find_level_returns_none_if_level_cannot_be_found(): - numbering = read_numbering_xml_element(_sample_numbering_xml) + numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(None, numbering.find_level("47", "2")) + + +def _read_numbering_xml_element(element, styles=None): + if styles is None: + styles = Styles.EMPTY + + return read_numbering_xml_element(element, styles=styles) -- cgit v1.2.3