super basic templating working

This commit is contained in:
vabene1111
2021-01-05 21:46:24 +01:00
parent f26b09cc0a
commit ee707eba5c
4 changed files with 26 additions and 2 deletions

View File

@ -0,0 +1,19 @@
import bleach
import markdown as md
from bleach_whitelist import markdown_tags, markdown_attrs
from jinja2 import Template
from cookbook.helper.mdx_attributes import MarkdownFormatExtension
from cookbook.helper.mdx_urlize import UrlizeExtension
def render_instructions(step): # TODO deduplicate markdown cleanup code
template = Template(step.instruction)
instructions = template.render(ingredients=step.ingredients.all())
tags = markdown_tags + ['pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead']
parsed_md = md.markdown(instructions, extensions=['markdown.extensions.fenced_code', 'tables', UrlizeExtension(), MarkdownFormatExtension()])
markdown_attrs['*'] = markdown_attrs['*'] + ['class']
return bleach.clean(parsed_md, tags, markdown_attrs)

View File

@ -180,6 +180,10 @@ class Step(models.Model):
order = models.IntegerField(default=0)
show_as_header = models.BooleanField(default=True)
def get_instruction_render(self):
from cookbook.helper.template_helper import render_instructions
return render_instructions(self)
class Meta:
ordering = ['order', 'pk']

View File

@ -376,7 +376,7 @@
<div class="row" style="margin-top: 8px">
<div class="col-md-12">
{% if s.instruction %}
{{ s.instruction | markdown | safe }}
{{ s.get_instruction_render | safe }}
{% endif %}
</div>
</div>

View File

@ -27,3 +27,4 @@ uritemplate==3.0.1
beautifulsoup4==4.9.3
microdata==0.7.1
django-random-queryset==0.1.3
Jinja2==2.11.0