Skip to main content

Pandoc filter for variable substitution using Mustache syntax

Project description

Date:
May 11, 2019

pandoc-mustache: Variable Substitution in Pandoc

Development Status PyPI version Python version Build Status

The pandoc-mustache filter allows you to put variables into your pandoc document text, with their values stored in a separate file. When you run pandoc the variables are replaced with their values.

Technical note: This pandoc filter is not a complete implementation of the Mustache template spec. Only variable replacement is supported: other tag types are not currently supported.

Example

This document, in document.md:

---
mustache: ./le_gaps.yaml
---
The richest American men live {{diff_le_richpoor_men}} years longer than the poorest men,
while the richest American women live {{diff_le_richpoor_women}} years longer than the poorest women.

Combined with these variable definitions, in le_gaps.yaml:

diff_le_richpoor_men: "14.6"
diff_le_richpoor_women: "10.1"

Will be converted by pandoc document.md --filter pandoc-mustache to:

The richest American men live 14.6 years longer than the poorest men, while the richest American women live 10.1 years longer than the poorest women.

Installation

Install by opening a terminal and running:

pip install -U pandoc-mustache

Python 2.7, 3.4+, pypy, and pypy3 are supported.

Usage

  1. Within a pandoc document, variables are referenced by enclosing the variable name in double “mustaches”, i.e. curly brackets, like {{this}}.

  2. The variables are defined in one or more separate files, using YAML formatted key-value pairs. For example:

    place: Montreal
    temperature: '7'
  3. The pandoc document containing the mustache variables points to the YAML file (or files) which contain the variable definitions. These files are indicated using the mustache field in a YAML metadata block, typically placed at the top of the pandoc document. Absolute paths and relative paths are supported: relative paths are evaluated relative to the working directory where pandoc is being run.

    An example:

    ---
    title: My Report
    author: Jane Smith
    mustache: ./vars.yaml
    ---
    The temperature in {{place}} was {{temperature}} degrees.

    Or, with more than one file:

    ---
    title: My Report
    author: Jane Smith
    mustache:
    - ./vars.yaml
    - ./additional_vars.yaml
    ---
    The temperature in {{place}} was {{temperature}} degrees.
    The humidity was {{humidity}}%.
  4. Run pandoc and replace all variables in the document with their values by adding --filter pandoc-mustache to the pandoc command.

Tips and Tricks

  • When defining variables in YAML, there is no need to enclose strings in quotes. But you should enclose numbers in quotes if you want them to appear in the document using the exact same formatting. Some examples:

    unquoted_string: Montreal  # becomes: Montreal
    quoted_string: 'Montreal'  # becomes: Montreal
    trailingzero_num: 7.40  # becomes: 7.4
    trailingzero_string: '7.40'  # becomes: 7.40
  • If you’re writing a document that reports computed numerical results, you can program your code (in R, Python, Stata, etc.) to write those numbers to a YAML file automatically each time they are generated. By referencing your numerical results using variables instead of hard-coding them into the text, the document can be updated instantly if the results change. And you can be certain that all the numbers in the output document reflect the latest results of your analysis.

Contributing

Project Status: Inactive – The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.

This code is not being actively developed. It was created to fulfill my pandoc writing needs, and the current feature set is adequate for me.

If you have a bug report, you can create an issue or file a pull request. I’ll look into it, time permitting.

If you have a feature request, it is unlikely that I will be able to implement it for you. You can create an issue to generate discussion. If you implement a feature, you can file pull request and I will review it eventually, as time permits. If you’re interested in making major additions to the code, I’d be happy to welcome a new maintainer to the project.

License

All of the files in this repository are released to the public domain under a CC0 license to permit the widest possible reuse.

Acknowledgements

This pandoc filter was created using Sergio Correia’s panflute package. The panflute repository also served as an inspiration for the organization of this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandoc-mustache-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

pandoc_mustache-0.1.0-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pandoc-mustache-0.1.0.tar.gz.

File metadata

  • Download URL: pandoc-mustache-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for pandoc-mustache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c468d1c21676cf1f9824045a078fe5d4e8456aecbf2cca2c223310aa81a7c6b3
MD5 8b7534ae05c2c87eb4b90a22dde6615e
BLAKE2b-256 295b6745ae8e84050e03618b0080d6654ffd8c91cb241e54116ce389eb44f91d

See more details on using hashes here.

File details

Details for the file pandoc_mustache-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pandoc_mustache-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for pandoc_mustache-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ce63e12a424bbb303457b5e9353cd901d8911eba2e01cd1aeb026047ed568b6d
MD5 46933f161953dcba93abbb7072e59d13
BLAKE2b-256 06bae5c68652ac75be3f5e090c5b0a15e7d360fdb2cd09fd39436e2e3142a55f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page