Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error for documents without ListParagraph style #518

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions ghostwriter/modules/reportwriter/richtext/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from lxml import etree

# Ghostwriter Libraries
from ghostwriter.modules.reportwriter.base import ReportExportError
from ghostwriter.modules.reportwriter.extensions import (
IMAGE_EXTENSIONS,
TEXT_EXTENSIONS,
Expand Down Expand Up @@ -69,7 +70,10 @@ def text(self, el, *, par=None, style={}, **kwargs):
run._r.append(hyperlink)
# A workaround for the lack of a hyperlink style
if "Hyperlink" in self.doc.styles:
run.style = "Hyperlink"
try:
run.style = "Hyperlink"
except KeyError:
pass
else:
run.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
run.font.underline = True
Expand All @@ -79,7 +83,10 @@ def text(self, el, *, par=None, style={}, **kwargs):
def style_run(self, run, style):
super().style_run(run, style)
if style.get("inline_code"):
run.style = "CodeInline"
try:
run.style = "CodeInline"
except KeyError:
pass
run.font.no_proof = True
if style.get("highlight"):
run.font.highlight_color = WD_COLOR_INDEX.YELLOW
Expand Down Expand Up @@ -209,7 +216,7 @@ def tag_ul(self, el, *, par=None, list_level=None, list_tracking=None, **kwargs)

tag_ol = tag_ul

def tag_blockquote(self, el, **kwargs):
def tag_blockquote(self, el, par=None, **kwargs):
# TODO: if done in a list, this won't preserve the level.
# Not sure how to do that, since this requires a new paragraph.
par = self.doc.add_paragraph()
Expand Down Expand Up @@ -296,7 +303,10 @@ def tag_span(self, el, *, par, **kwargs):
self.make_evidence(par, evidence)
elif "data-gw-caption" in el.attrs:
ref_name = el.attrs["data-gw-caption"]
par.style = "Caption"
try:
par.style = "Caption"
except KeyError:
pass
par._gw_is_caption = True
self.make_figure(par, ref_name or None)
elif "data-gw-ref" in el.attrs:
Expand Down Expand Up @@ -523,7 +533,10 @@ def create(self, doc):
level_list_is_ordered = self.level_list_is_ordered

# Create a new numbering
numbering = doc.part.numbering_part.numbering_definitions._numbering
try:
numbering = doc.part.numbering_part.numbering_definitions._numbering
except NotImplementedError as e:
raise ReportExportError("Tried to use a list in a template without list styles") from e
last_used_id = max(
(int(id) for id in numbering.xpath("w:abstractNum/@w:abstractNumId")),
default=-1,
Expand Down Expand Up @@ -580,6 +593,9 @@ def create(self, doc):
numbering_id = numbering.add_num(abstract_numbering_id).numId

for par, level in self.paragraphs:
par.style = "ListParagraph"
try:
par.style = "ListParagraph"
except KeyError:
pass
par._p.get_or_add_pPr().get_or_add_numPr().get_or_add_numId().val = numbering_id
par._p.get_or_add_pPr().get_or_add_numPr().get_or_add_ilvl().val = level