from pathlib import Path
import markdown
from markdown.extensions.wikilinks import WikiLinkExtension
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),
'tables',
'fenced_code',
'sane_lists',
'prependnewline',
'meta',
])
html = md.convert(md_path.read_text(encoding="utf-8"))
title = md.Meta["Title"]
next_page = md.Meta["Next"]
prev_page = md.Meta["Prev"]
html_page = f"""
{md_path.stem}
{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 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"):
md_path = Path(event.src_path)
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"):
css_path = Path(event.src_path)
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)