2022-05-16 00:01:54 +02:00
|
|
|
import sys, cooklang
|
|
|
|
from os.path import exists
|
|
|
|
|
2022-05-16 09:46:33 +02:00
|
|
|
def parse_recipe(cooklang_source):
|
2022-05-16 00:01:54 +02:00
|
|
|
"""
|
2022-05-16 09:46:33 +02:00
|
|
|
Takes: string cooklang_source
|
2022-05-16 00:01:54 +02:00
|
|
|
Returns: [ string ] tex
|
|
|
|
|
2022-05-16 09:46:33 +02:00
|
|
|
Takes the recipes source as an argument and returns the cuisine recipe block.
|
2022-05-16 00:01:54 +02:00
|
|
|
One element per line.
|
|
|
|
"""
|
|
|
|
|
2022-05-16 09:46:33 +02:00
|
|
|
recipe = cooklang.parseRecipe(cooklang_source)
|
2022-05-16 00:01:54 +02:00
|
|
|
|
|
|
|
title = get_metadata_value("title", recipe)
|
|
|
|
servings = get_metadata_value("servings", recipe)
|
|
|
|
time = get_metadata_value("time", recipe)
|
|
|
|
|
|
|
|
tex = []
|
|
|
|
|
|
|
|
tex.append("\\begin{recipe}{" +
|
|
|
|
title + "}{" +
|
|
|
|
servings + "}{" +
|
|
|
|
time + "}")
|
|
|
|
|
|
|
|
for step in recipe["steps"]:
|
|
|
|
ingredients = get_step_ingredients(step)
|
|
|
|
|
|
|
|
if ingredients:
|
|
|
|
tex += texify_ingredients(ingredients)
|
|
|
|
else:
|
|
|
|
tex.append("\\newstep")
|
|
|
|
|
|
|
|
step_text = ""
|
|
|
|
for part in step:
|
|
|
|
if "value" in part.keys():
|
|
|
|
step_text += part["value"]
|
|
|
|
elif "name" in part.keys():
|
|
|
|
# Remove whitespaces from ingredient names
|
|
|
|
step_text += part["name"].strip()
|
|
|
|
tex.append(step_text)
|
|
|
|
|
|
|
|
tex.append("\\end{recipe}")
|
|
|
|
|
|
|
|
return tex
|
|
|
|
|
|
|
|
|
2022-05-16 09:46:33 +02:00
|
|
|
def parse_recipe_from_file(path):
|
|
|
|
"""
|
|
|
|
Takes: string path
|
|
|
|
Returns: [ string ] tex
|
|
|
|
|
|
|
|
Takes a recipe path as an argument and returns the cuisine recipe block.
|
|
|
|
One element per line.
|
|
|
|
"""
|
|
|
|
if not exists(path):
|
|
|
|
raise ArgumentError
|
|
|
|
|
|
|
|
with open(path) as file:
|
|
|
|
return parse_recipe(file.read())
|
|
|
|
|
2022-05-16 00:01:54 +02:00
|
|
|
|
|
|
|
def get_metadata_value(key, recipe):
|
|
|
|
if key in recipe["metadata"].keys():
|
|
|
|
return recipe["metadata"][key]
|
|
|
|
else:
|
|
|
|
return "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
def get_step_ingredients(step):
|
|
|
|
ingredients = []
|
|
|
|
|
|
|
|
for part in step:
|
|
|
|
if part["type"] == "ingredient":
|
|
|
|
ingredients.append(part)
|
|
|
|
|
|
|
|
return ingredients
|
|
|
|
|
|
|
|
|
|
|
|
def texify_ingredients(ingredients):
|
|
|
|
tex = []
|
|
|
|
|
|
|
|
for ing in ingredients:
|
|
|
|
if ing["units"]:
|
|
|
|
|
|
|
|
try:
|
|
|
|
quantity = float(ing["quantity"])
|
|
|
|
if quantity % 1 == 0:
|
|
|
|
quantity = int(quantity)
|
|
|
|
except ValueError:
|
|
|
|
quantity = ing["units"]
|
|
|
|
|
|
|
|
tex.append("\\ingredient[" +
|
|
|
|
str(quantity) + "]{" +
|
|
|
|
ing["units"] + "}{" +
|
|
|
|
ing["name"] + "}")
|
|
|
|
else:
|
|
|
|
tex.append("\\ingredient{" +
|
|
|
|
ing["units"] + "}{" +
|
|
|
|
ing["name"] + "}")
|
|
|
|
|
|
|
|
return tex
|
|
|
|
|
|
|
|
|
2022-05-16 09:46:33 +02:00
|
|
|
class ArgumentError(Exception):
|
|
|
|
def __init__(self):
|
|
|
|
self.message = "Please supply the path to a cooklang file as an argument."
|
|
|
|
super().__init__(self.message)
|
|
|
|
|
|
|
|
|
2022-05-16 00:01:54 +02:00
|
|
|
if __name__ == "__main__":
|
2022-05-16 09:46:33 +02:00
|
|
|
|
|
|
|
if sys.argv[1] == "--":
|
|
|
|
tex = parse_recipe(sys.stdin.read())
|
|
|
|
else:
|
|
|
|
tex = parse_recipe_from_file(sys.argv[1])
|
|
|
|
|
|
|
|
for line in tex:
|
|
|
|
print(line)
|