super basic templating working
This commit is contained in:
19
cookbook/helper/template_helper.py
Normal file
19
cookbook/helper/template_helper.py
Normal 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)
|
@ -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']
|
||||
|
||||
|
@ -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>
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user