Skip to content

Commit

Permalink
Improve jumplinks in index
Browse files Browse the repository at this point in the history
  • Loading branch information
arlowatts committed Feb 28, 2024
1 parent 9b01136 commit bdbe18b
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions worldbuilding/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ def main():

content += "- " + key + "\n"

for entry in sorted(index[key]):
content += " - " + entry + "\n"
for path in sorted(index[key]):
title = getFileTitle(path)

if title in index[key][path]:
content += " - " + index[key][path][title] + "\n"
del index[key][path][title]
else:
content += " - " + title + "\n"

for title in index[key][path]:
content += " - " + index[key][path][title] + "\n"

with open("index.md", "w") as file:
file.write(content)
Expand All @@ -45,25 +54,40 @@ def read(dirname: str, index: dict):
def getFileEntries(path: str, index: dict):
with open(path) as file:
# split the file along bold indicators (two or more *)
content = re.split("(\*\*+)", file.read())
content = re.split("\*\*+", file.read())

headerTitle = getFileTitle(path)
urlKey = ""

# index the phrases inside the bold indicators, which are all odd-indexed
# also track the last seen header in urlKey for jumplinking
for i in range(len(content)):
# if the current phrase is bolded, index it
if (i % 2 != 0):
key = content[i].strip()

# add the phrase to the index if it isn't there already
if not key in index:
index[key] = {}

# index the phrases inside the bold indicators
# every fourth element in the split file is in bold
for i in range(2, len(content), 4):
key = content[i].strip()
urlKey = "index-" + re.sub("[^0-9a-zA-Z]", "-", key.lower())
# add the current path to the phrase if it isn't there already
if not path in index[key]:
index[key][path] = {}

if not key in index:
index[key] = []
# assemble the jumplink to the current location
link = "[" + headerTitle + "](" + path + urlKey + ")"

link = "[" + getFileTitle(path) + "](" + path + "#" + urlKey + ")"
if not link in index[key][path]:
index[key][path][headerTitle] = link

if not link in index[key]:
index[key].append(link)
content[i] = "<span id=\"" + urlKey + "\">" + content[i] + "</span>"
# otherwise, check the phrase for the latest title
else:
titleIndex = content[i].rfind("# ")

with open(path, "w") as file:
file.write("".join(content))
# if a title is found, get the name of the title and format it
if (titleIndex > 0):
headerTitle = content[i][titleIndex + 1 : content[i].find("\n", titleIndex)].strip()
urlKey = "#" + re.sub("[^0-9a-zA-Z\-]", "", re.sub(" ", "-", headerTitle.lower()))

# get the title from the contents of the file
# if no title is defined in the file, return the filename
Expand Down

0 comments on commit bdbe18b

Please sign in to comment.