Added compatibility for different metadata terms.
This commit is contained in:
parent
63710bfb8e
commit
899398a2ea
1 changed files with 24 additions and 13 deletions
37
cook2tex.py
37
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,18 +56,26 @@ 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"]
|
||||||
return recipe["metadata"][key]
|
|
||||||
else:
|
def get_metadata_value(keys, recipe, substitute="N/A"):
|
||||||
return "N/A"
|
for key in keys:
|
||||||
|
if key in recipe["metadata"].keys():
|
||||||
|
return recipe["metadata"][key]
|
||||||
|
|
||||||
|
return substitute
|
||||||
|
|
||||||
|
|
||||||
def get_step_ingredients(step):
|
def get_step_ingredients(step):
|
||||||
|
|
Loading…
Reference in a new issue