diff --git a/bin/pretex b/bin/pretex index 07924d6..dab3151 100755 --- a/bin/pretex +++ b/bin/pretex @@ -7,7 +7,7 @@ With slightly different syntax: # ... ## => %$ ... %# for Line Statements Usage: - pretex [] --data= [options] + pretex [] [options] Arguments: 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] --clean Clean aux files. -f --files= 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 @@ -89,7 +89,10 @@ def compile_all(data, templates, pdf, cmd, copy, clean): continue path, filename = os.path.split(compiled_file) 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 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): - template = latex_jinja_env.get_template(tex_file) - rendered = template.render(**data) + template = latex_jinja_env.get_template(os.path.abspath(tex_file)) + + path, filename = os.path.split(tex_file) + rendered = template.render(**data, load_yaml=functools.partial(load_yaml, path=path)) if file is None: return rendered f = open(file, 'w') @@ -112,6 +117,10 @@ def compile_file(tex_file, data, file=None): f.close() 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__': arguments = docopt(__doc__) @@ -127,13 +136,15 @@ if __name__ == '__main__': line_comment_prefix = '%#', trim_blocks = True, autoescape = False, - loader = jinja2.FileSystemLoader(os.path.abspath('.')), + loader = jinja2.FileSystemLoader(os.path.abspath('/')), ) latex_jinja_env.filters['datetime'] = datetime_filter - with open(arguments['--data']) as file: - data = yaml.load(file, Loader=yaml.FullLoader)["data"] + if arguments['--data']: + data = load_yaml(arguments["--data"]) + else: + data = {} cmd = functools.partial(get_latex_toolchain, arguments) compile_all(data, arguments[''], arguments['--pdf'], cmd, arguments['--copy'], arguments['--clean'] and arguments['--files'])