Browse Source

[WIP] indexes work again

pull/47/head
Louis 11 years ago
parent
commit
2f3d57b8a2
  1. 23
      songbook_core/content/__init__.py
  2. 2
      songbook_core/content/section.py
  3. 16
      songbook_core/content/song.py
  4. 2
      songbook_core/content/songsection.py

23
songbook_core/content/__init__.py

@ -3,6 +3,7 @@
import glob import glob
import importlib import importlib
import jinja2
import logging import logging
import os import os
@ -14,7 +15,7 @@ EOL = '\n'
class Content: class Content:
"""Content item of type 'example'.""" """Content item of type 'example'."""
def render(self): def render(self, context):
"""Render this content item. """Render this content item.
Returns a string, to be placed verbatim in the generated .tex file. Returns a string, to be placed verbatim in the generated .tex file.
@ -23,12 +24,13 @@ class Content:
# Block management # 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. """Return a boolean stating if a new block is to be created.
# Arguments # Arguments
- previous: the songbook.content.Content object of the previous item. - previous: the songbook.content.Content object of the previous item.
- context: TODO
# Return # Return
@ -37,11 +39,11 @@ class Content:
""" """
return True return True
def begin_block(self): def begin_block(self, context):
"""Return the string to begin a block.""" """Return the string to begin a block."""
return "" return ""
def end_block(self): def end_block(self, context):
"""Return the string to end a block.""" """Return the string to end a block."""
return "" return ""
@ -73,7 +75,8 @@ def load_plugins():
plugins[key] = value plugins[key] = value
return plugins return plugins
def render_content(content): @jinja2.contextfunction
def render_content(context, content):
rendered = "" rendered = ""
previous = None previous = None
last = None last = None
@ -83,15 +86,15 @@ def render_content(content):
continue continue
last = elem last = elem
if elem.begin_new_block(previous): if elem.begin_new_block(previous, context):
if previous: if previous:
rendered += previous.end_block() + EOL rendered += previous.end_block(context) + EOL
rendered += elem.begin_block() + EOL rendered += elem.begin_block(context) + EOL
rendered += elem.render() + EOL rendered += elem.render(context) + EOL
previous = elem previous = elem
if isinstance(last, Content): if isinstance(last, Content):
rendered += last.end_block() + EOL rendered += last.end_block(context) + EOL
return rendered return rendered

2
songbook_core/content/section.py

@ -20,7 +20,7 @@ class Section(Content):
self.name = name self.name = name
self.short = short self.short = short
def render(self): def render(self, __context):
if (self.short is None): if (self.short is None):
return r'\{}{{{}}}'.format(self.keyword, self.name) return r'\{}{{{}}}'.format(self.keyword, self.name)
else: else:

16
songbook_core/content/song.py

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import glob import glob
import jinja2
import os import os
from songbook_core.content import Content from songbook_core.content import Content
@ -11,23 +12,24 @@ class Song(Content):
def __init__(self, filename): def __init__(self, filename):
self.filename = filename self.filename = filename
def begin_new_block(self, previous): def begin_new_block(self, previous, __context):
return not isinstance(previous, Song) return not isinstance(previous, Song)
def begin_block(self): def begin_block(self, context):
#TODO index return r'\begin{songs}{((indexes|default("")))}' indexes = context.resolve("indexes")
return r'\begin{songs}{titleidx,authidx}' 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}' return r'\end{songs}'
def render(self): def render(self, __context):
return r'\input{{{}}}'.format(self.filename) return r'\input{{{}}}'.format(self.filename)
def parse(keyword, config, *arguments): def parse(keyword, config, *arguments):
songlist = [] songlist = []
if not arguments: if not arguments:
import ipdb; ipdb.set_trace()
arguments = [ arguments = [
os.path.relpath( os.path.relpath(
filename, filename,

2
songbook_core/content/songsection.py

@ -13,7 +13,7 @@ class SongSection(Content):
self.keyword = keyword self.keyword = keyword
self.name = name self.name = name
def render(self): def render(self, __context):
return r'\{}{{{}}}'.format(self.keyword, self.name) return r'\{}{{{}}}'.format(self.keyword, self.name)
def parse(keyword, config, *arguments): def parse(keyword, config, *arguments):

Loading…
Cancel
Save