diff --git a/scripts/maketitle.py b/scripts/maketitle.py index bad870b59c7372a4b8f28d85e043284b57f6be8a..d3f6dc20e14c50d3e04eb702505a515977369542 100644 --- a/scripts/maketitle.py +++ b/scripts/maketitle.py @@ -1,13 +1,25 @@ #!/usr/bin/python3 +""" +Script to process Markdown files, convert them to MDX, +and add frontmatter based on the first H1 heading. +""" + import os import re from pathlib import Path def process_md_file(md_path): + """ + Converts a Markdown file (.md) to MDX format (.mdx), + adds frontmatter with title from H1 heading, and removes the original file. + + Args: + md_path (Path): Path to the Markdown file. + """ try: - with open(md_path, 'r', encoding='utf-8') as f: - content = f.read() + with open(md_path, 'r', encoding='utf-8') as file_handle: + content = file_handle.read() except UnicodeDecodeError: print(f"Skipping {md_path} - unable to decode as UTF-8") return @@ -49,14 +61,15 @@ def process_md_file(md_path): # Create and write MDX file mdx_path = md_path.with_suffix('.mdx') - with open(mdx_path, 'w', encoding='utf-8') as f: - f.write(new_content) + with open(mdx_path, 'w', encoding='utf-8') as file_handle: + file_handle.write(new_content) # Remove original MD file md_path.unlink() print(f"Converted {md_path} to {mdx_path}") def main(): + """Walks through directories and processes all Markdown files.""" for root, dirs, files in os.walk('.'): dirs[:] = [d for d in dirs if not d.startswith('.')] for file in files: