Browse Source

Improved sub-template rendering

Using the existing jinja rendere, instead of re-instantiting a new one.
pull/103/head
Louis 9 years ago
parent
commit
5ab211315e
  1. 32
      patacrep/songs/chordpro/__init__.py

32
patacrep/songs/chordpro/__init__.py

@ -14,7 +14,6 @@ class ChordproSong(Song):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.jinjaenv = None
def parse(self, config): def parse(self, config):
"""Parse content, and return the dictionary of song data.""" """Parse content, and return the dictionary of song data."""
@ -37,36 +36,27 @@ class ChordproSong(Song):
"metadata": self.data, "metadata": self.data,
"render": self._render_ast, "render": self._render_ast,
"config": self.config, "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')), os.path.abspath(pkg_resources.resource_filename(__name__, 'data')),
output_format, output_format,
))) )))
jinjaenv.filters['search_image'] = self.search_image
jinjaenv.filters['search_partition'] = self.search_partition
self.jinjaenv.filters['search_image'] = self.search_image return Renderer(
self.jinjaenv.filters['search_partition'] = self.search_partition
return self._render_ast(
context,
self.cached['song'].content,
template=template, template=template,
) encoding='utf8',
jinjaenv=jinjaenv,
).template.render(context)
@staticmethod
@contextfunction @contextfunction
def _render_ast(self, context, content, template=None): def _render_ast(context, content):
"""Render ``content``.""" """Render ``content``."""
if isinstance(context, dict):
context['content'] = content
else:
context.vars['content'] = content context.vars['content'] = content
if template is None: return context.environment.get_template(content.template()).render(context)
template = content.template()
return Renderer(
template=template,
encoding='utf8',
jinjaenv=self.jinjaenv,
).template.render(context)
SONG_PARSERS = { SONG_PARSERS = {
'sgc': ChordproSong, 'sgc': ChordproSong,

Loading…
Cancel
Save