Skip to content

Commit

Permalink
Fix evidence refs with spaces
Browse files Browse the repository at this point in the history
For some reason, MS Word ignores bookmarks with spaces in them. I can't
find any restrictions on bookmark names in the spec. This patch replaces
spaces with underscores.

This also adds some bookmark ID tracking rather than using ID "0" for
every bookmark.
  • Loading branch information
ColonelThirtyTwo committed Nov 25, 2024
1 parent 311182d commit 6091daa
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions ghostwriter/modules/reportwriter/richtext/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ def _tag_h(self, el, **kwargs):
run = heading_paragraph.runs[0]
tag = run._r
start = docx.oxml.shared.OxmlElement("w:bookmarkStart")
start.set(docx.oxml.ns.qn("w:id"), "0")
start.set(docx.oxml.ns.qn("w:id"), str(self.current_bookmark_id))
start.set(docx.oxml.ns.qn("w:name"), el.attrs["id"])
tag.append(start)
end = docx.oxml.shared.OxmlElement("w:bookmarkEnd")
end.set(docx.oxml.ns.qn("w:id"), "0")
end.set(docx.oxml.ns.qn("w:id"), str(self.current_bookmark_id))
end.set(docx.oxml.ns.qn("w:name"), el.attrs["id"])
tag.append(end)
self.current_bookmark_id += 1

tag_h1 = _tag_h
tag_h2 = _tag_h
Expand Down Expand Up @@ -296,6 +297,7 @@ def __init__(
self.title_case_exceptions = title_case_exceptions
self.border_color_width = border_color_width
self.plural_acronym_pattern = re.compile(r"^[^a-z]+(:?s|'s)$")
self.current_bookmark_id = 1000 # Hopefully won't conflict with templates

def text(self, el, *, par=None, **kwargs):
if par is not None and getattr(par, "_gw_is_caption", False):
Expand Down Expand Up @@ -374,8 +376,8 @@ def make_caption(self, par, label: str, ref: str | None = None):
# Start a bookmark run with the figure label
p = par._p
bookmark_start = OxmlElement("w:bookmarkStart")
bookmark_start.set(qn("w:name"), ref)
bookmark_start.set(qn("w:id"), "0")
bookmark_start.set(qn("w:name"), ref.replace(" ", "_"))
bookmark_start.set(qn("w:id"), str(self.current_bookmark_id))
p.append(bookmark_start)

# Add the figure label
Expand Down Expand Up @@ -414,9 +416,11 @@ def make_caption(self, par, label: str, ref: str | None = None):
# End the bookmark after the number
p = par._p
bookmark_end = OxmlElement("w:bookmarkEnd")
bookmark_end.set(qn("w:id"), "0")
bookmark_end.set(qn("w:id"), str(self.current_bookmark_id))
p.append(bookmark_end)

self.current_bookmark_id += 1

def make_evidence(self, par, evidence):
file_path = settings.MEDIA_ROOT + "/" + evidence["path"]
if not os.path.exists(file_path):
Expand Down Expand Up @@ -528,7 +532,7 @@ def make_cross_ref(self, par, ref: str):
run = par.add_run()
r = run._r
instrText = OxmlElement("w:instrText")
instrText.text = ' REF "_Ref{}" \\h '.format(ref.replace("\\", "\\\\").replace('"', '\\"'))
instrText.text = ' REF "_Ref{}" \\h '.format(ref.replace("\\", "\\\\").replace('"', '\\"').replace(" ", "_"))
r.append(instrText)

# An optional ``separate`` value to enforce a space between label and number
Expand Down

0 comments on commit 6091daa

Please sign in to comment.