From 0e31f6bcf054acf939c49b407fa79f1c77aa0f53 Mon Sep 17 00:00:00 2001 From: Lukas Krupcik <lukas.krupcik@vsb.cz> Date: Mon, 3 Mar 2025 11:29:07 +0100 Subject: [PATCH] modified: scripts/maketitle.py --- scripts/maketitle.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/maketitle.py b/scripts/maketitle.py index bad870b5..d3f6dc20 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: -- GitLab