Browse Source

New template parameters mechanism.

Improved document generation in songbook.py.
Clean up some code to be more pythonic.
New format for template parameters.
Default template parameter type is now string.
remotes/origin/translate_notes
Alexandre Dupas 14 years ago
parent
commit
67ffa41856
  1. 141
      songbook.py
  2. 2
      songbook.sb
  3. 72
      templates/songbook.tmpl
  4. 14
      test.sb

141
songbook.py

@ -8,96 +8,117 @@ import glob
import re import re
import json import json
def makeTexFile(songbook, output): def matchRegexp(reg, iterable):
return [ m.group(1) for m in (reg.match(l) for l in iterable) if m ]
def songslist(songs):
directories = set(["img/"] + map(lambda x: "songs/" + os.path.dirname(x), songs))
result = ['\\graphicspath{'] + [ ' {{{0}/}},'.format(d) for d in directories ] + ['}'] + [ '\\input{{songs/{0}}}'.format(s.strip()) for s in songs ]
return '\n'.join(result)
def parseTemplate(template):
embeddedJsonPattern = re.compile(r"^%%:")
f = open(template)
code = [ line[3:-1] for line in f if embeddedJsonPattern.match(line) ]
f.close()
data = json.loads(''.join(code))
return data["parameters"]
def toValue(parameter, data):
if "type" not in parameter:
return data
elif parameter["type"] == "stringlist":
if "join" in parameter:
joinText = parameter["join"]
else:
joinText = ''
return joinText.join(data)
def formatDeclaration(name, parameter):
value = ""
if "default" in parameter:
value = parameter["default"]
return '\\def\\set@{name}#1{{\\def\\get{name}{{#1}}}}\n'.format(name=name) + formatDefinition(name, toValue(parameter, value))
def formatDefinition(name, value):
return '\\set@{name}{{{value}}}\n'.format(name=name, value=value)
def makeTexFile(sb, output):
name = output[:-4] name = output[:-4]
# default value # default value
dir = ['img']
template = "songbook.tmpl" template = "songbook.tmpl"
songs = [] songs = []
booktype = ["chorded"]
# parse the songbook data # parse the songbook data
if "template" in songbook: if "template" in sb:
template = songbook["template"] template = sb["template"]
if "songs" in songbook: del sb["template"]
songs = songbook["songs"] if "songs" in sb:
if "booktype" in songbook: songs = sb["songs"]
booktype = songbook["booktype"] del sb["songs"]
parameters = parseTemplate("templates/"+template)
# output relevant fields # output relevant fields
out = open(output, 'w') out = open(output, 'w')
out.write('%% This file has been automatically generated, do not edit!\n')
# output \template out.write('\\makeatletter\n')
out.write('\\newcommand{\\template}{\n') # output automatic parameters
for key in ["title", "author", "subtitle", "version", "mail", "picture", "picturecopyright", "footer", "licence"]: out.write(formatDeclaration("name", {"default":name}))
if key in songbook: out.write(formatDeclaration("songslist", {"type":"stringlist"}))
out.write(' \\'+key+'{{{data}}}\n'.format(data=songbook[key])) # output template parameter command
out.write('}\n') for name, parameter in parameters.iteritems():
# output \booktype out.write(formatDeclaration(name, parameter))
out.write('\\newcommand{{\\booktype}}{{{data}}}'.format(data=','.join(booktype))) # output template parameter values
# output \songlist for name, value in sb.iteritems():
if not type(songs) is list: if name in parameters:
if songs == "all": out.write(formatDefinition(name, toValue(parameters[name],value)))
l = glob.glob('songs/*/*.sg') # output songslist
l.sort() if songs == "all":
songs = map(lambda x: x[6:], l) songs = map(lambda x: x[6:], glob.glob('songs/*/*.sg'))
songs.sort()
if len(songs) > 0: if len(songs) > 0:
out.write('\\newcommand{\\songslist}{\n') out.write(formatDefinition('songslist', songslist(songs)))
dir += map(os.path.dirname, map(lambda x:"songs/"+x, songs)) out.write('\\makeatother\n')
dir = set(dir)
out.write(' \\graphicspath{\n') # output template
for dirname in dir: commentPattern = re.compile(r"^\s*%")
out.write(' {{{imagedir}/}},\n'.format(imagedir=dirname)) f = open("templates/"+template)
out.write(' }\n') content = [ line for line in f if not commentPattern.match(line) ]
for song in songs: f.close()
out.write(' \\input{{songs/{songfile}}}\n'.format(songfile=song.strip())) out.write(''.join(content))
out.write('}\n') out.close()
tmpl = open("templates/"+template)
out.write(tmpl.read().replace("SONGBOOKNAME", name+"_index"))
tmpl.close()
out.close()
def makeDepend(sb, output): def makeDepend(sb, output):
name = output[:-2] name = output[:-2]
# pattern that get dependencies
dependsPattern = re.compile(r"^[^%]*(?:include|input)\{(.*?)\}") dependsPattern = re.compile(r"^[^%]*(?:include|input)\{(.*?)\}")
indexPattern = re.compile(r"^[^%]*\\(?:newauthor|new)index\{.*\}\{(.*?)\}") indexPattern = re.compile(r"^[^%]*\\(?:newauthor|new)index\{.*\}\{(.*?)\}")
lilypondPattern = re.compile(r"^[^%]*\\(?:lilypond)\{(.*?)\}") lilypondPattern = re.compile(r"^[^%]*\\(?:lilypond)\{(.*?)\}")
# check for deps (in sb data) # check for deps (in sb data)
deps = [] deps = matchRegexp(dependsPattern, [ v for v in sb.itervalues() if type(v) is not list ])
if type(sb["songs"]) is list: if sb["songs"] == "all":
deps += map(lambda x: "songs/"+x, sb["songs"]) deps += glob.glob('songs/*/*.sg')
for k in sb.keys(): else:
if not type(sb[k]) is list: deps += map(lambda x: "songs/" + x, sb["songs"])
match = dependsPattern.match(sb[k])
if match:
deps += [match.group(1)]
# check for lilypond deps (in songs data) if necessary # check for lilypond deps (in songs data) if necessary
lilypond = [] lilypond = []
if "booktype" in sb.keys() and "lilypond" in sb["booktype"]: if "booktype" in sb and "lilypond" in sb["booktype"]:
for filename in deps: for filename in deps:
tmpl = open(filename) tmpl = open(filename)
for l in tmpl: lilypond += matchRegexp(lilypondPattern, tmpl)
match = lilypondPattern.match(l)
if match:
lilypond.append(match.group(1))
tmpl.close() tmpl.close()
# check for index (in template file) # check for index (in template file)
if "template" in sb: if "template" in sb:
filename = "templates/"+sb["template"] filename = sb["template"]
else: else:
filename = "templates/songbook.tmpl" filename = "songbook.tmpl"
idx = [] tmpl = open("templates/"+filename)
tmpl = open(filename) idx = map(lambda x: x.replace("\getname", name), matchRegexp(indexPattern, tmpl))
for l in tmpl:
match = indexPattern.match(l)
if match:
idx.append(match.group(1).replace("SONGBOOKNAME", name+"_index"))
tmpl.close() tmpl.close()
# write .d file # write .d file

2
songbook.sb

@ -7,6 +7,6 @@
"picture" : "feel-the-music", "picture" : "feel-the-music",
"picturecopyright" : "foxygamergirl @ deviantart.com", "picturecopyright" : "foxygamergirl @ deviantart.com",
"footer" : "\\begin{flushleft}\\includegraphics[width=3cm]{on-fire}\\end{flushleft}", "footer" : "\\begin{flushleft}\\includegraphics[width=3cm]{on-fire}\\end{flushleft}",
"licence" : "\\input{license.tex}", "license" : "\\input{license.tex}",
"songs" : "all" "songs" : "all"
} }

72
templates/songbook.tmpl

@ -21,53 +21,40 @@
% %
% Modified to serve personnal purposes. Newer versions can be % Modified to serve personnal purposes. Newer versions can be
% obtained from http://www.lohrun.net. % obtained from http://www.lohrun.net.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Template optionnal parameters (to be read by songbook-client) % Template parameters
% template mandatory parameter are currently booktype, template and songs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the first two have default values but not last one %%:{"parameters" : {
%%:{"parameters" : [ %%: "title" : {"description":"Title", "default":"Recueil de chansons pour guitare"},
%%: {"name":"title", "description":"Title", "type":"string"}, %%: "author" : {"description":"Author", "default":"Romain Goffe \\and Alexandre Dupas"},
%%: {"name":"author", "description":"Author", "type":"string", "default":"Alexandre"}, %%: "version" : {"description":"Version", "default":"3.1"},
%%: {"name":"version", "description":"Version", "type":"string", "default":"1"}, %%: "subtitle" : {"description":"Subtitle"},
%%: {"name":"subtitle", "description":"Subtitle", "type":"string"}, %%: "mail" : {"description":"Email", "default":"crep@team-on-fire.com"},
%%: {"name":"mail", "description":"Email", "type":"string"}, %%: "picture" : {"description":"Picture", "default":"feel-the-music"},
%%: {"name":"picture", "description":"Picture", "type":"string"}, %%: "picturecopyright" : {"description":"Copyright", "default":"foxygamergirl@deviantart.com"},
%%: {"name":"picturecopyright", "description":"Copyright", "type":"string"}, %%: "footer" : {"description":"Footer", "default":"\\begin{flushleft}\\includegraphics[width=3cm]{on-fire}\\end{flushleft}"},
%%: {"name":"footer", "description":"Footer", "type":"string"}, %%: "license" : {"description":"License", "default":"\\input{license.tex}"},
%%: {"name":"license", "description":"License", "type":"string"} %%: "booktype" : {"description":"Booktype", "type":"stringlist", "default":["chorded"], "join":","}
%%: ] %%: }
%%:} %%:}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% template variables
\providecommand{\template}{}
\providecommand{\songslist}{}
\providecommand{\booktype}{chorded}
% default template
\newcommand{\defaulttemplate}{
\title{Recueil de chansons pour guitare}
\author{Romain Goffe \and Alexandre Dupas}
\subtitle{}
\version{3.1}
\mail{crep@team-on-fire.com}
\picture{feel-the-music}
\picturecopyright{©foxygamergirl @ deviantart.com}
\footer{
\begin{flushleft}
\includegraphics[width=3cm]{on-fire}
\end{flushleft}
}
\licence{\input{license.tex}}
}
% begin document % begin document
\documentclass[\booktype]{crepbook} \documentclass[\getbooktype]{crepbook}
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage[english,french]{babel} \usepackage[english,french]{babel}
\defaulttemplate \title{\gettitle}
\template \author{\getauthor}
\subtitle{\getsubtitle}
\version{\getversion}
\mail{\getmail}
\picture{\getpicture}
\picturecopyright{\getpicturecopyright}
\footer{\getfooter}
\licence{\getlicense}
\newindex{titleidx}{SONGBOOKNAME_title} \newindex{titleidx}{\getname_title}
\newauthorindex{authidx}{SONGBOOKNAME_auth} \newauthorindex{authidx}{\getname_auth}
\graphicspath{ \graphicspath{
{img/}, {img/},
@ -82,8 +69,9 @@
\songsection{Chansons} \songsection{Chansons}
\begin{songs}{titleidx,authidx} \begin{songs}{titleidx,authidx}
\songslist \getsongslist
\end{songs} \end{songs}
\end{document} \end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% end document % end document

14
test.sb

@ -1,9 +1,13 @@
{ {
"template" : "songbook.tmpl", "template" : "songbook.tmpl",
"booktype" : ["lyric"], "author" : "Alex",
"title" : "Ceci est un test du template", "version" : "1",
"booktype" : [
"chorded",
"lilypond"
],
"songs" : [ "songs" : [
"Le_Donjon_de_Naheulbeuk/Geste_heroique.sg", "Le_Donjon_de_Naheulbeuk/La_biere_du_donjon.sg",
"Le_Donjon_de_Naheulbeuk/La_biere_du_donjon.sg" "Le_Donjon_de_Naheulbeuk/Geste_heroique.sg"
] ]
} }

Loading…
Cancel
Save