mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
use Sandbox Environment to render templates
This commit is contained in:
@@ -3,6 +3,8 @@ from gettext import gettext as _
|
|||||||
import bleach
|
import bleach
|
||||||
import markdown as md
|
import markdown as md
|
||||||
from jinja2 import Template, TemplateSyntaxError, UndefinedError
|
from jinja2 import Template, TemplateSyntaxError, UndefinedError
|
||||||
|
from jinja2.exceptions import SecurityError
|
||||||
|
from jinja2.sandbox import SandboxedEnvironment
|
||||||
from markdown.extensions.tables import TableExtension
|
from markdown.extensions.tables import TableExtension
|
||||||
|
|
||||||
from cookbook.helper.mdx_attributes import MarkdownFormatExtension
|
from cookbook.helper.mdx_attributes import MarkdownFormatExtension
|
||||||
@@ -89,11 +91,13 @@ def render_instructions(step): # TODO deduplicate markdown cleanup code
|
|||||||
return f"<scalable-number v-bind:number='{bleach.clean(str(number))}' v-bind:factor='ingredient_factor'></scalable-number>"
|
return f"<scalable-number v-bind:number='{bleach.clean(str(number))}' v-bind:factor='ingredient_factor'></scalable-number>"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
template = Template(instructions)
|
env = SandboxedEnvironment()
|
||||||
instructions = template.render(ingredients=ingredients, scale=scale)
|
instructions = env.from_string(instructions).render(ingredients=ingredients, scale=scale)
|
||||||
except TemplateSyntaxError:
|
except TemplateSyntaxError:
|
||||||
return _('Could not parse template code.') + ' Error: Template Syntax broken'
|
return _('Could not parse template code.') + ' Error: Template Syntax broken'
|
||||||
except UndefinedError:
|
except UndefinedError:
|
||||||
return _('Could not parse template code.') + ' Error: Undefined Error'
|
return _('Could not parse template code.') + ' Error: Undefined Error'
|
||||||
|
except SecurityError:
|
||||||
|
return _('Could not parse template code.') + ' Error: Security Error'
|
||||||
|
|
||||||
return instructions
|
return instructions
|
||||||
|
|||||||
Reference in New Issue
Block a user