Skip to content

Add LaTeX support #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Options:
Use only this section from the configuration
--strict Disallow undefined variables to be used within the
template
--latex Use markers that are compatible with LaTeX
```

## Optional YAML support
Expand All @@ -43,6 +44,30 @@ If `xmltodict` is present, you can use XML as an input data source.

`$ pip install jinja2-cli[xml]`

## LaTeX support
The default markers used by Jinja2 are incompatible with LaTeX, as explained
in blog posts by [Brad Erickson] and [Arthur Miller]. The option `--latex`
changes the default markers to the following ones by passing , which are compatible with
LaTeX and do not require any special escaping.

| Default markers | LaTeX mode | Jinja2 env settings |
| :-------------------- | :---------------- | :---------------------- |
| `{%` ... `%}` | `\BLOCK{` ... `}` | `block_*_string` |
| `{{` ... `}}` | `\VAR{`... `}` | `variable_*_string` |
| `{#` ... `#}` | `\#{` ... `}` | `comment_*_string` |
| *disabled by default* | `%%` | `line_statement_prefix` |
| *disabled by default* | `%#` | `line_comment_prefix` |

In addition, the option `trim_blocks` is set to true, and `autoescape` set to
false.

Example usage:
```
jinja2 --latex samples/sample.tex samples/sample.json
```

[Brad Erickson]: http://eosrei.net/articles/2015/11/latex-templates-python-and-jinja2-generate-pdfs
[Arthur Miller]: https://miller-blog.com/latex-with-jinja2/
## TODO
* Variable inheritance and overrides
* Tests!
31 changes: 29 additions & 2 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ def _parse_env(data):
}


def render(template_path, data, extensions, strict=False):
def render(template_path, data, extensions, env_options, strict=False):
from jinja2 import Environment, FileSystemLoader, StrictUndefined

env = Environment(
loader=FileSystemLoader(os.path.dirname(template_path)),
extensions=extensions,
**env_options,
keep_trailing_newline=True,
)
if strict:
Expand Down Expand Up @@ -300,6 +301,23 @@ def cli(opts, args):
sys.stderr.write("ERROR: unknown section. Exiting.")
return 1

# Set up environment options suitable to use LaTeX templates
if opts.latex:
env_options = dict(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False)

else:
env_options = dict()

data.update(parse_kv_string(opts.D or []))

if opts.outfile is None:
Expand All @@ -312,7 +330,7 @@ def cli(opts, args):

out = codecs.getwriter("utf8")(out)

out.write(render(template_path, data, extensions, opts.strict))
out.write(render(template_path, data, extensions, env_options, opts.strict))
out.flush()
return 0

Expand Down Expand Up @@ -398,6 +416,15 @@ def main():
dest="strict",
action="store_true",
)
parser.add_option(
# These marker types might be compatible with TeX, too, so --tex
# might be more accurate (but also harder to find)
"--latex",
"--tex",
help="Use variable/block/... markers that are compatible with LaTeX",
dest="latex",
action="store_true",
)
parser.add_option(
"-o",
"--outfile",
Expand Down
11 changes: 11 additions & 0 deletions samples/sample.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
\documentclass{article}

\begin{document}

\begin{itemize}
\BLOCK{ for i in items }
\item \VAR{i}
\BLOCK{ endfor }
\end{itemize}

\end{document}