Pandoc style dollar templates for python
Project description
Pandoc style dollar templates for python
A slim reimplementation of pandoc's templating engine in python. This allows you to use pandoc-style templates (cf. https://pandoc.org/MANUAL.html#template-syntax) in any python project, even if you do not want to use pandoc itself.
While this project aims to follow the template syntax described in
the pandoc documentation, it is a completely separate project and may
therefore behave differently than pandoc in certain situations. Especially
edge cases are not particularly well tested (yet). Also note that, as
of now, not all features found in pandoc's templating engine have been
implemented (see below). If you notice differences between pandoc and
dollar_templates concerning features that have been implemented,
feel free to submit a bug report or even a pull request.
Installing
This repository uses python setuptools and can be installed with pip.
To install dollar_templates into the current user's home directory, for
instance, you can simply invoke pip install --user .. Also note that
a stable version of this python module is available at pypi. To install
the stable version, simply run pip install --user dollar-templates.
Usage
The easiest way to expand a template with some data is to use the
apply_template function:
from dollar_templates import apply_template
template = "$for(var)$- $var$\n$endfor$"
metadata = {"var": [1,2,3]}
print(apply_template(template, metadata))
To parse the template once and apply it multiple times to different
data, use the parse_template function that returns an object of
type dollar_templates.Template.
from dollar_templates import parse_template
template = "$for(var)$- $var$\n$endfor$"
metadata_a = {"var": [1,2,3]}
metadata_b = {"var": [11,12,13]}
t = parse_template(template)
print(t.apply(metadata_a))
print(t.apply(metadata_b))
Unit tests
Unit tests are implemented in test_all.py and can be executed with
pytest. Simply install pytest and then execute
the pytest command in this directory without arguments.
Implementation state
Pandoc lists the following features of its templating engine (cf. https://pandoc.org/MANUAL.html#template-syntax):
- Comments
- Delimiters
- Interpolated variables
- Conditionals
- For loops
- Partials
- Nesting
- Breakable spaces
- Pipes
The list above shows which ones have been implemented in dollar_templates. The missing ones might or might not follow in the future.
License
All files in this repository are made available under the terms of the
GNU General Purpose License, version 3 or later. A copy of that license
is included in the repository as license.txt.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dollar_templates-1.0.1.tar.gz.
File metadata
- Download URL: dollar_templates-1.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca531dc63cf7d79b48d48a3c70b1533e3a30a82f8b6b838774d33ec3c04e2986
|
|
| MD5 |
2d19bcddd6b18591a4b304c5ec175a7b
|
|
| BLAKE2b-256 |
4e3ce12196d777717d53822b9b35344bf6d249e4f6704275267da48d66bab389
|
File details
Details for the file dollar_templates-1.0.1-py3-none-any.whl.
File metadata
- Download URL: dollar_templates-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc2e8b7c263b92586783a198380977e9fdf02c08c9e9ac91c08a5d042a4a32b1
|
|
| MD5 |
c7ccd0653738db8775ee993e58e5cce5
|
|
| BLAKE2b-256 |
115f2305adfdd7a32adcdaa76205ddf3848ae6c269765b628b04d3635a176a43
|