Coverage for cookbook/helper/template_helper.py: 74%
53 statements
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 01:02 +0100
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 01:02 +0100
1from gettext import gettext as _
3import bleach
4import markdown as md
5from jinja2 import Template, TemplateSyntaxError, UndefinedError
6from markdown.extensions.tables import TableExtension
8from cookbook.helper.mdx_attributes import MarkdownFormatExtension
9from cookbook.helper.mdx_urlize import UrlizeExtension
12class IngredientObject(object):
13 amount = ""
14 unit = ""
15 food = ""
16 note = ""
18 def __init__(self, ingredient):
19 if ingredient.no_amount:
20 self.amount = ""
21 else:
22 self.amount = f"<scalable-number v-bind:number='{bleach.clean(str(ingredient.amount))}' v-bind:factor='ingredient_factor'></scalable-number>"
23 if ingredient.unit:
24 if ingredient.unit.plural_name in (None, ""):
25 self.unit = bleach.clean(str(ingredient.unit))
26 else:
27 if ingredient.always_use_plural_unit or ingredient.amount > 1 and not ingredient.no_amount:
28 self.unit = bleach.clean(ingredient.unit.plural_name)
29 else:
30 self.unit = bleach.clean(str(ingredient.unit))
31 else:
32 self.unit = ""
33 if ingredient.food:
34 if ingredient.food.plural_name in (None, ""):
35 self.food = bleach.clean(str(ingredient.food))
36 else:
37 if ingredient.always_use_plural_food or ingredient.amount > 1 and not ingredient.no_amount:
38 self.food = bleach.clean(str(ingredient.food.plural_name))
39 else:
40 self.food = bleach.clean(str(ingredient.food))
41 else:
42 self.food = ""
43 self.note = bleach.clean(str(ingredient.note))
45 def __str__(self):
46 ingredient = self.amount
47 if self.unit != "":
48 ingredient += f' {self.unit}'
49 return f'{ingredient} {self.food}'
52def render_instructions(step): # TODO deduplicate markdown cleanup code
53 instructions = step.instruction
55 tags = {
56 "h1", "h2", "h3", "h4", "h5", "h6",
57 "b", "i", "strong", "em", "tt",
58 "p", "br",
59 "span", "div", "blockquote", "code", "pre", "hr",
60 "ul", "ol", "li", "dd", "dt",
61 "img",
62 "a",
63 "sub", "sup",
64 'pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'
65 }
66 parsed_md = md.markdown(
67 instructions,
68 extensions=[
69 'markdown.extensions.fenced_code', TableExtension(),
70 UrlizeExtension(), MarkdownFormatExtension()
71 ]
72 )
73 markdown_attrs = {
74 "*": ["id", "class", 'width', 'height'],
75 "img": ["src", "alt", "title"],
76 "a": ["href", "alt", "title"],
77 }
79 instructions = bleach.clean(parsed_md, tags, markdown_attrs)
81 ingredients = []
83 for i in step.ingredients.all():
84 ingredients.append(IngredientObject(i))
86 try:
87 template = Template(instructions)
88 instructions = template.render(ingredients=ingredients)
89 except TemplateSyntaxError:
90 return _('Could not parse template code.') + ' Error: Template Syntax broken'
91 except UndefinedError:
92 return _('Could not parse template code.') + ' Error: Undefined Error'
94 return instructions