aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: d31bc67187cc34df6b3643302477de6ea349b60e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# tpl

`tpl` is an extremely simple general-purpose templating tool written in
POSIX-2024-compliant AWK.

To get started, define a list of variables in a file:

	# This is a comment.
	food = pizza
	beverage = coffee

	# Previously defined variables can be used in variable definitions.
	# Whitespace is optional inside braces.
	dinner = {{ food }} and {{beverage}}

	# The = character can be surrounded by any number of whitespace
	# characters.
	one	= 1
	two	= 2
	three=3

Then reference those variables in your template file by enclosing them in
double braces:

	My favorite food is {{ food }}, and my favorite beverage is
	{{ beverage }}. I'm going to have {{ dinner }} for dinner tonight.

    The first positive integers are {{one}}, {{two}}, and {{three}}. {{one}} is
    the smallest of the three.

Then pass your variable file and template file, in that order, as arguments to
the `template` script. To see the examples above in action, run the following
command:

    ./tpl example/vars example/text.tpl

The result will be written to `stdout`.

## Specification

This section describes the behavior of the program in excruciating detail and
will be of little interest to most users.

The program may be invoked as follows:

	tpl [variable file] [template file...]

The variable file will be parsed, the variables defined within will be
substituted for all apperances of corresponding template variables that appear
in the given template file(s), and the processed text will be printed to
stdout. If multiple template files are specified, their contents will be
concatenated in the output.

The following subsections specify the behavior of the program in detail.
Illegal inputs proscribed below will not necessarily produce an error message
but may result in undefined behavior.

### Template Variable Format

A template variable consists of a variable name enclosed in double braces,
with zero or more whitespace characters separating the variable name and
the braces. The following examples are all legal:

    {{ my_var }}
    {{my_var}}
    {{  my_var  }}
    {{      my_var}}

### Variable File

Variables are defined as key-value pairs separated by the character `=`.
The `=` character may be surrounded by an arbitrary number of whitespace
characters on either side. The following examples are all legal:

    my_var = foo
    my_var=foo
    my_var      = foo

Lines beginning with the character `#` are ignored. Note that text following
`#` is *not* otherwise ignored, so trailing inline comments are not supported.

Variable names may contain any character supported by the version of awk used
to execute this script except for the `=` character or any of the regular
expression metacharacters:

    \ ^ $ . [ ] | ( ) * + ? { }

Variable values may reference variables defined earlier in the file, in which
case they must use the brace-delimited variable format described above:

	foo = {{ bar }} baz

When a value references a previously defined variable, the most recent value of
that variable is used. If a variable name appears more than once, the previous
value is overwritten on each assignment; previous assignments are not affected.

For example, suppose a variable file contained the following lines:

    food = pizza
    favorite_food = {{ food }}
    food = apple

After the value was parsed, the value of `favorite_food` would be `pizza` and
the value of `food` would be `apple`.

### Template File

A template consists of arbitrary text containing zero or more template
variables in the brace-delimited format described above. A variable may appear
any number of times within a file or a line. A template variable that does not
reference a valid variable name defined in the variable file will be ignored.