🚧 details (portions etc.)

This commit is contained in:
neingeist 2025-06-02 00:45:10 +02:00
parent 24939a76bd
commit d50295838e
43 changed files with 219 additions and 199 deletions

View file

@ -6,6 +6,16 @@ import re
from pathvalidate import validate_filename
import os
output_dir = "recipes-md"
def valid_duration(duration):
if not duration:
return False
if duration == "0":
return False
return True
def parse_duration(duration):
if not duration:
return ""
@ -27,28 +37,29 @@ def format_recipe_to_markdown(recipe):
if 'description' in recipe:
md.append(f"\n_{recipe['description']}_\n")
# Metadata
time_parts = []
if 'prepTime' in recipe:
time_parts.append(f"Prep time: {parse_duration(recipe['prepTime'])}")
if 'cookTime' in recipe:
time_parts.append(f"Cook time: {parse_duration(recipe['cookTime'])}")
if 'totalTime' in recipe:
time_parts.append(f"Total time: {parse_duration(recipe['totalTime'])}")
# Details
details_parts = []
if valid_duration(recipe.get('prepTime')):
details_parts.append(f"Prep time: {parse_duration(recipe['prepTime'])}")
if valid_duration(recipe.get('cookTime')):
details_parts.append(f"Cook time: {parse_duration(recipe['cookTime'])}")
if valid_duration(recipe.get('totalTime')):
details_parts.append(f"Total time: {parse_duration(recipe['totalTime'])}")
if 'recipeYield' in recipe:
time_parts.append(f"Yield: {recipe['recipeYield']}")
if time_parts:
md.append('\n**Details:** ' + ' | '.join(time_parts) + '\n')
details_parts.append(f"Portionen: {recipe['recipeYield']}")
if details_parts:
for p in details_parts:
md.append(f"* {p}\n")
# Ingredients
if 'recipeIngredient' in recipe:
md.append("\n## Ingredients")
md.append("\n## Zutaten")
for ingredient in recipe['recipeIngredient']:
md.append(f"- {ingredient}")
# Instructions
if 'recipeInstructions' in recipe:
md.append("\n## Instructions")
md.append("\n## Zubereitung")
instructions = recipe['recipeInstructions']
if isinstance(instructions, list):
for i, step in enumerate(instructions, 1):
@ -92,8 +103,8 @@ def main(json_file):
markdown_fn = f"{recipe.get('name', 'Untitled Recipe')}.md"
validate_filename(markdown_fn) # XXX does this check directory traversal?
markdown = format_recipe_to_markdown(recipe)
print(markdown)
with open(os.path.join("recipes-md", markdown_fn), "w") as f:
os.makedirs(output_dir, exist_ok=True)
with open(os.path.join(output_dir, markdown_fn), "w") as f:
f.write(markdown)
if __name__ == "__main__":