diff --git a/songbook_core/content/__init__.py b/songbook_core/content/__init__.py index 27c5967a..3b908868 100644 --- a/songbook_core/content/__init__.py +++ b/songbook_core/content/__init__.py @@ -3,6 +3,7 @@ import glob import importlib +import jinja2 import logging import os @@ -14,7 +15,7 @@ EOL = '\n' class Content: """Content item of type 'example'.""" - def render(self): + def render(self, context): """Render this content item. Returns a string, to be placed verbatim in the generated .tex file. @@ -23,12 +24,13 @@ class Content: # Block management - def begin_new_block(self, previous): + def begin_new_block(self, previous, context): """Return a boolean stating if a new block is to be created. # Arguments - previous: the songbook.content.Content object of the previous item. + - context: TODO # Return @@ -37,11 +39,11 @@ class Content: """ return True - def begin_block(self): + def begin_block(self, context): """Return the string to begin a block.""" return "" - def end_block(self): + def end_block(self, context): """Return the string to end a block.""" return "" @@ -73,7 +75,8 @@ def load_plugins(): plugins[key] = value return plugins -def render_content(content): +@jinja2.contextfunction +def render_content(context, content): rendered = "" previous = None last = None @@ -83,15 +86,15 @@ def render_content(content): continue last = elem - if elem.begin_new_block(previous): + if elem.begin_new_block(previous, context): if previous: - rendered += previous.end_block() + EOL - rendered += elem.begin_block() + EOL - rendered += elem.render() + EOL + rendered += previous.end_block(context) + EOL + rendered += elem.begin_block(context) + EOL + rendered += elem.render(context) + EOL previous = elem if isinstance(last, Content): - rendered += last.end_block() + EOL + rendered += last.end_block(context) + EOL return rendered diff --git a/songbook_core/content/section.py b/songbook_core/content/section.py index 8fb19527..f2cf4e89 100644 --- a/songbook_core/content/section.py +++ b/songbook_core/content/section.py @@ -20,7 +20,7 @@ class Section(Content): self.name = name self.short = short - def render(self): + def render(self, __context): if (self.short is None): return r'\{}{{{}}}'.format(self.keyword, self.name) else: diff --git a/songbook_core/content/song.py b/songbook_core/content/song.py index b9b13858..447d20b3 100644 --- a/songbook_core/content/song.py +++ b/songbook_core/content/song.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import glob +import jinja2 import os from songbook_core.content import Content @@ -11,23 +12,24 @@ class Song(Content): def __init__(self, filename): self.filename = filename - def begin_new_block(self, previous): + def begin_new_block(self, previous, __context): return not isinstance(previous, Song) - def begin_block(self): - #TODO index return r'\begin{songs}{((indexes|default("")))}' - return r'\begin{songs}{titleidx,authidx}' + def begin_block(self, context): + indexes = context.resolve("indexes") + if isinstance(indexes, jinja2.runtime.Undefined): + indexes = "" + return r'\begin{songs}{%s}' % indexes - def end_block(self): + def end_block(self, __context): return r'\end{songs}' - def render(self): + def render(self, __context): return r'\input{{{}}}'.format(self.filename) def parse(keyword, config, *arguments): songlist = [] if not arguments: - import ipdb; ipdb.set_trace() arguments = [ os.path.relpath( filename, diff --git a/songbook_core/content/songsection.py b/songbook_core/content/songsection.py index 61d7b88d..c27a3a31 100644 --- a/songbook_core/content/songsection.py +++ b/songbook_core/content/songsection.py @@ -13,7 +13,7 @@ class SongSection(Content): self.keyword = keyword self.name = name - def render(self): + def render(self, __context): return r'\{}{{{}}}'.format(self.keyword, self.name) def parse(keyword, config, *arguments):