Skip to content

Commit

Permalink
infra/gen-view: Solve links in all file types
Browse files Browse the repository at this point in the history
Previously, only links from labs were rerouted.
This commit updates the links from all file types, namely: guides,
tasks, questions, reading, and media.

Signed-off-by: Alex Apostolescu <[email protected]>
  • Loading branch information
Alex-deVis committed Nov 8, 2024
1 parent c60b0b4 commit 9d147e3
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions gen-view.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ def setup_overview(fileToLab: dict):

with open(f"{viewDir}/{c}-overview.md") as f:
text = f.read()
text = solve_links(text, fileToLab)
with open(f"{viewDir}/{c}-overview.md", "w") as f:
f.write(text)


def solve_links(text: str, fileToLab: dict) -> str:
def solve_links(filename: str, fileToLab: dict) -> str:
"""
Make relative links work in the final markdown file.
Expand All @@ -123,6 +122,9 @@ def solve_links(text: str, fileToLab: dict) -> str:
The lab number is determined by the fileToLab dictionary, and the subchapter is the first line of the file.
For example, [text](../reading/basic-syscall.md) will become [text](.view/lab1#basic-syscall).
"""
with open(filename) as f:
text = f.read()

# Questions from the same chapter are at Questions/<question>, without the .md extension
text = re.sub(r"(\[.*\])\(.*questions/(.*)\.md\)", r"\1(Questions/\2)", text)

Expand Down Expand Up @@ -156,7 +158,7 @@ def solve_links(text: str, fileToLab: dict) -> str:
title = f.readline().strip("#").replace("`", "").replace(":", "")
subchapter = prefix + hypenate(title)
except:
print(f"Error: Could not solve link to {filepath}")
print(f"Error: Could not solve link to {filepath} for {filename}")
continue

text = re.sub(
Expand All @@ -165,18 +167,20 @@ def solve_links(text: str, fileToLab: dict) -> str:
text,
)

return text
with open(filename, "w") as f:
f.write(text)


class Lab:
def __init__(self, title: str, filename: str, content: List[str]):
self.title = title
self.filename = filename

self.text = f"# {title}\n\n"
for file in content:
self.process_file(file)

print(f"Generating lab {viewDir}/{filename}")
with open(f"{viewDir}/{filename}", "w") as f:
f.write(self.text)

def process_file(self, filename: str):
"""
Process a file and add it to the lab text.
Expand All @@ -199,29 +203,16 @@ def process_file(self, filename: str):
filecontent = re.sub(r"^(#+)", r"\1#", filecontent, flags=re.MULTILINE)
self.text += filecontent + "\n\n"

def generate(self, fileToLab: dict):
"""
Generate the final markdown file for the lab.
"""
print(f"Generating lab {viewDir}/{self.filename}")

self.text = solve_links(self.text, fileToLab)

with open(f"{viewDir}/{self.filename}", "w") as f:
f.write(self.text)


class ConfigParser:
def __init__(self, path):
self.fileToLab = None
with open(path) as f:
self.data = yaml.safe_load(f)

def create_labs(self) -> List[Lab]:
labs = []
def create_labs(self):
for entry in self.data["lab_structure"]:
labs.append(Lab(entry["title"], entry["filename"], entry["content"]))
return labs
Lab(entry["title"], entry["filename"], entry["content"])

def get_file_to_lab_dict(self) -> dict:
"""
Expand Down Expand Up @@ -258,13 +249,17 @@ def main():

# Parse the config file
config = ConfigParser("config.yaml")
labs = config.create_labs()
for lab in labs:
lab.generate(config.get_file_to_lab_dict())
config.create_labs()

# Copy the overview.md file for each chapter to the .view directory
setup_overview(config.get_file_to_lab_dict())

# Solve links recursively in all markdown files
for root, _, files in os.walk(viewDir):
for f in files:
if f.endswith(".md"):
solve_links(os.path.join(root, f), config.get_file_to_lab_dict())


if __name__ == "__main__":
main()

0 comments on commit 9d147e3

Please sign in to comment.