Skip to content
Open
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
24 changes: 24 additions & 0 deletions sphinx_simplepdf/builders/simplepdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ def finish(self) -> None:
if (n == retries - 1) and not success:
raise RuntimeError(f"maximum number of retries {retries} failed in weasyprint")

"""
Fixes a link that refers to a sub-headline.

For instance the broken link:
#headline#sub-headline#sub-sub-headline
Will be fixed to:
#sub-sub-headline

If the link is not a sub-headline, the link isn't modified

Args:
link_to_fix: The link to fix

Returns: If no modification is needed, link_to_fix otherwise the fixed link
"""
def fix_link(self, link_to_fix):
pos = link_to_fix.rfind("#")
if pos != -1:
# In case we only find one #, this will still work as it won't affect the link
link_to_fix = link_to_fix [pos::]
return link_to_fix

"""
attempts to fix cases where a document has multiple chapters that have the same name.

Expand Down Expand Up @@ -218,6 +240,8 @@ def _toctree_fix(self, html):
# remove document file reference
for toc_link in toc_links:
toc_link["href"] = toc_link["href"].replace(f"{self.app.config.root_doc}.html", "")
# In case we link to a sub-heading, fix the link, if it doesn't need fixing, nothing is done
toc_link["href"] = self.fix_link(toc_link["href"])

# search for duplicates
counts = dict(Counter([str(x).split(">")[0] for x in toc_links]))
Expand Down