🎨 reformat using black
This commit is contained in:
parent
a1ab4c0b5a
commit
1994109507
5 changed files with 70 additions and 53 deletions
|
@ -25,9 +25,10 @@ def normalize_amount(a):
|
|||
else:
|
||||
return a
|
||||
|
||||
|
||||
def md_for_step_ingredients(step):
|
||||
md = []
|
||||
for ingredient in step['ingredients']:
|
||||
for ingredient in step["ingredients"]:
|
||||
i_amount = ingredient["amount"]
|
||||
i_amount = normalize_amount(i_amount)
|
||||
|
||||
|
@ -52,6 +53,7 @@ def md_for_step_ingredients(step):
|
|||
md.append(f"- {i_name}")
|
||||
return md
|
||||
|
||||
|
||||
def format_recipe_to_markdown(recipe):
|
||||
md = []
|
||||
|
||||
|
@ -60,7 +62,7 @@ def format_recipe_to_markdown(recipe):
|
|||
md.append(f"# {recipe.get('name', 'Untitled Recipe')}")
|
||||
|
||||
# Description
|
||||
if valid_description(recipe.get('description')):
|
||||
if valid_description(recipe.get("description")):
|
||||
md.append(f"\n{recipe['description']}\n")
|
||||
|
||||
# Details
|
||||
|
@ -68,10 +70,10 @@ def format_recipe_to_markdown(recipe):
|
|||
prep_time = recipe.get("working_time") or 0
|
||||
total_time = (recipe.get("working_time") or 0) + (recipe.get("waiting_time") or 0)
|
||||
if prep_time:
|
||||
details_parts.append(f"Prep time: {prep_time}")
|
||||
details_parts.append(f"Prep time: {prep_time}")
|
||||
if total_time:
|
||||
details_parts.append(f"Total time: {total_time}")
|
||||
if 'servings' in recipe:
|
||||
details_parts.append(f"Total time: {total_time}")
|
||||
if "servings" in recipe:
|
||||
details_parts.append(f"Portionen: {recipe['servings']}")
|
||||
# TODO servings_text
|
||||
if details_parts:
|
||||
|
@ -84,9 +86,9 @@ def format_recipe_to_markdown(recipe):
|
|||
md += md_for_step_ingredients(step)
|
||||
|
||||
# Instructions
|
||||
if 'steps' in recipe:
|
||||
if "steps" in recipe:
|
||||
md.append("\n## Zubereitung")
|
||||
steps = recipe['steps']
|
||||
steps = recipe["steps"]
|
||||
for i, step in enumerate(steps, 1):
|
||||
step_name = step["name"] or "<span></span>"
|
||||
md.append(f"{i}. {step_name}")
|
||||
|
@ -100,24 +102,25 @@ def format_recipe_to_markdown(recipe):
|
|||
# if key != "@type":
|
||||
# md.append(f"- **{key.replace('_', ' ').capitalize()}**: {value}")
|
||||
|
||||
return '\n'.join(md) + '\n'
|
||||
return "\n".join(md) + "\n"
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
recipes = []
|
||||
for json_file in os.listdir(OUTDIR_JSON):
|
||||
with open(os.path.join(OUTDIR_JSON, json_file), 'r', encoding='utf-8') as f:
|
||||
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
recipes.append(data)
|
||||
|
||||
for recipe in tqdm(recipes):
|
||||
markdown_fn = f"{recipe.get('name', 'Untitled Recipe')}.md"
|
||||
validate_filename(markdown_fn) # XXX does this check directory traversal?
|
||||
validate_filename(markdown_fn) # XXX does this check directory traversal?
|
||||
markdown = format_recipe_to_markdown(recipe)
|
||||
os.makedirs(OUTDIR_MARKDOWN, exist_ok=True)
|
||||
with open(os.path.join(OUTDIR_MARKDOWN, markdown_fn), "w") as f:
|
||||
f.write(markdown)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue