1 ! Copyright (C) 2005 Alex Chapman
2 ! Copyright (C) 2006, 2010 Slava Pestov
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors combinators compiler.units html.templates io
5 io.encodings.utf8 io.files kernel lexer math namespaces parser
6 parser.notes quotations sequences splitting vocabs.parser ;
7 IN: html.templates.fhtml
9 ! We use a custom lexer so that %> ends a token even if not
10 ! followed by whitespace
11 TUPLE: template-lexer < lexer ;
13 : <template-lexer> ( lines -- lexer )
14 template-lexer new-lexer ;
16 M: template-lexer skip-word
19 { [ 2dup nth CHAR: \" = ] [ drop 1 + ] }
20 { [ 2dup swap tail-slice "%>" head? ] [ drop 2 + ] }
23 ] change-lexer-column ;
27 : check-<% ( lexer -- col )
28 [ line-text>> "<%" ] [ column>> ] bi start ;
30 : found-<% ( accum lexer col -- accum )
33 [ column>> ] 2dip subseq suffix!
35 ] 2keep 2 + >>column drop ;
37 : still-looking ( accum lexer -- accum )
39 [ line-text>> ] [ column>> ] bi tail
40 suffix! \ print suffix!
43 : parse-%> ( accum lexer -- accum )
46 [ found-<% ] [ [ still-looking ] keep parse-%> ] if*
51 SYNTAX: %> lexer get parse-%> ;
53 : parse-template-lines ( lines -- quot )
55 V{ } clone lexer get parse-%> f (parse-until) >quotation
58 : parse-template ( string -- quot )
62 "html.templates.fhtml" use-vocab
63 string-lines parse-template-lines
65 ] with-compilation-unit ;
67 : eval-template ( string -- )
68 parse-template call( -- ) ;
74 M: fhtml call-template* ( filename -- )
75 path>> utf8 file-contents eval-template ;
77 INSTANCE: fhtml template