diff --git a/bopytex/jinja2_env/texenv.py b/bopytex/jinja2_env/texenv.py new file mode 100644 index 0000000..d1fb124 --- /dev/null +++ b/bopytex/jinja2_env/texenv.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# encoding: utf-8 + +import jinja2 + +__all__ = [ + "texenv", +] + +# Definition of jinja syntax for latex +texenv = jinja2.Environment( + block_start_string=r"\Block{", + block_end_string="}", + variable_start_string=r"\Var{", + variable_end_string="}", + comment_start_string=r"\#{", + comment_end_string="}", + line_statement_prefix="%-", + line_comment_prefix="%#", + loader=jinja2.ChoiceLoader( + [ + jinja2.FileSystemLoader(["./"]), + ] + ), + extensions=["jinja2.ext.do"], +) + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del diff --git a/test/test_texenv.py b/test/test_texenv.py new file mode 100644 index 0000000..dc0d353 --- /dev/null +++ b/test/test_texenv.py @@ -0,0 +1,31 @@ +from bopytex.jinja2_env.texenv import texenv + + +def test_variable_block(): + base_template = r"\Var{a}" + jinja2_template = texenv.from_string(base_template) + output = jinja2_template.render(a=2) + assert output == "2" + + +def test_block_string(): + base_template = r"\Block{set a = 2}\Var{a}" + jinja2_template = texenv.from_string(base_template) + output = jinja2_template.render() + assert output == "2" + + +def test_block_line_statement(): + base_template = r"""%-set a = 2 +\Var{a}""" + jinja2_template = texenv.from_string(base_template) + output = jinja2_template.render() + assert output == "2" + +def test_block_line_statement_with_comment(): + base_template = r"""%-set a = 2 +%# comment +\Var{a}""" + jinja2_template = texenv.from_string(base_template) + output = jinja2_template.render() + assert output == "\n2"