CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...
http://cdelord.fr/pp
GNU General Public License v3.0
252 stars 21 forks source link

Interpolated Variable Processor #95

Open ghost opened 4 years ago

ghost commented 4 years ago

A while ago I wrote yamlp, a YAML preprocessor that performs string variable interpolation. The app was written because Pandoc does not have a way to perform variable interpolation itself. However, despite having a standalone YAML preprocessor, it makes the tool chain all kinds of awkward (piping Pandoc into itself). I've written up a section on how to use yamlp as part of my series on Typesetting Markdown.

From a higher vantage point, YAML is merely one type of structured document format that can be used for interpolated variables. In theory, any hierarchical data could be used, as shown in the following architecture diagram for Markdown editors:

Text Editor Architecture

It would be spectacular if there was a way to have PP perform the preprocessing of Markdown that includes interpolating and injecting variables from structured data formats. Consider:

pp --var-source=https://api.host.com/v1/records \
  --var-type=yaml --var-delim-start="$" --var-delim-end="$" --var-delim-sep="." \
  filename-in.md > filename-out.md

pandoc -f markdown -t context filename-out.md

Given a YAML source document such as:

a: A 
b: $a$ B 
c: $b$ C $b$
d: $a$ $b$ $c$ D $a$
e:
  f: 
    g: G
h: H and $e.f.g$

If PP's input Markdown document was:

Hello $h$ and $d$.

After running PP, the Markdown document for Pandoc would resemble:

Hello H and G and A A B A B C A B D A.

Various variable name delimiters and internal separator tokens, such as {{ and }} and ., should be configurable as well. This would allow for many variable syntaxes, including R Markdown.

CDSoft commented 6 months ago

Sorry for the late reply... Please keep in mind that pp is not supported anymore, it's hard to deploy. For new projects I suggest ypp which is is based on a Lua interpreter and way easier to compile and install and binaries are easier to produce (thanks to zig) and deploy (see hey).

This can be done with ypp and some Lua YAML parser. Or even with ypp and Lua tables.