Skip to content

Instantly share code, notes, and snippets.

@Omustardo
Created August 13, 2025 05:45
Show Gist options
  • Select an option

  • Save Omustardo/424847fa51c0ce018017e8940a41bd82 to your computer and use it in GitHub Desktop.

Select an option

Save Omustardo/424847fa51c0ce018017e8940a41bd82 to your computer and use it in GitHub Desktop.
import os
import subprocess
BASE_DIR = "/home/omustardo/go/src/github.com/omustardo/omustardo.com/"
DEPLOY_DIR = BASE_DIR + "public/"
import os
from pathlib import Path
def convert_md_files_to_html(directory):
# Walk through all directories
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.md'):
md_path = Path(root) / file
html_path = md_path.with_suffix('.html')
# Calculate relative path to the root directory
rel_path = os.path.relpath(DEPLOY_DIR, os.path.dirname(html_path))
# Convert Windows backslashes to forward slashes if needed
rel_path = rel_path.replace('\\', '/')
# Make sure it ends with a slash
if not rel_path.endswith('/'):
rel_path += '/'
# Create the dynamic header with relative paths
# This allows the website to function when running locally
#
# `marked.min.js` isn't needed on its own. It is needed for `puremd.js`.
# `puremd.js` already depends on it, so putting it here isn't entirely necessary,
# but if it isn't explicitly included then scrapers like wget might not see it and then puremd won't
# work if the website is loaded locally. This might be solvable using a sitemap, which I should probably
# Note that we still need to include it in puremd.js as the order of DOM events used there seems important.
# TODO: Get rid of this dynamic rendering. Just use pandoc so the site doesn't need javascript.
header = f'''
<script src="{rel_path}js/marked.min.js"></script>
<script src="{rel_path}js/puremd.js"></script>
[Home]({rel_path}index.html)
'''
try:
# Read markdown content
with open(md_path, 'r', encoding='utf-8') as f:
content = f.read()
# Write to html file
with open(html_path, 'w', encoding='utf-8') as f:
f.write(header + content)
print(f"Created: {html_path}")
except Exception as e:
print(f"Error processing {md_path}: {e}")
if __name__ == "__main__":
if os.path.exists(DEPLOY_DIR):
convert_md_files_to_html(DEPLOY_DIR)
else:
print(f"Error: The specified directory {DEPLOY_DIR} does not exist.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment