1 ! Copyright (C) 2005 Alex Chapman
2 ! Copyright (C) 2006, 2008 Slava Pestov
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: continuations sequences kernel namespaces debugger
5 combinators math quotations generic strings splitting
7 parser lexer io io.files io.streams.string io.encodings.utf8
10 IN: html.templates.fhtml
12 ! We use a custom lexer so that %> ends a token even if not
13 ! followed by whitespace
14 TUPLE: template-lexer < lexer ;
16 : <template-lexer> ( lines -- lexer )
17 template-lexer new-lexer ;
19 M: template-lexer skip-word
22 { [ 2dup nth CHAR: " = ] [ drop 1+ ] }
23 { [ 2dup swap tail-slice "%>" head? ] [ drop 2 + ] }
26 ] change-lexer-column ;
30 : check-<% ( lexer -- col )
31 "<%" over line-text>> rot column>> start* ;
33 : found-<% ( accum lexer col -- accum )
36 [ column>> ] 2dip subseq parsed
38 ] 2keep 2 + >>column drop ;
40 : still-looking ( accum lexer -- accum )
42 [ line-text>> ] [ column>> ] bi tail
43 parsed \ print-html parsed
46 : parse-%> ( accum lexer -- accum )
49 [ found-<% ] [ [ still-looking ] keep parse-%> ] if*
54 : %> lexer get parse-%> ; parsing
56 : parse-template-lines ( lines -- quot )
58 V{ } clone lexer get parse-%> f (parse-until) >quotation
61 : parse-template ( string -- quot )
65 "html.templates.fhtml" use+
66 string-lines parse-template-lines
69 : eval-template ( string -- )
76 M: fhtml call-template* ( filename -- )
77 '[ _ path>> utf8 file-contents eval-template ] assert-depth ;
79 INSTANCE: fhtml template