-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
32 lines (28 loc) · 1.08 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from recipes import load_recipes as load, save_recipes as save
def export_markdown():
recipes_json = load()
recipes_md = []
for recipe in recipes_json["recipes"]:
md_text = "<title>" + recipe["name"] + "</title>\n"
md_text += "# " + recipe["name"] + "\n"
md_text += recipe["desc"] + " \n"
if "recipes" in recipe:
for subrecipe in recipe["recipes"]:
md_text += do_recipe(subrecipe)
md_text += do_recipe(recipe, "")
recipes_md.append(
{"name": recipe["name"], "desc": recipe["desc"], "text": md_text}
)
return recipes_md
def do_recipe(recipe, extra="#"):
md_text = ""
if extra:
md_text += f"{extra}# " + recipe["name"] + "\n"
if "ingredients" in recipe:
md_text += f"{extra}## Ingredients\n"
for ingredient in recipe["ingredients"]:
md_text += "- " + ingredient + " \n"
if "instructions" in recipe:
md_text += f"{extra}## Instructions\n"
md_text += recipe["instructions"].replace("\n", " \n") + " \n"
return md_text