!/usr/bin/env python

import os, sys, shutil, markdown

from pathlib import Path

check whether the output root already exists

if(os.path.isdir('./html-out/')):

# if the output path exists, remove it and any contents
shutil.rmtree('./html-out/')
# create the output path anew
os.makedirs('./html-out/')

mdextras=["admonitions", "breaks", "code-friendly", "fenced-code-blocks", "footnotes", "metadata", "pyshell", "spoiler", "strike", "tables", "task_list"]

traverse root directory, and list directories as dirs and files as files

for root, dirs, files in os.walk("./markdown-in"):

path = root.split(os.sep)
print(f"\nWalking {root} {dirs} {files}")
# check each file in the working folder fo an .md extension
for file in files:
    print(f"\nWorking {file}")
    # initialize root folder of output path
    tgtroot=root.replace('markdown-in','html-out')
    if(os.path.isdir(tgtroot)!=True):
        os.makedirs(tgtroot)
    if file[-2:] == "md":
        print(f"Found markdown: {file}")
        if(file == "header.md"):
            print(f"Skipping {file}")
            continue;
        if(file == "footer.md"):
            print(f"Skipping {file}")
            continue;
        # check for any html prefab; if one should exist, copy it into the output stream here.
        # while copying, look for any <style> tag. Should one be found, look for css/render.css and copy it into the stream. Should no render.css be there,
        # the style tag will fail silently.
        outfile=file.replace('.md','.html')
        fullout=f"./{tgtroot}/{outfile}"
        Path(fullout).touch(mode=0o755, exist_ok=True)
        print(f" -- processing {root}/{file} ==> {tgtroot}/{outfile}")
        with open(fullout, 'w') as outfile:
            if(os.path.exists("./html-prefab/preamble.html")):
                print("    Injecting html preamble")
                with open("./html-prefab/preamble.html", mode="r", buffering=1, encoding="utf-8") as readfile:
                    for src in readfile:
                        outfile.write(src)
                        # check for <style> tag
                        if(src.find('<style>')) >= 0:
                            # inject any css rendering classes
                            if(os.path.exists("./css/render.css")):
                                print("    Injecting CSS")
                                with open("./css/render.css", mode="r", buffering=1, encoding="utf-8") as readfile:
                                    for src in readfile:
                                        outfile.write(src)
            else:
                print("    Injecting default html preamble")
                writefile.write("<html>\n  <head>\n    <title>Work In Progress</title>\n  </head>\n  <body>\n")
            # convert header.md, if it exists
            if(os.path.exists(f"{root}/header.md")):
                print("    processing markdown header")
                with open(f"{root}/header.md", mode="r", buffering=1, encoding="utf-8") as readfile:
                    for src in readfile:
                        outfile.write(markdown.markdown(src, extras=mdextras))
            # convert and append topical md file
            print(f"    converting topical markdown file {root}/{file}")
            with open(f"{root}/{file}", mode="r", buffering=1, encoding="utf-8") as readfile:
                for src in readfile:
                    outfile.write(markdown.markdown(src, extras=mdextras))
            # convert footer.md and append to stream, if it exists
            if(os.path.exists(f"{root}/footer.md")):
                print("    processing markdown footer")
                with open(f"{root}/footer.md", mode="r", buffering=1, encoding="utf-8") as readfile:
                    for src in readfile:
                        outfile.write(markdown.markdown(src,extras=mdextras))
            # copy in any finishing html touches
            if(os.path.exists("./html-prefab/trailer.html")):
                print("    inject html trailer prefab")
                with open("./html-prefab/trailer.html", buffering=1, mode="r", encoding="utf-8") as readfile:
                    for src in readfile:
                        outfile.write(markdown.markdown(src,extras=mdextras))
            else:
                print("    inject default html trailer prefab")
                outfile.write("</body></html>")
    else:
        print(f"copying {root}/{file} to {tgtroot}/{file}")
        shutil.copyfile(f"{root}/{file}", f"{tgtroot}/{file}")

home/profile/status/shortform/soapbox/documents


produced via mdfoundry (c) James Stallings 2026 - all rights released under the BSD license.