Skip to content

Commit

Permalink
Merge pull request #556 from ColonelThirtyTwo/fix-bookmark-spaces
Browse files Browse the repository at this point in the history
Fix evidence refs with spaces
  • Loading branch information
chrismaddalena authored Nov 26, 2024
2 parents 311182d + bcd94c2 commit c0860f6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [4.3.7] - 25 November 2024

#### Fixed

* Fixed forms not accepting decimal values for extra fields (PR #554)
* Fixed cross-references not working when the reference name contained spaces (PR #556)

## [4.3.6] - 14 November 2024

### Added
Expand Down
4 changes: 2 additions & 2 deletions VERSION
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
v4.3.6
14 November 2024
v4.3.7
25 November 2024
4 changes: 2 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# 3rd Party Libraries
import environ

__version__ = "4.3.6"
__version__ = "4.3.7"
VERSION = __version__
RELEASE_DATE = "14 November 2024"
RELEASE_DATE = "25 November 2024"

ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
APPS_DIR = ROOT_DIR / "ghostwriter"
Expand Down
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 c0860f6

Please sign in to comment.