-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
executable file
·73 lines (58 loc) · 2.58 KB
/
converter.py
File metadata and controls
executable file
·73 lines (58 loc) · 2.58 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import os
import re
import json
import frontmatter
def sanitize(ingredients):
# matches ingredient['name'] starting with "=" to remove them from ingredients list
pattern = re.compile(r"^=+")
return [ingredient for ingredient in ingredients if not pattern.match(ingredient['name'])]
def write_recipes_to_json_file():
recipes = []
success = []
failed = []
# Walk through all subdirectories of "recipes" to find ".md" files
for root, _, files in os.walk("recipes"):
for filename in files:
if filename.endswith(".md") and filename != "_template.md":
# Open each file, read the frontmatter in it and add it to recipes list
filepath = os.path.join(root, filename)
try:
with open(filepath, "r", encoding="utf-8") as file:
fm = frontmatter.load(file)
title = fm.get("title")
parent = fm.get("parent")
ingredients = fm.get("ingredients")
if not title:
err = "missing or empty 'title'"
failed.append({"file": filename, "reason": err})
continue
if not ingredients or len(ingredients) == 0:
err = "missing or empty 'ingredients' list"
failed.append({"file": filename, "reason": err})
continue
sanitized_ingredients = sanitize(ingredients)
recipe_data = {
"title": title,
"parent": parent,
"ingredients": sanitized_ingredients,
}
recipes.append(recipe_data)
success.append(filename)
except Exception as err:
failed.append({"file": filename, "reason": err})
continue
try:
with open("recipes.json", "w", encoding="utf-8") as file:
json.dump(recipes, file, indent=2, ensure_ascii=False)
except Exception as err:
print(f"🚨 Failed to write to 'recipes.json' file: {err}")
if len(success) > 0:
print("\n✅ Successfully processed:")
for file in success:
print(f" - {file}")
if len(failed) > 0:
print("\n❌ Failed processing for:")
for key in failed:
print(f" - {key['file']} ({key['reason']})")
write_recipes_to_json_file()