Literate Programming Using OmniMark
Contents
2. Template Processing
Template processing is central to the weaving process, where
we build a formatted document from the literate program. The
template language devised for this project is quite simple: a
template consists of boilerplate text, and template
parameters, which are strings bracketed by [: and :]. Templates are emitted via the emit-template function: <1 template processing> =
define string source function
emit-template value string source template
as
using group "fill-in template parameters"
submit template
In our case, all the templates are stored in the global string
shelf templates: <2 global shelves> =
global string templates variable elsewhere
As a result, the argument to emit-template could be a
string instead of a string source. Template
parameters are stored in the global string shelf template-parameters: <2 global shelves> +=
global string template-parameters variable
The substitution of template parameter values into the
template is handled by find rules: <1 template processing> +=
group "fill-in template parameters"
find "[:" any ++ => parameter-name ":]" (blank* "%n")?
output template-parameters{parameter-name}
when template-parameters has key parameter-name
A second form of template parameter, this time delimited by
[? and ?] is used when a referent should be emitted
rather than a value: <1 template processing> +=
group "fill-in template parameters"
find "[?" ("key:" | "name:") => p any ++ => parameter-name "?]" (blank* "%n")?
output referent (p || template-parameters{parameter-name})
when template-parameters has key parameter-name
Although the shelf of templates is shown here, the actual
values are discussed later, when they are needed. <2 global shelves> +=
OmniMark's save-clear operation allows for rudimentary
scoping of template parameters: <3 clear template parameters> =
save-clear template-parameters
This is sufficient for our needs.
Previous section: Introduction
Next section: Weaving
|