|
Blatte - text macro/markup/template language |
Blatte - text macro/markup/template language
use Blatte; use Blatte::Builtins;
my $perl = &Blatte::Parse('...some Blatte program ...');
if (defined($perl)) {
my $result = eval $perl;
if (defined($result)) {
&Blatte::traverse($result, \&callback);
} elsif ($@) {
...handle execution error...
}
} else {
...handle parsing error...
}
Blatte is a very powerful text markup and transformation language with a very simple syntax. A Blatte document can be translated into a Perl program that, when executed, produces a transformed version of the input document.
This module itself contains some utility functions for handling Blatte documents, described below in the FUNCTIONS section. However, writers of Blatte-based software will generally be more interested in other associated modules. See in particular the Blatte::Compiler manpage (for processing files full of Blatte code) and the Blatte::Builtins manpage (for a description of the Blatte language's intrinsic functions).
Most casual end users will probably be interested in Blatte's ability to serve as a higher-level language for writing web pages. This requires the additional CPAN package Blatte::HTML.
Most of the remainder of this document describes the syntax and semantics of the Blatte language.
Blatte has three metacharacters: \ { }. These are used to represent lists, variables, syntactic forms, function calls, string literals, comments, and a ``forget-whitespace'' operator explained below.
Everything else in a Blatte document is either whitespace or is divided into ``words,'' also explained below.
To include a literal metacharacter, precede it with backslash: \\, \{, and \}.
All Blatte expressions correspond to equivalent Perl expressions. When a Blatte document is parsed, its Blatte expressions are converted to Perl, which may then be evaluated.
Here is a quick rundown of Blatte expression types.
This corresponds to the Perl scalar variable $var. All values in Blatte are Perl scalars.
This corresponds to the Perl sequence
use vars '$var'; $var = EXPR;
(where EXPR has been transformed to its Perl equivalent).
This corresponds to the Perl expression
$VAR = EXPR
All Blatte values are true except for 0 and the empty string (as in Perl) and the empty Blatte list, {} (which corresponds to the Perl array reference []).
Perl equivalent:
if (TEST) {
THEN;
} else {
ELSE1;
ELSE2;
...
}
Perl equivalent:
EXPR1 && EXPR2 && ...
Perl equivalent:
EXPR1 || EXPR2 || ...
Perl equivalent:
if (TEST1) {
THEN1a;
THEN1b;
...
} elsif (TEST2) {
THEN2a;
THEN2b;
...
} ...
Perl correspondence:
while (TEST) {
EXPR1;
EXPR2;
...
}
Each PARAM is one of the following:
See below for how to invoke Blatte subroutines, and how argument parsing proceeds.
Perl equivalent:
sub {
...(argument parsing)...
EXPR1;
EXPR2;
...
}
{\define \NAME {\lambda {PARAM1 PARAM2 ...} EXPR1 EXPR2 ...}}
Perl equivalence:
{
my($VAR1, $VAR2, ...) = (VAL1, VAL2, ...);
EXPR1;
EXPR2;
...
}
Perl equivalence:
{
my $VAR1 = VAL1;
my $VAR2 = VAL2;
...
EXPR1;
EXPR2;
...
}
Perl equivalence:
{
my($VAR1, $VAR2, ...);
($VAR1, $VAR2, ...) = (VAL1, VAL2, ...);
EXPR1;
EXPR2;
...
}
If the value of EXPR1 is a Blatte subroutine, it is a function call, and the values of EXPR2 through EXPRn are passed as arguments. In this case it corresponds to the following Perl:
&{EXPR1}({ ...named parameter hash... }, ...other arguments...)
See below for an explanation of Blatte subroutine argument parsing.
If EXPR1 isn't a Blatte subroutine, then
{EXPR1 EXPR2 ... EXPRn}
is a plain list whose elements are the values of EXPR1 through EXPRn. Blatte lists correspond to Perl array references:
[EXPR1, EXPR2, ..., EXPRn]
In addition to the expression types above, there are two additional pieces of Blatte syntax:
As mentioned above, Blatte subroutines (a.k.a. functions) have three kinds of parameters: positional, named, and rest.
When calling a Blatte function, named parameters can be given values by writing
\NAME=EXPR
where \NAME is the parameter (which was given as \=NAME in the function definition).
All remaining arguments in the function call are assigned to positional parameters in the order in which they were declared. If there aren't enough positional parameters, then all the remaining arguments are collected in a Blatte list and assigned to the rest parameter.
When a Blatte function call is translated to Perl, the named parameter assignments are collected together in an anonymous HASH reference, which is passed as the first argument to the Perl subroutine corresponding to the Blatte function. (All Blatte functions are Perl subroutines that accept this anonymous HASH reference of named parameters as a first argument.) All remaining Blatte arguments are passed in sequence to the Perl subroutine.
Inside the Perl subroutine, arguments are unpacked as follows: named parameters are extracted from the HASH reference and assigned to correspondingly named Perl variables; Perl variables for the positional parameters get the next N arguments from @_; and the remainder of @_ is turned into an ARRAY reference (a Blatte list) and assigned to the Perl variable denoting the rest parameter.
Example:
{\define {\fn \=n1 \=n2 \a \b \&r}
...do stuff...}
becomes:
$fn = sub {
my($_named, $a, $b) = splice(@_, 0, 3);
my $n1 = $_named->{n1};
my $n2 = $_named->{n2};
my $r = \@_;
...do stuff...
};
and the function call
{\fn \n2=17 This is an example.}
becomes:
&$fn({n2 => 17}, 'This', 'is', 'an', 'example.')
which means $n1 will be undef, $n2 will be 17, $a will be 'This', $b will be 'is', and $r will be ['an', 'example'].
(Almost. This example ignores Blatte's whitespace handling, which is explained below.)
Before recognizing a Blatte expression, the Blatte parser skips over a
(possibly empty) sequence of whitespace. This whitespace is
preserved; then, when the parser is finished, the result is wrapped in
a Blatte::Ws object, or a ``whitespace wrapper,'' containing the
preserved whitespace and the parsed expression. In this way, the
whitespace preceding each expression is carried along with the
expression itself. When Blatte expressions are parsed, evaluated, and
rendered normally (see flatten() below), the output preserves the same
whitespace found in the input.
Parse(INPUT)(If you don't use the default parser, it's possible to change Blatte's syntax, to obtain the intermediate parse tree before conversion to Perl, and more. See the Blatte::Parser(3) manpage.)
INPUT is either a string containing Blatte code, or a reference to such a string. If it's a reference, then after a successful parse, the matched expression will be removed from the beginning of the referenced string.
CALLBACK should return truth if it uses its whitespace argument and
falsehood if it doesn't (meaning that traverse() should reuse the
whitespace value in subsequent calls, if necessary). ``Uses'' usually
means that the whitespace is copied to some sort of output.
This function supplies the logic that causes outer whitespace wrappers to take precedence over inner ones.
This function is written in terms of traverse().
unwrapws(OBJ)wsof(OBJ)wsof(wrapws(X, Y))
is always X, however
unwrapws(wrapws(X, Y))
is Y only when Y is not itself a whitespace wrapper.
true(OBJ)quote(STRING)
Blatte is a successor to an earlier language called Latte. The B is for ``better'' -- better because, whereas Latte's runtime facilities were fairly limited, Blatte's are effectively unlimited, since Blatte has the full power of Perl at its disposal. The implementation is also much faster and simpler than that of Latte. Latte users should beware that, despite being substantially similar, the Blatte language has significant differences from Latte.
The design of the Blatte language was strongly influenced by Scheme, and was guided by these principles:
Blatte is dedicated to the memory of Julie Epelboim, 1964-2001. Not that she ever would have used it. This was a woman who preferred to write raw Postscript code rather than use a page layout program. The tragedy of having lost her is dwarfed by the good fortune of having known her.
Bob Glickstein <bobg@zanshin.com>.
Visit the Blatte website, <http://www.blatte.org/>.
Copyright 2001 Bob Glickstein. All rights reserved.
Blatte is distributed under the terms of the GNU General Public License, version 2. See the file LICENSE that accompanies the Blatte distribution.
the Blatte::Compiler(3) manpage, the Blatte::Builtins(3) manpage, the Blatte::Parser(3) manpage, the Blatte::Ws(3) manpage.
|
Blatte - text macro/markup/template language |