Skip to content

Commit

Permalink
Parse run highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent b5e5ea1 commit 0d5b276
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.8.0

* Support parsing of run highlights.

# 1.7.1

* Switch the precedence of numbering properties in paragraph properties and the
Expand Down
3 changes: 3 additions & 0 deletions mammoth/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Run(HasChildren):
vertical_alignment = cobble.field()
font = cobble.field()
font_size = cobble.field()
highlight = cobble.field()

@cobble.data
class Text(Element):
Expand Down Expand Up @@ -128,6 +129,7 @@ def run(
vertical_alignment=None,
font=None,
font_size=None,
highlight=None,
):
if vertical_alignment is None:
vertical_alignment = VerticalAlignment.baseline
Expand All @@ -144,6 +146,7 @@ def run(
vertical_alignment=vertical_alignment,
font=font,
font_size=font_size,
highlight=highlight,
)

class VerticalAlignment(object):
Expand Down
2 changes: 2 additions & 0 deletions mammoth/docx/body_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def run(element):
is_strikethrough = read_boolean_element(properties.find_child("w:strike"))
is_all_caps = read_boolean_element(properties.find_child("w:caps"))
is_small_caps = read_boolean_element(properties.find_child("w:smallCaps"))
highlight = properties.find_child_or_null("w:highlight").attributes.get("w:val")

def add_complex_field_hyperlink(children):
hyperlink_kwargs = current_hyperlink_kwargs()
Expand All @@ -126,6 +127,7 @@ def add_complex_field_hyperlink(children):
vertical_alignment=vertical_alignment,
font=font,
font_size=font_size,
highlight=highlight,
))

def _read_run_style(properties):
Expand Down
9 changes: 9 additions & 0 deletions tests/docx/body_xml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ def test_run_with_invalid_w_sz_has_none_font_size(self):
run = self._read_run_with_properties([font_size_xml])
assert_equal(None, run.font_size)

def test_run_has_no_highlight_by_default(self):
run = self._read_run_with_properties([])
assert_equal(None, run.highlight)

def test_run_has_highlight_read_from_properties(self):
highlight_xml = xml_element("w:highlight", {"w:val": "yellow"})
run = self._read_run_with_properties([highlight_xml])
assert_equal("yellow", run.highlight)

def _read_run_with_properties(self, properties, styles=None):
properties_xml = xml_element("w:rPr", {}, properties)
run_xml = xml_element("w:r", {}, [properties_xml])
Expand Down

0 comments on commit 0d5b276

Please sign in to comment.