Skip to content

Instantly share code, notes, and snippets.

@tene
Created June 4, 2010 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tene/426104 to your computer and use it in GitHub Desktop.
Save tene/426104 to your computer and use it in GitHub Desktop.
Initial proof-of-concept. Next task is rendering to a string, fh, whatever.
I'm doing the evil "add inverted delimiters surrounding" because I got infinite loops
when adding ^ and $ as alternatives.
There are quite a few obvious improvements, but it'll serve as a reasonable starting point.
class Ratel {
has $.source;
has $.compiled;
has @.hunks;
method load(Str $filename) {
$.compile(slurp($filename));
}
method compile(Str $text) {
my $index = 0;
$!source = $text;
my $source = "%]$text[%";
@!hunks = $source.comb(/'%]' (.*?) '[%'/);
$!compiled = $source.subst(/(['%]' | ^ ] .*? [ $ | '[%' ])/, {";\$.emit-hunk({$index++});"}, :g);
}
method emit-hunk(Int $i) {
print @.hunks[$i][0];
}
method do(*%attrs) {
eval $.compiled;
}
}
use Ratel;
my $template = q|<html>
<title>[% print %attrs<title> %]</title>
<ul>
[% for 1..10 { %]
<li>item [% print $_ %]</li>
[% } %]
</ul>
</html>|;
my Ratel $r .= new();
$r.compile($template);
$r.do(:title('OMG HAI GUYS!!'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment