Compare commits
3 commits
63710bfb8e
...
0f9f7b237c
Author | SHA1 | Date | |
---|---|---|---|
0f9f7b237c | |||
576a9bb324 | |||
899398a2ea |
3 changed files with 130 additions and 22 deletions
12
README.md
12
README.md
|
@ -15,7 +15,17 @@ Optionally, if provided with the argument "--", reads the recipes source from st
|
||||||
## cooklangbook.py
|
## cooklangbook.py
|
||||||
|
|
||||||
Takes multiple recipe paths as arguments and returns a complete latex document on stdout.
|
Takes multiple recipe paths as arguments and returns a complete latex document on stdout.
|
||||||
|
Optionally takes the '-r' flag and two directory paths,
|
||||||
|
the first one being the source and the second one the destination directory.
|
||||||
|
It then generates multiple .tex files,
|
||||||
|
one per recipe and one called `recipe-book.tex` including all recipies.
|
||||||
|
|
||||||
### Example
|
### Examples
|
||||||
|
|
||||||
|
Basic operation:
|
||||||
|
|
||||||
`python3 cooklangbook.py /path/to/recipes/{rec1,rec2,rec3}`
|
`python3 cooklangbook.py /path/to/recipes/{rec1,rec2,rec3}`
|
||||||
|
|
||||||
|
Recursive book generation:
|
||||||
|
|
||||||
|
`python3 cooklangbook.py /path/to/source/dir /path/to/dest/dir`
|
||||||
|
|
48
cook2tex.py
48
cook2tex.py
|
@ -1,7 +1,6 @@
|
||||||
import sys, cooklang
|
import sys, os, cooklang
|
||||||
from os.path import exists
|
|
||||||
|
|
||||||
def parse_recipe(cooklang_source):
|
def parse_recipe(cooklang_source, title=""):
|
||||||
"""
|
"""
|
||||||
Takes: string cooklang_source
|
Takes: string cooklang_source
|
||||||
Returns: [ string ] tex
|
Returns: [ string ] tex
|
||||||
|
@ -12,9 +11,13 @@ def parse_recipe(cooklang_source):
|
||||||
|
|
||||||
recipe = cooklang.parseRecipe(cooklang_source)
|
recipe = cooklang.parseRecipe(cooklang_source)
|
||||||
|
|
||||||
title = get_metadata_value("title", recipe)
|
if title:
|
||||||
servings = get_metadata_value("servings", recipe)
|
title = get_metadata_value(["title"], recipe, title)
|
||||||
time = get_metadata_value("time", recipe)
|
else:
|
||||||
|
title = get_metadata_value(["title"], recipe)
|
||||||
|
|
||||||
|
servings = get_metadata_value(time_keys, recipe)
|
||||||
|
time = get_metadata_value(serving_keys, recipe)
|
||||||
|
|
||||||
tex = []
|
tex = []
|
||||||
|
|
||||||
|
@ -53,19 +56,38 @@ def parse_recipe_from_file(path):
|
||||||
Takes a recipe path as an argument and returns the cuisine recipe block.
|
Takes a recipe path as an argument and returns the cuisine recipe block.
|
||||||
One element per line.
|
One element per line.
|
||||||
"""
|
"""
|
||||||
if not exists(path):
|
if not os.path.exists(path):
|
||||||
raise ArgumentError
|
raise ArgumentError
|
||||||
|
|
||||||
|
name = os.path.basename(path)
|
||||||
|
split_name = os.path.splitext(name)
|
||||||
|
title = split_name[0]
|
||||||
|
|
||||||
with open(path) as file:
|
with open(path) as file:
|
||||||
return parse_recipe(file.read())
|
return parse_recipe(file.read(), title)
|
||||||
|
|
||||||
|
|
||||||
def get_metadata_value(key, recipe):
|
serving_keys = ["servings", "serves"]
|
||||||
if key in recipe["metadata"].keys():
|
time_keys = ["time", "total time", "total-time"]
|
||||||
return recipe["metadata"][key]
|
|
||||||
else:
|
def get_metadata_value(keys, recipe, substitute="N/A"):
|
||||||
return "N/A"
|
|
||||||
|
|
||||||
|
# Sometimes people use too many >s for their metadata…
|
||||||
|
meta_data = { rem_beginning_duplicate(key, ">").strip(): recipe["metadata"][key] for key in recipe["metadata"].keys() }
|
||||||
|
|
||||||
|
for key in keys:
|
||||||
|
if key in meta_data.keys():
|
||||||
|
return meta_data[key]
|
||||||
|
|
||||||
|
return substitute
|
||||||
|
|
||||||
|
|
||||||
|
def rem_beginning_duplicate(in_str, rem_char):
|
||||||
|
while in_str and in_str[0] == rem_char:
|
||||||
|
in_str = in_str[1:]
|
||||||
|
|
||||||
|
return in_str
|
||||||
|
|
||||||
|
|
||||||
def get_step_ingredients(step):
|
def get_step_ingredients(step):
|
||||||
ingredients = []
|
ingredients = []
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import sys, cook2tex
|
import sys, os, cook2tex
|
||||||
|
|
||||||
def create_book(argv):
|
def create_book(argv):
|
||||||
"""
|
"""
|
||||||
|
@ -10,21 +10,97 @@ def create_book(argv):
|
||||||
"""
|
"""
|
||||||
tex = []
|
tex = []
|
||||||
|
|
||||||
|
for path in argv:
|
||||||
|
tex.append("\n")
|
||||||
|
tex += cook2tex.parse_recipe_from_file(path)
|
||||||
|
|
||||||
|
return latex_boilerplate(tex)
|
||||||
|
|
||||||
|
def create_book_recursive(source_path, dest_path):
|
||||||
|
"""
|
||||||
|
Takes: string source_path
|
||||||
|
string dest_path
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
Reads all .cook files found in source_path and it's subdirectories.
|
||||||
|
Creates a new directory at dest_dir if it doesn't exist already.
|
||||||
|
When a recipe path reads '/source_dir/.../last_dir/recipe.cook',
|
||||||
|
creates /dest_dir/last_dir/recipe.tex and writes texified recipe to it.
|
||||||
|
Creates /dest_dir/recipe-book.tex which includes all generated recipe files,
|
||||||
|
has one chapter per directory and a table of contents.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.mkdir(dest_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
chapters = {}
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(source_path):
|
||||||
|
cook_files = {}
|
||||||
|
|
||||||
|
for name in files:
|
||||||
|
split_name = os.path.splitext(name)
|
||||||
|
if split_name[-1] == ".cook":
|
||||||
|
cook_files[split_name[0]] = os.path.join(root, name)
|
||||||
|
|
||||||
|
if cook_files:
|
||||||
|
dir_name = os.path.basename(root)
|
||||||
|
dir_path = os.path.join(dest_path, dir_name)
|
||||||
|
|
||||||
|
chapters[dir_name] = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.mkdir(dir_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for name in cook_files.keys():
|
||||||
|
recipe_path = os.path.join(dir_path, name + ".tex")
|
||||||
|
chapters[dir_name].append(recipe_path)
|
||||||
|
|
||||||
|
with open(recipe_path, "w") as file:
|
||||||
|
for line in cook2tex.parse_recipe_from_file(cook_files[name]):
|
||||||
|
file.write(line + "\n")
|
||||||
|
|
||||||
|
tex = []
|
||||||
|
|
||||||
|
for chap in chapters.keys():
|
||||||
|
tex.append("\\section{" + chap + "}")
|
||||||
|
for file_name in chapters[chap]:
|
||||||
|
tex.append("\\input{" +
|
||||||
|
os.path.relpath(file_name, dest_path) + "}")
|
||||||
|
|
||||||
|
tex.append("\n")
|
||||||
|
|
||||||
|
with open(os.path.join(dest_path, "recipe-book.tex"), "w") as file:
|
||||||
|
for line in latex_boilerplate(tex):
|
||||||
|
file.write(line + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
def latex_boilerplate(body):
|
||||||
|
tex = []
|
||||||
|
|
||||||
tex.append("\\documentclass{article}")
|
tex.append("\\documentclass{article}")
|
||||||
tex.append("\\usepackage{cuisine}")
|
tex.append("\\usepackage{cuisine}")
|
||||||
tex.append("\n")
|
tex.append("\n")
|
||||||
tex.append("\\begin{document}")
|
tex.append("\\begin{document}")
|
||||||
|
tex.append("\n")
|
||||||
|
tex.append("\\tableofcontents")
|
||||||
|
tex.append("\n")
|
||||||
|
|
||||||
for path in argv:
|
tex += body
|
||||||
tex.append("\n")
|
|
||||||
tex += cook2tex.parse_recipe_from_file(path)
|
|
||||||
|
|
||||||
tex.append("\n")
|
tex.append("\n")
|
||||||
tex.append("\\end{document}")
|
tex.append("\\end{document}")
|
||||||
|
|
||||||
return tex
|
return tex
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
for line in create_book(sys.argv[1:]):
|
if sys.argv[1] == "-r":
|
||||||
print(line)
|
tex = create_book_recursive(sys.argv[2], sys.argv[3])
|
||||||
|
else:
|
||||||
|
for line in create_book(sys.argv[1:]):
|
||||||
|
print(line)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue