from pathlib import Path import markdown from markdown.extensions.wikilinks import WikiLinkExtension from markdown.extensions.toc import TocExtension SOURCE_DIR = Path("docs") # input folder OUTPUT_DIR = Path("www") # output folder def my_url_builder(label, base, end): url = (base + label + end).lower().replace(' ', '_') return url def convert_markdown_to_html(md_path: Path, html_path: Path): html_path.parent.mkdir(parents=True, exist_ok=True) md = markdown.Markdown(extensions=[ WikiLinkExtension(base_url='./', end_url='.html', build_url=my_url_builder), TocExtension(permalink=True), 'tables', 'fenced_code', 'sane_lists', 'prependnewline', 'meta', ]) html = md.convert(md_path.read_text(encoding="utf-8")) title = md.Meta["title"][0] # type: ignore prev_page = md.Meta["prev"][0] # type: ignore next_page = md.Meta["next"][0] # type: ignore prev_link = f'Prev' next_link = f'Next' html_page = f""" YREA SLS | {title}

{title}


{html}
""" html_path.write_text(html_page, encoding="utf-8") print(f"Converted: {md_path} → {html_path}") def convert_all(source_dir: Path, output_dir: Path): for svg_file in source_dir.rglob("*.svg"): rel_path = svg_file.relative_to(source_dir) dest_path = output_dir / rel_path dest_path.parent.mkdir(parents=True, exist_ok=True) dest_path.write_bytes(svg_file.read_bytes()) print(f"Copied: {svg_file} → {dest_path}") for css_file in source_dir.rglob("*.css"): rel_path = css_file.relative_to(source_dir) dest_path = output_dir / rel_path dest_path.parent.mkdir(parents=True, exist_ok=True) dest_path.write_bytes(css_file.read_bytes()) print(f"Copied: {css_file} → {dest_path}") for md_file in source_dir.rglob("*.md"): rel_path = md_file.relative_to(source_dir) html_path = output_dir / rel_path.with_suffix(".html") convert_markdown_to_html(md_file, html_path) def convert_all_watch_files(source_dir: Path, output_dir: Path): import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class ChangeHandler(FileSystemEventHandler): def on_modified(self, event): if event.src_path.endswith(".md"): # type: ignore md_path = Path(event.src_path) # type: ignore rel_path = md_path.relative_to(source_dir) html_path = output_dir / rel_path.with_suffix(".html") convert_markdown_to_html(md_path, html_path) elif event.src_path.endswith(".css"): # type: ignore css_path = Path(event.src_path) # type: ignore rel_path = css_path.relative_to(source_dir) dest_path = output_dir / rel_path dest_path.parent.mkdir(parents=True, exist_ok=True) dest_path.write_bytes(css_path.read_bytes()) print(f"Copied: {css_path} → {dest_path}") event_handler = ChangeHandler() observer = Observer() observer.schedule(event_handler, str(source_dir), recursive=True) observer.start() print("Watching for changes... Press Ctrl+C to stop.") try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() if __name__ == "__main__": convert_all(SOURCE_DIR, OUTPUT_DIR) # convert_all_watch_files(SOURCE_DIR, OUTPUT_DIR)