Skip to content

Commit

Permalink
Prefer numbering properties to numbering from paragraph style
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Mar 20, 2024
1 parent 74d3543 commit 19a8412
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.7.1

* Switch the precedence of numbering properties in paragraph properties and the
numbering in paragraph styles so that the numbering properties in paragraph
properties takes precedence.

# 1.7.0

* Support attributes in HTML paths in style mappings.
Expand Down
12 changes: 6 additions & 6 deletions mammoth/docx/body_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ def _undefined_style_warning(style_type, style_id):
return results.warning("{0} style with ID {1} was referenced but not defined in the document".format(style_type, style_id))

def _read_numbering_properties(paragraph_style_id, element):
num_id = element.find_child_or_null("w:numId").attributes.get("w:val")
level_index = element.find_child_or_null("w:ilvl").attributes.get("w:val")
if num_id is not None and level_index is not None:
return numbering.find_level(num_id, level_index)

if paragraph_style_id is not None:
level = numbering.find_level_by_paragraph_style_id(paragraph_style_id)
if level is not None:
return level

num_id = element.find_child_or_null("w:numId").attributes.get("w:val")
level_index = element.find_child_or_null("w:ilvl").attributes.get("w:val")
if num_id is None or level_index is None:
return None
else:
return numbering.find_level(num_id, level_index)
return None

def _read_paragraph_indent(element):
attributes = element.attributes
Expand Down
21 changes: 17 additions & 4 deletions tests/docx/body_xml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ def test_paragraph_has_numbering_properties_from_paragraph_properties_if_present
assert_equal("1", paragraph.numbering.level_index)
assert_equal(True, paragraph.numbering.is_ordered)

def test_numbering_on_paragraph_style_takes_precedence_over_numpr(self):
def test_paragraph_has_numbering_from_paragraph_style_if_present(self):
properties_xml = xml_element("w:pPr", {}, [
xml_element("w:pStyle", {"w:val": "List"}),
])
paragraph_xml = xml_element("w:p", {}, [properties_xml])

numbering = _NumberingMap(
levels_by_paragraph_style_id={"List": documents.numbering_level("1", True)}
)
paragraph = _read_and_get_document_xml_element(paragraph_xml, numbering=numbering)

assert_equal(True, paragraph.numbering.is_ordered)

def test_numbering_properties_in_paragraph_properties_takes_precedence_over_numbering_in_paragraph_style(self):
numbering_properties_xml = xml_element("w:numPr", {}, [
xml_element("w:ilvl", {"w:val": "1"}),
xml_element("w:numId", {"w:val": "42"}),
Expand All @@ -117,12 +130,12 @@ def test_numbering_on_paragraph_style_takes_precedence_over_numpr(self):
paragraph_xml = xml_element("w:p", {}, [properties_xml])

numbering = _NumberingMap(
nums={"42": {"1": documents.numbering_level("1", False)}},
levels_by_paragraph_style_id={"List": documents.numbering_level("1", True)}
nums={"42": {"1": documents.numbering_level("1", True)}},
levels_by_paragraph_style_id={"List": documents.numbering_level("2", True)}
)
paragraph = _read_and_get_document_xml_element(paragraph_xml, numbering=numbering)

assert_equal(True, paragraph.numbering.is_ordered)
assert_equal("1", paragraph.numbering.level_index)

def test_numbering_properties_are_ignored_if_lvl_is_missing(self):
paragraph_xml = self._paragraph_with_numbering_properties([
Expand Down

0 comments on commit 19a8412

Please sign in to comment.