Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/scripts/link_to_mysite.py
Views: 449
import re1import sys2from pathlib import Path34# Define the base URL for the chapters5base_url = "https://www.weijiechen.com/linear-algebra-with-python-book/qmd/"67# Function to generate the correct URL for each chapter8def generate_url(chapter_name):9chapter_slug = chapter_name.replace(" ", "%20").replace(",", "%2C")10return f"{base_url}{chapter_slug}.html"1112# Function to update links in the Markdown file13def update_links_in_markdown(file_path):14# Ensure the file path is a Path object15file_path = Path(file_path)1617if not file_path.is_file():18print(f"Error: The file {file_path} does not exist.")19return2021# Read the original file22with file_path.open('r', encoding='utf-8') as file:23content = file.read()2425# Regular expression to find all links with chapters26pattern = re.compile(r'\[([^\]]+)\]\((https://nbviewer\.org/github/weijie-chen/Linear-Algebra-With-Python/blob/master/notebooks/[^)]+)\)')2728def replace_link(match):29chapter_name = match.group(1)30return f"[{chapter_name}]({generate_url(chapter_name)})"3132# Replace the links with the correct format33updated_content = pattern.sub(replace_link, content)3435# Write the updated content back to the file36with file_path.open('w', encoding='utf-8') as file:37file.write(updated_content)3839print(f"Links in {file_path} have been updated.")4041# Main function to handle command-line arguments42if __name__ == "__main__":43# Path to README.md, assuming script is in 'scripts' directory44script_dir = Path(__file__).parent45markdown_file_path = script_dir.parent / 'README.md'4647# Debug print to confirm path48print(f"Processing file: {markdown_file_path}")4950update_links_in_markdown(markdown_file_path)515253