aboutsummaryrefslogtreecommitdiff
path: root/mammoth/docx/numbering_xml.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2019-03-17 18:20:17 +0000
committerMichael Williamson <mike@zwobble.org>2019-03-17 18:20:17 +0000
commit6d5fe54cd6183d12c0e984d9d0d7e3204817583f (patch)
treee7d25ad8d8307bb06b333e897488cd137dad19af /mammoth/docx/numbering_xml.py
parent20c1fea1a37ac39f3114775facad74644435253e (diff)
Defer resolution of abstract nums until necessary
Diffstat (limited to 'mammoth/docx/numbering_xml.py')
-rw-r--r--mammoth/docx/numbering_xml.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/mammoth/docx/numbering_xml.py b/mammoth/docx/numbering_xml.py
index f7f98e0..31a0d3c 100644
--- a/mammoth/docx/numbering_xml.py
+++ b/mammoth/docx/numbering_xml.py
@@ -1,10 +1,12 @@
+import cobble
+
from ..documents import numbering_level
def read_numbering_xml_element(element):
abstract_nums = _read_abstract_nums(element)
- nums = _read_nums(element, abstract_nums)
- return Numbering(nums)
+ nums = _read_nums(element)
+ return Numbering(abstract_nums=abstract_nums, nums=nums)
def _read_abstract_nums(element):
@@ -33,27 +35,34 @@ def _read_abstract_num_level(element):
return numbering_level(level_index, is_ordered)
-def _read_nums(element, abstract_nums):
+def _read_nums(element):
num_elements = element.find_children("w:num")
return dict(
- _read_num(num_element, abstract_nums)
+ _read_num(num_element)
for num_element in num_elements
)
-def _read_num(element, abstract_nums):
+def _read_num(element):
num_id = element.attributes.get("w:numId")
abstract_num_id = element.find_child_or_null("w:abstractNumId").attributes["w:val"]
- return num_id, abstract_nums[abstract_num_id]
+ return num_id, _Num(abstract_num_id=abstract_num_id)
+
+
+@cobble.data
+class _Num(object):
+ abstract_num_id = cobble.field()
class Numbering(object):
- def __init__(self, nums):
+ def __init__(self, abstract_nums, nums):
+ self._abstract_nums = abstract_nums
self._nums = nums
-
+
def find_level(self, num_id, level):
num = self._nums.get(num_id)
if num is None:
return None
else:
- return num.get(level)
+ abstract_num = self._abstract_nums[num.abstract_num_id]
+ return abstract_num.get(level)