Adding script to generate full recipe site

This commit is contained in:
gamerdonkey 2025-09-13 03:16:07 +00:00
parent b3be5fa90d
commit 4a462d4123

51
recipes2html.py Normal file
View File

@ -0,0 +1,51 @@
from os import path, walk
from recipe import Recipe
from sys import argv
if len(argv) < 2:
print(f"Usage: {argv[0]} <recipe directory>")
exit(1)
recipe_dir = argv[1]
recipe_names = {}
for dirpath, dirnames, filenames in walk(recipe_dir):
for filename in filenames:
if filename.endswith("json"):
recipe = Recipe(path.join(dirpath, filename))
html = recipe.to_html()
html_filepath = path.join(dirpath, f"{filename.rsplit('.', maxsplit=1)[0]}.html")
with open(html_filepath, 'w') as html_file:
html_file.write(html)
recipe_names[recipe.name] = path.relpath(html_filepath, start=recipe_dir)
index_html = f"""<!DOCTYPE html>
<html lang='en'>
<head>
<title>Recipes</title>
<link rel='stylesheet' type='text/css' href='style.css'>
<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'>
</head>
<body>
<header>
<h1>Recipes</h1>
<p>These are recipes that I'd like to share.</p>
</header>
<main>
<ul>
{"\n".join([f" <li><a href='./{filepath}'>{name}</a></li>" for name, filepath in recipe_names.items()])}
</ul>
</main>
</body>
</html>
"""
with open(path.join(recipe_dir, "index.html"), 'w') as index_file:
index_file.write(index_html)