Created
June 4, 2010 23:58
-
-
Save tene/426104 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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