Skip to content

Commit

Permalink
Merge branch 'release/0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Stepner committed Dec 26, 2017
2 parents d4bf3c9 + 4219980 commit f8c262f
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 24 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
README.rst
README.html
debug.txt
filters/metavars.py
filters/caps.py

### Python (https://www.gitignore.io/api/python) ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.pyc

# Distribution / packaging
dist/
*.egg-info/

# Unit test / coverage reports
.cache
34 changes: 27 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# os: linux and sudo: false is assumed, which means it is using container-based Ubuntu 12.04
# os: linux and sudo: false is assumed, which means it is using container-based Ubuntu
language: python
cache: pip
# build matrix: different python and pandoc versions
Expand All @@ -10,6 +10,7 @@ python:
- "3.6-dev" # 3.6 development branch
- "3.7-dev" # 3.7 development branch
- "nightly" # Travis CI supports a special version name nightly, which points to a recent development version of CPython build
# pypy (version info from [Changelogs — PyPy documentation](http://doc.pypy.org/en/latest/index-of-whatsnew.html))
- "pypy"
- "pypy3"
# https://groups.google.com/forum/?fromgroups#!topic/pandoc-discuss/uGASAhRydfI
Expand Down Expand Up @@ -39,13 +40,32 @@ install:
# pandoc
- wget $downloadUrl &&
sudo dpkg -i $file
- pip install -r requirements.txt
- pip install -e .
before_script:
# pasteurize for py2 only
- |
if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" || "$TRAVIS_PYTHON_VERSION" == "pypy" ]]; then
pasteurize -wn ./src
fi
# pasteurize for py2 only (disabled because Python code is currently universal as-written)
# - |
# if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" || "$TRAVIS_PYTHON_VERSION" == "pypy" ]]; then
# pasteurize -wn ./pandoc_mustache/pandoc_mustache.py
# fi
# commands to run tests
script:
- pytest
before_deploy:
# create README.rst for PyPI README
- pandoc -f markdown+autolink_bare_uris-fancy_lists-implicit_header_references -M date="`date "+%B %e, %Y"`" --toc -s -o README.rst README.md
# debugging rst output
## install dependencies for rst to html
- pip install -e .[pypi]
## test if the rst output is valid
- rst2html.py README.rst > README.html
deploy:
provider: pypi
user: michaelstepner
password:
secure: HI/2sdV0lcuZ9llZr5TUfxVASz8hYjYdehvxRKKKrVBeBIfaIOus6Cysh5hRorqy/wssXy4tA3Q+SzqElhbv1NECjfzWyGqwSiZWwvz5wiAFQlHVVvG98kcCJDXqI3ez8BHJgCz8WSPu5jeBHGhwweJk3DIGw4vhZRcsN+tzWSlPkyZ4TizGoSEF2P4P7k+ostryPID75lQ71LgAyE/vzbdkqmJvZ/UY9OHZU17n1xYTxpKV+bKAQoHh+TWulb7fiGP0QiZdi96meDU9huJIsjBDSJj71WGAir3lh/96itM+kHNgPn5A7Q9LwH207tVtMQOD3cxWvaDnpFmXJH8e0xWsyQv3zS1sH10X+3PoM6xAOh42eCOKYjrloF/6C52TOlieBVzT1l3siCk/fg72x/aqffwkluz9VMUDQC05abbyewTBFPLOdn40vhy6nw8tuXlR5ahBj8YUVEdU5dMMLR3tDtFTMBAnAALi2vTtSvZL2uYooFjUQ0e7csiSl7dVJvnkHNNTGw08q0u1MZK1D1tpgZk/bnFzrBHwEQVt18/GLIbompam9OWFTnGsAfDngBxiNW51HkztTuieVPyCKyWJTvg8xTynqqNj2Juva/DvkzNPH6LBj5WCM61wbR9LO6EMUvK5ihUu2YTw3enwGHD77d08G5eyWkPpYFWcBMA=
distributions: sdist bdist_wheel
skip_cleanup: true
on:
tags: true
python: '3.6'
condition: "$pandocVersion = latest"
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Contributing to pandoc-mustache

[![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.](http://www.repostatus.org/badges/latest/inactive.svg)](http://www.repostatus.org/#inactive)

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.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# pandoc-mustache: Variable Substitution in Pandoc

[![Build Status](https://travis-ci.org/michaelstepner/pandoc-mustache.svg?branch=master)](https://travis-ci.org/michaelstepner/pandoc-mustache)

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](https://mustache.github.io/). Only variable replacement is supported: other [tag types](https://mustache.github.io/mustache.5.html#TAG-TYPES) are not currently supported.

## Example

This document, `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_men}} years longer than the poorest women.
Combined with this dictionary, `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:

```yaml
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](https://pandoc.org/MANUAL.html#metadata-blocks), 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:
```yaml
---
title: My Report
author: Jane Smith
mustache: ./vars.yaml
---
The temperature in {{place}} was {{temperature}} degrees.
```

Or, with more than one file:

```yaml
---
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:

```yaml
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.](http://www.repostatus.org/badges/latest/inactive.svg)](http://www.repostatus.org/#inactive)

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](https://creativecommons.org/publicdomain/zero/1.0/) to permit the widest possible reuse.

## Acknowledgements

This pandoc filter was created using Sergio Correia's [panflute](https://github.com/sergiocorreia/panflute) package. The [panflute](https://github.com/sergiocorreia/panflute) repository also served as an inspiration for the organization of this repository.

### Related Filters

Scott Koga-Browes' [pandoc-abbreviations](https://github.com/scokobro/pandoc-abbreviations) filter also performs variable replacement in pandoc documents, using a different syntax.
1 change: 1 addition & 0 deletions pandoc_mustache/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .version import __version__
2 changes: 0 additions & 2 deletions src/pandoc-mustache.py → pandoc_mustache/pandoc_mustache.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env python
"""
Pandoc filter to apply mustache templates on regular text.
"""
from past.builtins import basestring

from panflute import *
import pystache, yaml

Expand Down
1 change: 1 addition & 0 deletions pandoc_mustache/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.0.1'
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[metadata]
description-file = README.md
[bdist_wheel]
universal=1
134 changes: 134 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"""Pandoc filter for variable substitution using Mustache syntax
See:
https://github.com/michaelstepner/pandoc-mustache/
"""

from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
try:
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
long_description = f.read()
except (IOError):
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

# Import version number
version = {}
with open("pandoc_mustache/version.py") as fp:
exec(fp.read(), version)
version = version['__version__']

setup(
name='pandoc-mustache',

# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version=version,

description='Pandoc filter for variable substitution using Mustache syntax',
long_description=long_description,

# The project's main homepage.
url='https://github.com/michaelstepner/pandoc-mustache/',

# Author details
author="Michael Stepner",
author_email='stepner@mit.edu',

# Choose your license
license='CC0-1.0',

# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 4 - Beta',

# Indicate who your project is intended for
'Environment :: Console',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'Topic :: Text Processing :: Filters',
'Operating System :: OS Independent',

# Pick your license as you wish (should match "license" above)
'License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication',

# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy'
],

# What does your project relate to?
keywords='pandoc pandocfilters panflute markdown latex mustache',

# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
# packages=find_packages(exclude=['contrib', 'docs', 'tests', 'examples']),

# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
py_modules=["pandoc_mustache"],

# List run-time dependencies here. These will be installed by pip when
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=[
'panflute',
'pystache',
'pyyaml',
'future'
],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
# for example:
# $ pip install -e .[dev,test]
extras_require={
# 'dev': ['check-manifest'],
# 'test': ['pandocfilters', 'configparser', 'pytest-cov', 'future'],
'pypi': ['docutils']
},

# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
#package_data={
# 'sample': ['package_data.dat'],
#},

# Although 'package_data' is the preferred approach, in some case you may
# need to place data files outside of your packages. See:
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa
# In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
#data_files=[('my_data', ['data/data_file'])],

# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={
'console_scripts': [
'pandoc-mustache=pandoc_mustache.pandoc_mustache:main',
],
},
)
6 changes: 3 additions & 3 deletions tests/test_html_escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_escape_singlequote(tmpdir):
template['path'].write(template['content'])

# Run pandoc
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "./src/pandoc-mustache.py", "--to=plain"], universal_newlines=True)
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache", "--to=plain"], universal_newlines=True)

# Test output
assert output == "Hello world ' universe\n"
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_escape_gt(tmpdir):
template['path'].write(template['content'])

# Run pandoc
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "./src/pandoc-mustache.py", "--to=plain"], universal_newlines=True)
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache", "--to=plain"], universal_newlines=True)

# Test output
assert output == "Hello world > universe\n"
Expand Down Expand Up @@ -91,7 +91,7 @@ def test_escape_ampersand(tmpdir):
template['path'].write(template['content'])

# Run pandoc
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "./src/pandoc-mustache.py", "--to=plain"], universal_newlines=True)
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache", "--to=plain"], universal_newlines=True)

# Test output
assert output == "Hello world & universe\n"
4 changes: 2 additions & 2 deletions tests/test_metadata_list_emptyval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_blank_mustache_list(tmpdir):

# Run pandoc, assert error
try:
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "./src/pandoc-mustache.py"], universal_newlines=True, stderr=subprocess.STDOUT)
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache"], universal_newlines=True, stderr=subprocess.STDOUT)
assert 0 # expecting an exception when calling pandoc
except subprocess.CalledProcessError as e:
assert e.returncode == 83
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_2el_mustache_list_wblank(tmpdir):

# Run pandoc, assert error
try:
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "./src/pandoc-mustache.py"], universal_newlines=True, stderr=subprocess.STDOUT)
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache"], universal_newlines=True, stderr=subprocess.STDOUT)
assert 0 # expecting an exception when calling pandoc
except subprocess.CalledProcessError as e:
assert e.returncode == 83
Expand Down

0 comments on commit f8c262f

Please sign in to comment.