|
@ -1,6 +1,8 @@ |
|
|
#!/usr/bin/env python |
|
|
#!/usr/bin/env python |
|
|
# -*- coding: utf-8 -*- |
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
"""Build a songbook, according to parameters found in a .sb file.""" |
|
|
|
|
|
|
|
|
from subprocess import call |
|
|
from subprocess import call |
|
|
import codecs |
|
|
import codecs |
|
|
import glob |
|
|
import glob |
|
@ -16,9 +18,15 @@ from songbook import __SHAREDIR__ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseTemplate(template): |
|
|
def parseTemplate(template): |
|
|
|
|
|
"""Return the list of parameters defined in the template.""" |
|
|
embeddedJsonPattern = re.compile(r"^%%:") |
|
|
embeddedJsonPattern = re.compile(r"^%%:") |
|
|
with open(template) as template_file: |
|
|
with open(template) as template_file: |
|
|
code = [line[3:-1] for line in template_file if embeddedJsonPattern.match(line)] |
|
|
code = [ |
|
|
|
|
|
line[3:-1] |
|
|
|
|
|
for line |
|
|
|
|
|
in template_file |
|
|
|
|
|
if embeddedJsonPattern.match(line) |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
data = json.loads(''.join(code)) |
|
|
data = json.loads(''.join(code)) |
|
|
parameters = dict() |
|
|
parameters = dict() |
|
@ -56,7 +64,8 @@ def formatDeclaration(name, parameter): |
|
|
value = "" |
|
|
value = "" |
|
|
if "default" in parameter: |
|
|
if "default" in parameter: |
|
|
value = parameter["default"] |
|
|
value = parameter["default"] |
|
|
return ('\\def\\set@{name}#1{{\\def\\get{name}{{#1}}}}\n'.format(name=name) |
|
|
return ( |
|
|
|
|
|
'\\def\\set@{name}#1{{\\def\\get{name}{{#1}}}}\n'.format(name=name) |
|
|
+ formatDefinition(name, toValue(parameter, value)) |
|
|
+ formatDefinition(name, toValue(parameter, value)) |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -66,6 +75,10 @@ def formatDefinition(name, value): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean(basename): |
|
|
def clean(basename): |
|
|
|
|
|
"""Clean (some) temporary files used during compilation. |
|
|
|
|
|
|
|
|
|
|
|
Depending of the LaTeX modules used in the template, there may be others |
|
|
|
|
|
that are note deleted by this function.""" |
|
|
generated_extensions = [ |
|
|
generated_extensions = [ |
|
|
"_auth.sbx", |
|
|
"_auth.sbx", |
|
|
"_auth.sxd", |
|
|
"_auth.sxd", |
|
@ -85,6 +98,7 @@ def clean(basename): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def makeTexFile(sb, output): |
|
|
def makeTexFile(sb, output): |
|
|
|
|
|
"""Create the LaTeX file corresponding to the .sb file given in argument.""" |
|
|
datadir = sb['datadir'] |
|
|
datadir = sb['datadir'] |
|
|
name = output[:-4] |
|
|
name = output[:-4] |
|
|
template_dir = os.path.join(datadir, 'templates') |
|
|
template_dir = os.path.join(datadir, 'templates') |
|
@ -178,13 +192,25 @@ def makeTexFile(sb, output): |
|
|
|
|
|
|
|
|
# output template |
|
|
# output template |
|
|
commentPattern = re.compile(r"^\s*%") |
|
|
commentPattern = re.compile(r"^\s*%") |
|
|
with codecs.open(os.path.join(template_dir, template), 'r', 'utf-8') as template_file: |
|
|
with codecs.open( |
|
|
content = [line for line in template_file if not commentPattern.match(line)] |
|
|
os.path.join(template_dir, template), 'r', 'utf-8' |
|
|
|
|
|
) as template_file: |
|
|
|
|
|
content = [ |
|
|
|
|
|
line |
|
|
|
|
|
for line |
|
|
|
|
|
in template_file |
|
|
|
|
|
if not commentPattern.match(line) |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
for index, line in enumerate(content): |
|
|
for index, line in enumerate(content): |
|
|
if re.compile("getDataImgDirectory").search(line): |
|
|
if re.compile("getDataImgDirectory").search(line): |
|
|
if os.path.abspath(os.path.join(datadir, "img")).startswith(os.path.abspath(os.path.dirname(output))): |
|
|
if os.path.abspath(os.path.join(datadir, "img")).startswith( |
|
|
imgdir = os.path.relpath(os.path.join(datadir, "img"), os.path.dirname(output)) |
|
|
os.path.abspath(os.path.dirname(output)) |
|
|
|
|
|
): |
|
|
|
|
|
imgdir = os.path.relpath( |
|
|
|
|
|
os.path.join(datadir, "img"), |
|
|
|
|
|
os.path.dirname(output) |
|
|
|
|
|
) |
|
|
else: |
|
|
else: |
|
|
imgdir = os.path.abspath(os.path.join(datadir, "img")) |
|
|
imgdir = os.path.abspath(os.path.join(datadir, "img")) |
|
|
line = line.replace("\\getDataImgDirectory", ' {%s/} ' % imgdir) |
|
|
line = line.replace("\\getDataImgDirectory", ' {%s/} ' % imgdir) |
|
|