A tiny preprocessor that enables you to embed shell code everywhere!
!!get_h1() { t=$(grep '^#' "$1"); t=${t#'# '}; }
<!DOCTYPE html>
<html lang=en>
<head>
<title>Dylan${1:+ - $1}</title>
<style>$(cat css/style.css)</style>
</head>
<body>
<h1>$1</h1>
<ul>
!!
for f in post/*.md; do
get_h1 "$f"
echo "<li><a href="/${f%.md}">$t</a></li>"
done
!!
</ul>
<footer>
<p>Page updated: $(date '+%a %D %I:%M')</p>
</footer>
</body>
</html>See the example/ subdirectory for more examples or peruse the source code for
my websites which are built around dpp: https://github.com/dylanaraps/wild.gr
https://github.com/dylanaraps/dylan.gr.
Using make (install requires appropriate privileges).
$ make
$ make PREFIX=/usr installDPP works like any “UNIXy” program, it reads input from stdin and outputs to
stdout. Any errors that occur are sent to stderr.
$ dpp < input > outputPassing command-line arguments to DPP makes them accessible via $1, $2, etc
within the input.
$ dpp a b c d < input > outputDPP has no command-line flags like -h (help) or -v (version). The version
number is accessible via DPP_VERSION within DPP.
$ echo '$DPP_VERSION' | dppDPP also comes with a second utility called dpp-compile. This command is
identical to dpp only it doesn’t execute the transformed input. It instead
prints the transformed input to stdout where it can be piped to a shell or
saved to a file for later execution.
Basically, it allows you to “compile” text files into programs for later execution.
# Transform and execute dppfetch.
$ dpp < example/dppfetch
# Transform and save the "compiled" dppfetch to a file for later execution.
$ dpp-compile < example/dppfetch > dppfetch
$ chmod +x dppfetch
$ ./dppfetchDPP can be controlled via environment variables.
-
DPP_BLOCK(default!!)Change the marker DPP uses for single line and multi line evaluations.
-
DPP_INCLUDE(default'')Set to a shell script that DPP will source at the beginning.
-
DPP_CAT(defaultcat)Set to a shell command that DPP will run on ordinary text. The value can contain spaces (
DPP_CAT="md2html --github", for example). -
DPP_SHELL(default/bin/sh)The shell DPP will execute. The value must be an absolute path.
DPP also exposes some information via environment variables.
-
DPP_VERSIONThe version of DPP.
-
DPP_LEVELThe DPP nest level.
Before syntax is explained the reader must understand a few things.
-
All shell code that DPP evaluates exists within the same shell environment.
For example, if a variable or function is defined in one code block it remains defined and accessible for the remainder of the input. The input is basically an inside-out(?) shell script where whole programs can live interspersed inside of arbitrary data.
!! cnt() { n=$((n + 1)); echo "$n"; } !! !!cnt The value of the counter is currently: $n The value of the counter is currently: $(cnt) (this runs in a subshell and so the value of '$n' does not change.) !!n= The value of the counter is currently: ${n:-null}
-
All shell code is evaluated with
set -eandset -uenabled.DPP will error and exit when evaluated code returns a non-zero exit code or when variables are unset. Any error messages will be sent to stderr. Also,
set -o pipefailis enabled (if supported). These options can be modified at compile time inconfig.hor at runtime by simply usingsetin your code.
This should all be pretty self-explanatory to those familiar with the shell.
Starting a line with !! tells DPP to evaluate the entire line.
!!echo "Hello, World!"
!! echo "The space after !! is optional"
Lines starting with a backslash will not be evaluated.
\!! echo "The space after !! is optional"
!!
for l in a b c d e f g; do
echo "$l"
done
!!
Blocks starting with a backslash will not be evaluated.
\!!
for l in a b c d e f g; do
echo "$l"
done
\!!The current date is: $(date +%d-%m-%Y)
Inline code blocks starting with a backslash will not be evaluated.
\$(echo "I will be treated as plain text")The value of a is ${a:-unset}.
!!a=/home/dylan/projects/dpp/dpp
The value of a is $a.
The basename of a is ${a##*/}
The dirname of a is ${a%/*}
The user's shell is $SHELL.
The current version of DPP is $DPP_VERSION.
This is line $DPP_LINE of the input.
Variables starting with a backslash \$a will not be evaluated.The value of a is ${a:-unset}.
!!a=3
The value of a is $a.
$((a * 3))
Arithmetic blocks starting with a backslash \$((a * 2)) will not be
evaluated.Any arguments given to DPP are accessible via $1, $2, etc.
<!DOCTYPE html>
<html lang=en>
<head>
<title>Dylan ${1:+- $1}</title>
<style>$(cat style.css)</style>
</head>
<body>
<h1>$2</h1>
</body>
</html>
Variables starting with a backslash \$1 will not be evaluated.To see how DPP works inspect the output of the dpp-compile command.
I like shell. Also, I have another version of DPP which uses C as the language
and optionally compiles itself using tcc. It's slighly more complex and I
might release it later, we'll see.