Skip to content

Commit

Permalink
add duplicate check to toctree_fix function (useblocks#92)
Browse files Browse the repository at this point in the history
* add duplicate check to toctree_fix function

* correct placement of duplicate headline fix

* replace .strip with .replace

* reformatting of duplicate check
  • Loading branch information
mawieland authored Nov 14, 2023
1 parent 5991ed4 commit 87cfef0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sphinx_simplepdf/builders/simplepdf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter
import os
import re
from typing import Any, Dict
Expand Down Expand Up @@ -172,6 +173,37 @@ def _toctree_fix(self, html):
for link in links:
link["href"] = link["href"].replace(f"{self.app.config.root_doc}.html", "")

# search for duplicates
counts = dict(Counter([str(x).split(">")[0] for x in links]))
duplicates = {key: value for key, value in counts.items() if value > 1}

if duplicates:
print("found duplicate references in toctree attempting to fix")

for text, counter in duplicates.items():

ref = re.findall("href=\"#.*\"", str(text))

# clean href data for searching
cleaned_ref_toc = ref[0].replace("href=\"", "").replace("\"", "") # "#target"
cleaned_ref_target = ref[0].replace("href=\"#", "").replace("\"", "") # "target"

occurences = soup.find_all('section', attrs={"id": cleaned_ref_target})

# rename duplicate references, relies on fact -> order in toc is order of occurence in document
replace_counter = 0

for link in links:
if link["href"] == cleaned_ref_toc:
# edit reference in table of content
link["href"] = link["href"] + "-" + str(replace_counter + 1)

# edit target reference
occurences[replace_counter]["id"] = occurences[replace_counter]["id"] + "-" + str(
replace_counter + 1)

replace_counter += 1

for heading_tag in ["h1", "h2"]:
headings = soup.find_all(heading_tag, class_="")
for number, heading in enumerate(headings):
Expand Down

0 comments on commit 87cfef0

Please sign in to comment.