Initial commit.
This commit is contained in:
commit
a05784c849
3 changed files with 152 additions and 0 deletions
20
README.md
Normal file
20
README.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Cook2tex
|
||||
|
||||
Two small python scripts that turn [cooklang](https://cooklang.org/) recipes into LaTeX source files.
|
||||
The LaTeX files use the [cuisine](https://ctan.org/pkg/cuisine) package.
|
||||
|
||||
## cook2tex.py
|
||||
|
||||
Takes a recipe path as an argument and returns the cuisine recipe block on stdout.
|
||||
|
||||
### Example
|
||||
|
||||
`python3 cook2tex.py /path/to/recipe`
|
||||
|
||||
## cooklangbook.py
|
||||
|
||||
Takes multiple recipe paths as arguments and returns a complete latex document on stdout.
|
||||
|
||||
### Example
|
||||
|
||||
`python3 cooklangbook.py /path/to/recipes/{rec1,rec2,rec3}`
|
102
cook2tex.py
Normal file
102
cook2tex.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
import sys, cooklang
|
||||
from os.path import exists
|
||||
|
||||
def parse_recipe(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:
|
||||
# First argument is recipe file
|
||||
recipe = cooklang.parseRecipe(file.read())
|
||||
|
||||
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
|
||||
|
||||
|
||||
class ArgumentError(Exception):
|
||||
def __init__(self):
|
||||
self.message = "Please supply the path to a cooklang file as an argument."
|
||||
super().__init__(self.message)
|
||||
|
||||
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
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for line in parse_recipe(sys.argv[1]):
|
||||
print(line)
|
30
cooklangbook.py
Normal file
30
cooklangbook.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import sys, cook2tex
|
||||
|
||||
def create_book(argv):
|
||||
"""
|
||||
Takes: [ string ] argv
|
||||
Returns: [ string ] tex
|
||||
|
||||
Takes multiple recipe paths as arguments and returns a complete latex document.
|
||||
One element per line.
|
||||
"""
|
||||
tex = []
|
||||
|
||||
tex.append("\\documentclass{article}")
|
||||
tex.append("\\usepackage{cuisine}")
|
||||
tex.append("\n")
|
||||
tex.append("\\begin{document}")
|
||||
|
||||
for path in argv:
|
||||
tex.append("\n")
|
||||
tex += cook2tex.parse_recipe(path)
|
||||
|
||||
tex.append("\n")
|
||||
tex.append("\\end{document}")
|
||||
|
||||
return tex
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for line in create_book(sys.argv[1:]):
|
||||
print(line)
|
Loading…
Reference in a new issue