PP - Generic preprocessor (with pandoc in mind)
PP is a
text preprocessor designed for Pandoc (and more generally Markdown and
reStructuredText).
The PP
package used to contain three preprocessors for
Pandoc.
I started using Markdown and Pandoc with
GPP. Then I wrote
DPP
to embed diagrams in Markdown documents. And finally
PP
which merges the functionalities of
GPP and
DPP.
GPP and
DPP
are no longer included in
PP as
pp can now be used standalone. dpp and gpp can be found in the
legacy
DPP
repository.
pp now implements:
Warning: PP is not supported anymore
Their is no plan to support PP from now on. PP is meant to be replaced
by a combination of:
- ypp: Lua-scriptable PreProcessor
- Panda: Pandoc add-ons (Lua filters for
Pandoc)
ypp and Panda are written in Lua and are way easier to deploy.
Open source
PP is
an Open source software. Anybody can contribute on
GitHub to:
- suggest or add new features
- report or fix bugs
- improve the documentation
- add some nicer examples
- find new usages
- …
Installation
Compilation:
- Download and extract pp.tgz.
- Run
make.
PP is
written in Haskell and is built with
Stack. On MacOS,
running make requires the GNU version of tar which can be installed
with brew install gnu-tar.
Docker:
A Linux docker made by Joshua Dotson is
available here:
Installation:
- Run
make install to copy pp in ~/.local/bin.
- or copy
pp (pp.exe on Windows) wherever you want.
pp requires (optionally) Graphviz,
blockdiag,
Asymptote,
R and Java
(PlantUML and
ditaa are embedded in pp).
Precompiled binaries:
The recommended way to get PP binaries is to compile them from the
sources. Anyway if you have no Haskell compiler, you can try some
precompiled binaries.
Usage
pp is a simple preprocessor written in Haskell. It’s mainly designed
for Pandoc but may be used as a generic preprocessor. It is not intended
to be as powerful as GPP, for instance, but is a simple implementation
for my own needs, as well as an opportunity to play with Haskell.
pp takes strings as input and incrementally builds an environment
which is a lookup table containing variables and various other
information. Built-in macros are Haskell functions that takes arguments
(strings) and the current environment and build a new environment in the
IO monad. User defined macros are simple definitions, arguments are
numbered 1 to N.
pp emits the preprocessed document on the standard output. Inputs are
listed on the command line and concatenated, the standard input is used
when no input is specified.
Command line
pp executes arguments in the same order as the command line. It starts
with an initial environment containing:
- the environment variables of the current process
- a
lang variable containing the current langage (currently only
French (fr), Italian (it), Spanish (es) and English (en) are
supported)
- a
format variable containing the current output format (html,
pdf, odt, epub or mobi)
- a
dialect variable containing the current dialect (md or rst)
The dialect is used to format links and images in the output
documents. Currently only Markdown and reStructuredText are supported.
If no input file is specified, pp preprocesses the standard input.
The command line arguments are intentionally very basic. The user can
define and undefine variables and list input files.
-v
displays the current version and exits.
-h
displays some help and exits.
-help
displays a longer help and exits.
-userhelp
displays a longer help for user macros only and exits.
-DSYMBOL[=VALUE] or -D SYMBOL[=VALUE]
adds the symbol SYMBOL to the current environment and associates it to
the optional value VALUE. If no value is provided, the symbol is
simply defined with an empty value.
-USYMBOL or -U SYMBOL
removes the symbol SYMBOL from the current environment.
-img=PREFIX or -img PREFIX
changes the prefix of the images output path.
-import=FILE or -import FILE
preprocessed FILE but discards its output. It only keeps macro
definitions and other side effects.
-M TARGET or -M=TARGET
tracks dependencies and outputs a make rule listing the dependencies.
The target name is necessary since it can not be infered by pp. This
option only lists files that are imported, included and used with
mdate and csvmacros.
-plantuml=FILE or -plantuml FILE
use FILE instead of the embedded plantuml.jar file.
-ditaa=FILE or -ditaa FILE
use FILE instead of the embedded ditaa.jar file.
-<macro>[=<arg>]
calls a builtin macro with an optional argument (see pp -help for the
full macro list). Some macros may prevent pp from reading stdin when no
file is given on the command line (langs, formats, dialects, os,
arch, macros, usermacros).
Other arguments are filenames.
Files are read and preprocessed using the current state of the
environment. The special filename “-” can be used to preprocess the
standard input.
Macros
Built-in macros are hard coded in pp and can not be redefined. User
defined macros are simple text substitutions that may have any number of
parameters (named !1 to !n). User macros can be (re)defined on the
command line or in the documents.
Macro names are:
- case sensitive (i.e.:
!my_macro and !My_Macro are different
macros)
- made of letters, digits and underscores (
a-zA-Z0-9_)
User macros starting with _ are not listed in macros lists and help
texts.
To get the value of a variable you just have to write its name after a
‘!’. Macros can be given arguments. Each argument is enclosed in
parenthesis, curly braces or square brackets. For instance, the macro
foo with two arguments can be called as !foo(x)(y), !foo{x}{y} or
even !foo[x][y]. Mixing brackets, braces and parenthesis within a
single macro is not allowed: all parameters must be enclosed within the
same type of delimiters. This helps ending a list of arguments in some
edge cases:
!macro(x)(y)
[link]: foo bar
Here, [link] is not parsed as a third parameter of !macro
Arguments are stripped. Removing leading and trailing spaces helps
preserving line structure in the document.
The last arguments can be enclosed between lines of tildas or backquotes
(of the same length) instead of parenthesis, brackets or braces and.
This is useful for literate programming, diagrams or scripts (see
examples). Code block arguments are not stripped: spaces
and blank lines are preserved.
Arguments can be on separate lines but must not be separated by blank
lines.
You can choose the syntax that works better with your favorite editor
and syntax colorization.
For most of the macros, arguments are preprocessed before executing the
macro. Macros results are not preprocessed (unless used as a parameter
of an outer macro). The include macro is an exception: its output is
also preprocessed. The rawinclude macro can include a file without
preprocessing it.
define, def
!def[ine](SYMBOL)[[(DOC)](VALUE)] adds the symbol SYMBOL to the
current environment and associate it with the optional value VALUE.
Arguments are denoted by !1 … !n in VALUE. If DOC is given it is
used to document the macro (see the -help option).
undefine, undef
!undef[ine](SYMBOL) removes the symbol SYMBOL from the current
environment.
defined
!defined(SYMBOL) returns 1 if SYMBOL is defined, 0 otherwise.
rawdef
!rawdef(X) returns the raw (unevaluated) definition of X.
ifdef
!ifdef(SYMBOL)(TEXT_IF_DEFINED)[(TEXT_IF_NOT_DEFINED)] returns
TEXT_IF_DEFINED if SYMBOL is defined or TEXT_IF_NOT_DEFINED if it
is not defined.
ifndef
!ifndef(SYMBOL)(TEXT_IF_NOT_DEFINED)[(TEXT_IF_DEFINED)] returns
TEXT_IF_NOT_DEFINED if SYMBOL is not defined or TEXT_IF_DEFINED if
it is defined.
ifeq
!ifeq(X)(Y)(TEXT_IF_EQUAL)[(TEXT_IF_DIFFERENT)] returns
TEXT_IF_EQUAL if X and Y