CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
weijie-chen

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: weijie-chen/Linear-Algebra-With-Python
Path: blob/master/scripts/link_to_mysite.py
Views: 449
1
import re
2
import sys
3
from pathlib import Path
4
5
# Define the base URL for the chapters
6
base_url = "https://www.weijiechen.com/linear-algebra-with-python-book/qmd/"
7
8
# Function to generate the correct URL for each chapter
9
def generate_url(chapter_name):
10
chapter_slug = chapter_name.replace(" ", "%20").replace(",", "%2C")
11
return f"{base_url}{chapter_slug}.html"
12
13
# Function to update links in the Markdown file
14
def update_links_in_markdown(file_path):
15
# Ensure the file path is a Path object
16
file_path = Path(file_path)
17
18
if not file_path.is_file():
19
print(f"Error: The file {file_path} does not exist.")
20
return
21
22
# Read the original file
23
with file_path.open('r', encoding='utf-8') as file:
24
content = file.read()
25
26
# Regular expression to find all links with chapters
27
pattern = re.compile(r'\[([^\]]+)\]\((https://nbviewer\.org/github/weijie-chen/Linear-Algebra-With-Python/blob/master/notebooks/[^)]+)\)')
28
29
def replace_link(match):
30
chapter_name = match.group(1)
31
return f"[{chapter_name}]({generate_url(chapter_name)})"
32
33
# Replace the links with the correct format
34
updated_content = pattern.sub(replace_link, content)
35
36
# Write the updated content back to the file
37
with file_path.open('w', encoding='utf-8') as file:
38
file.write(updated_content)
39
40
print(f"Links in {file_path} have been updated.")
41
42
# Main function to handle command-line arguments
43
if __name__ == "__main__":
44
# Path to README.md, assuming script is in 'scripts' directory
45
script_dir = Path(__file__).parent
46
markdown_file_path = script_dir.parent / 'README.md'
47
48
# Debug print to confirm path
49
print(f"Processing file: {markdown_file_path}")
50
51
update_links_in_markdown(markdown_file_path)
52
53