better relative imports and load_yaml in tempaltes

master
Ugo Finnendahl 5 years ago
parent 1879691cbc
commit f25f9f264d
  1. 27
      bin/pretex

@ -7,7 +7,7 @@ With slightly different syntax:
# ... ## => %$ ... %# for Line Statements # ... ## => %$ ... %# for Line Statements
Usage: Usage:
pretex [<templates>] --data=<yamlfile> [options] pretex [<templates>] [options]
Arguments: Arguments:
<templates> Compiles every given file ending with ".j2.tex" into <templates> Compiles every given file ending with ".j2.tex" into
@ -27,7 +27,7 @@ Options:
[default: {TEX} {ARG} {document}.tex, {BIB} {document}, {TEX} {ARG} {document}.tex, {TEX} {ARG} {document}.tex] [default: {TEX} {ARG} {document}.tex, {BIB} {document}, {TEX} {ARG} {document}.tex, {TEX} {ARG} {document}.tex]
--clean Clean aux files. --clean Clean aux files.
-f --files=<end> Files to clean. -f --files=<end> Files to clean.
[default: *.aux, *.bbl, *.blg, *.idx, *.ind, *.lof, *.lot, *.out, *.toc, *.acn, *.acr, *.alg, *.glg, *.glo, *.gls, *.ist, *.fls, *.log, *.fdb_latexmk, *.synctex.gz] [default: *.aux, *.bbl, *.blg, *.idx, *.ind, *.lof, *.lot, *.out, *.toc, *.acn, *.acr, *.alg, *.glg, *.glo, *.gls, *.ist, *.fls, *.log, *.fdb_latexmk]
""" """
import yaml import yaml
@ -89,7 +89,10 @@ def compile_all(data, templates, pdf, cmd, copy, clean):
continue continue
path, filename = os.path.split(compiled_file) path, filename = os.path.split(compiled_file)
for command in cmd(filename[:-4]): for command in cmd(filename[:-4]):
subprocess.call(shlex.split(command), cwd=path, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) cwd = path
if cwd == '':
cwd = None
subprocess.call(shlex.split(command), cwd=cwd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if copy: if copy:
if not os.path.exists(copy): if not os.path.exists(copy):
@ -103,8 +106,10 @@ def compile_all(data, templates, pdf, cmd, copy, clean):
def compile_file(tex_file, data, file=None): def compile_file(tex_file, data, file=None):
template = latex_jinja_env.get_template(tex_file) template = latex_jinja_env.get_template(os.path.abspath(tex_file))
rendered = template.render(**data)
path, filename = os.path.split(tex_file)
rendered = template.render(**data, load_yaml=functools.partial(load_yaml, path=path))
if file is None: if file is None:
return rendered return rendered
f = open(file, 'w') f = open(file, 'w')
@ -112,6 +117,10 @@ def compile_file(tex_file, data, file=None):
f.close() f.close()
return f'"{tex_file}" successful rendered into "{file}"' return f'"{tex_file}" successful rendered into "{file}"'
def load_yaml(filename, path=""):
with open(os.path.join(path,filename)) as file:
data = yaml.load(file, Loader=yaml.FullLoader)
return data
if __name__ == '__main__': if __name__ == '__main__':
arguments = docopt(__doc__) arguments = docopt(__doc__)
@ -127,13 +136,15 @@ if __name__ == '__main__':
line_comment_prefix = '%#', line_comment_prefix = '%#',
trim_blocks = True, trim_blocks = True,
autoescape = False, autoescape = False,
loader = jinja2.FileSystemLoader(os.path.abspath('.')), loader = jinja2.FileSystemLoader(os.path.abspath('/')),
) )
latex_jinja_env.filters['datetime'] = datetime_filter latex_jinja_env.filters['datetime'] = datetime_filter
with open(arguments['--data']) as file: if arguments['--data']:
data = yaml.load(file, Loader=yaml.FullLoader)["data"] data = load_yaml(arguments["--data"])
else:
data = {}
cmd = functools.partial(get_latex_toolchain, arguments) cmd = functools.partial(get_latex_toolchain, arguments)
compile_all(data, arguments['<templates>'], arguments['--pdf'], cmd, arguments['--copy'], arguments['--clean'] and arguments['--files']) compile_all(data, arguments['<templates>'], arguments['--pdf'], cmd, arguments['--copy'], arguments['--clean'] and arguments['--files'])

Loading…
Cancel
Save