From 5ab211315ea73127c49c8304d51893931aa495cb Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 19 Sep 2015 19:55:54 +0200 Subject: [PATCH] Improved sub-template rendering Using the existing jinja rendere, instead of re-instantiting a new one. --- patacrep/songs/chordpro/__init__.py | 34 ++++++++++------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index cd9bfd4e..42011244 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -14,7 +14,6 @@ class ChordproSong(Song): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.jinjaenv = None def parse(self, config): """Parse content, and return the dictionary of song data.""" @@ -37,37 +36,28 @@ class ChordproSong(Song): "metadata": self.data, "render": self._render_ast, "config": self.config, + "content": self.cached['song'].content, } - self.jinjaenv = Environment(loader=FileSystemLoader(os.path.join( + jinjaenv = Environment(loader=FileSystemLoader(os.path.join( os.path.abspath(pkg_resources.resource_filename(__name__, 'data')), output_format, ))) + jinjaenv.filters['search_image'] = self.search_image + jinjaenv.filters['search_partition'] = self.search_partition - self.jinjaenv.filters['search_image'] = self.search_image - - self.jinjaenv.filters['search_partition'] = self.search_partition - - return self._render_ast( - context, - self.cached['song'].content, - template=template, - ) - - @contextfunction - def _render_ast(self, context, content, template=None): - """Render ``content``.""" - if isinstance(context, dict): - context['content'] = content - else: - context.vars['content'] = content - if template is None: - template = content.template() return Renderer( template=template, encoding='utf8', - jinjaenv=self.jinjaenv, + jinjaenv=jinjaenv, ).template.render(context) + @staticmethod + @contextfunction + def _render_ast(context, content): + """Render ``content``.""" + context.vars['content'] = content + return context.environment.get_template(content.template()).render(context) + SONG_PARSERS = { 'sgc': ChordproSong, }