Browse Source

Merge branch 'next' of github.com:patacrep/songbook-core into next

pull/20/head
Louis 11 years ago
parent
commit
4afa826480
  1. 34
      songbook_core/build.py
  2. 26
      songbook_core/data/templates/default.tex
  3. 52
      songbook_core/data/templates/patacrep.tex
  4. 0
      songbook_core/plastex.py
  5. 65
      songbook_core/templates.py

34
songbook_core/build.py

@ -5,7 +5,6 @@
import codecs import codecs
import glob import glob
import json
import logging import logging
import os.path import os.path
import re import re
@ -26,28 +25,6 @@ DEFAULT_AUTHWORDS = {
} }
def parse_template(template):
"""Return the dict of default parameters defined in the template."""
embedded_json_pattern = re.compile(r"^%%:")
with open(template) as template_file:
code = [
line[3:-1]
for line
in template_file
if embedded_json_pattern.match(line)
]
parameters = dict()
if code:
data = json.loads(''.join(code))
for param in data:
try:
parameters[param["name"]] = param["default"]
except KeyError:
parameters[param["name"]] = None
return parameters
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class Songbook(object): class Songbook(object):
"""Represent a songbook (.sb) file. """Represent a songbook (.sb) file.
@ -86,12 +63,9 @@ class Songbook(object):
Song.authwords['ignore'] = self.config['authwords']['ignore'] Song.authwords['ignore'] = self.config['authwords']['ignore']
Song.authwords['sep'] = [ Song.authwords['sep'] = [
re.compile(r"^(.*)%s (.*)$" % sep) re.compile(r"^(.*)%s (.*)$" % sep)
for sep for sep in ([
in ([ " %s" % sep for sep in self.config['authwords']["sep"]
" %s" % sep ] + [','])
for sep
in self.config['authwords']["sep"]
] + [','])
] ]
def _parse(self, raw_songbook): def _parse(self, raw_songbook):
@ -135,7 +109,7 @@ class Songbook(object):
self.config['datadir'], self.config['datadir'],
) )
context = parse_template(renderer.file_template()) context = renderer.get_variables()
context.update(self.config) context.update(self.config)
context['titleprefixkeys'] = ["after", "sep", "ignore"] context['titleprefixkeys'] = ["after", "sep", "ignore"]

26
songbook_core/data/templates/default.tex

@ -1,6 +1,30 @@
(* set indexes = "titleidx,authidx" *) (% variables %)
[
{"name":"title", "description":"Title", "default":"Recueil de chansons pour guitare", "mandatory":true},
{"name":"author", "description":"Author", "default":"The Songbook Team", "mandatory":true},
{"name":"booktype", "description":"Type", "type":"enum", "values":["chorded","lyric"], "default":"chorded", "mandatory":true},
{"name":"lang", "description":"Language", "default":"english"},
{"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]},
{"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["diagram","pictures"]},
{"name":"version", "description":"Version", "default":"unknown"},
{"name":"subtitle", "description":"Subtitle"},
{"name":"web", "description":"Web", "default":"http://www.patacrep.com"},
{"name":"mail", "description":"Email", "default":"crep@team-on-fire.com"},
{"name":"picture", "description":"Picture", "type":"file", "default":"treble_a"},
{"name":"picturecopyright", "description":"Copyright", "default":"Dbolton \\url{http://commons.wikimedia.org/wiki/User:Dbolton}"},
{"name":"footer", "description":"Footer", "default":"\\begin{flushright}Generated using Songbook (\\url{http://www.patacrep.com})\\end{flushright}"},
{"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"},
{"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"D1E4AE"},
{"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"D1E4AE"},
{"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"D1E4AE"},
{"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"},
{"name":"authwords", "descriptipn":"Set of options to process author string (LaTeX commands authsepword, authignoreword, authbyword)"},
{"name":"languages", "description":"List of languages used by songs", "default":"english"}
]
(% endvariables %)
(* extends "songs.tex" *) (* extends "songs.tex" *)
(* set indexes = "titleidx,authidx" *)
(* block songbookpreambule *) (* block songbookpreambule *)
(( super() )) (( super() ))

52
songbook_core/data/templates/patacrep.tex

@ -1,31 +1,27 @@
(% comment %) (% variables %)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [
% Template parameters {"name":"title", "description":"Title", "default":"Recueil de chansons pour guitare", "mandatory":true},
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% {"name":"author", "description":"Author", "default":"The Songbook Team", "mandatory":true},
%%:[ {"name":"booktype", "description":"Type", "type":"enum", "values":["chorded","lyric"], "default":"chorded", "mandatory":true},
%%: {"name":"title", "description":"Title", "default":"Recueil de chansons pour guitare", "mandatory":true}, {"name":"lang", "description":"Language", "default":"english"},
%%: {"name":"author", "description":"Author", "default":"The Songbook Team", "mandatory":true}, {"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]},
%%: {"name":"booktype", "description":"Type", "type":"enum", "values":["chorded","lyric"], "default":"chorded", "mandatory":true}, {"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["diagram","pictures"]},
%%: {"name":"lang", "description":"Language", "default":"english"}, {"name":"version", "description":"Version", "default":"unknown"},
%%: {"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]}, {"name":"subtitle", "description":"Subtitle"},
%%: {"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["diagram","pictures"]}, {"name":"web", "description":"Web", "default":"http://www.patacrep.com"},
%%: {"name":"version", "description":"Version", "default":"unknown"}, {"name":"mail", "description":"Email", "default":"crep@team-on-fire.com"},
%%: {"name":"subtitle", "description":"Subtitle"}, {"name":"picture", "description":"Picture", "type":"file", "default":"treble_a"},
%%: {"name":"web", "description":"Web", "default":"http://www.patacrep.com"}, {"name":"picturecopyright", "description":"Copyright", "default":"Dbolton \\url{http://commons.wikimedia.org/wiki/User:Dbolton}"},
%%: {"name":"mail", "description":"Email", "default":"crep@team-on-fire.com"}, {"name":"footer", "description":"Footer", "default":"\\begin{flushright}Generated using Songbook (\\url{http://www.patacrep.com})\\end{flushright}"},
%%: {"name":"picture", "description":"Picture", "type":"file", "default":"treble_a"}, {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"},
%%: {"name":"picturecopyright", "description":"Copyright", "default":"Dbolton \\url{http://commons.wikimedia.org/wiki/User:Dbolton}"}, {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"D1E4AE"},
%%: {"name":"footer", "description":"Footer", "default":"\\begin{flushright}Generated using Songbook (\\url{http://www.patacrep.com})\\end{flushright}"}, {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"D1E4AE"},
%%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"}, {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"D1E4AE"},
%%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"D1E4AE"}, {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"},
%%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"D1E4AE"}, {"name":"authwords", "descriptipn":"Set of options to process author string (LaTeX commands authsepword, authignoreword, authbyword)"},
%%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"D1E4AE"}, {"name":"languages", "description":"List of languages used by songs", "default":"english"}
%%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"}, ]
%%: {"name":"authwords", "descriptipn":"Set of options to process author string (LaTeX commands authsepword, authignoreword, authbyword)"}, (% endvariables %)
%%: {"name":"languages", "description":"List of languages used by songs", "default":"english"}
%%:]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(% endcomment %)
(* extends "default.tex" *) (* extends "default.tex" *)

0
songbook_core/plastex.py

65
songbook_core/templates.py

@ -3,8 +3,10 @@
"""Template for .tex generation settings and utilities""" """Template for .tex generation settings and utilities"""
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PackageLoader from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PackageLoader
from jinja2.meta import find_referenced_templates as find_templates
import os import os
import re import re
import json
_LATEX_SUBS = ( _LATEX_SUBS = (
(re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'\\'), r'\\textbackslash'),
@ -23,6 +25,7 @@ def _escape_tex(value):
newval = pattern.sub(replacement, newval) newval = pattern.sub(replacement, newval)
return newval return newval
class TexRenderer(object): class TexRenderer(object):
"""Render a template to a LaTeX file.""" """Render a template to a LaTeX file."""
@ -32,8 +35,6 @@ class TexRenderer(object):
Arguments: Arguments:
- datadir: location of the user-defined templates - datadir: location of the user-defined templates
''' '''
self.template = template
self.texenv = Environment( self.texenv = Environment(
loader=ChoiceLoader([ loader=ChoiceLoader([
FileSystemLoader( FileSystemLoader(
@ -42,31 +43,69 @@ class TexRenderer(object):
PackageLoader( PackageLoader(
'songbook_core', os.path.join('data', 'templates') 'songbook_core', os.path.join('data', 'templates')
), ),
]) ]),
) )
self.texenv.block_start_string = '(*' self.texenv.block_start_string = '(*'
self.texenv.block_end_string = '*)' self.texenv.block_end_string = '*)'
self.texenv.variable_start_string = '((' self.texenv.variable_start_string = '(('
self.texenv.variable_end_string = '))' self.texenv.variable_end_string = '))'
self.texenv.comment_start_string = '(% comment %)' self.texenv.comment_start_string = '(% variables %)'
self.texenv.comment_end_string = '(% endcomment %)' self.texenv.comment_end_string = '(% endvariables %)'
self.texenv.line_comment_prefix = '(%%)'
self.texenv.filters['escape_tex'] = _escape_tex self.texenv.filters['escape_tex'] = _escape_tex
self.texenv.trim_blocks = True self.texenv.trim_blocks = True
self.texenv.lstrip_blocks = True self.texenv.lstrip_blocks = True
def file_template(self): # TODO: catch the TemplateNotFound
"""Return the filename of the selected template.""" self.template = self.texenv.get_template(template)
return self.texenv.get_template(self.template).filename
def get_variables(self):
data = self.parse_templates()
variables = dict()
for param in data:
try:
variables[param["name"]] = param["default"]
except KeyError:
variables[param["name"]] = None
return variables
def parse_templates(self):
templates = self.get_templates(self.template)
templates |= set([self.template.name])
variables = []
regex = re.compile(r'\(% variables %\)(?P<variables>.*)'
'\(% endvariables %\)', re.DOTALL)
for template_name in templates:
filename = self.texenv.get_template(template_name).filename
with open(filename, 'r') as template_file:
content = template_file.read()
match = re.search(regex, content)
if match:
content = match.group('variables')
variables += json.loads(content)
return variables
def get_templates(self, template):
'''Recursively get a set of all the templates used
by a particular template.
'''
with open(template.filename, 'r') as template_file:
content = template_file.readlines()
new_templates = list(find_templates(self.texenv.parse(content)))
all_templates = set(new_templates)
if len(new_templates) > 0:
for new_template_name in new_templates:
new_template = self.texenv.get_template(new_template_name)
# union of the sets
all_templates |= self.get_templates(new_template)
return all_templates
def render_tex(self, output, context): def render_tex(self, output, context):
'''Render a template into a .tex file '''Render a template into a .tex file
Arguments: Arguments:
- output: a file object to write the result - output: a file object to write the result
- context: all the data to populate the template - context: a dict of all the data to populate the template
''' '''
#pylint: disable=star-args output.write(self.template.render(context))
output.write(
self.texenv.get_template(self.template).render(**context)
)

Loading…
Cancel
Save