1 ! Copyright (C) 2005 Alex Chapman
2 ! Copyright (C) 2006, 2009 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 accessors
6 assocs fry vocabs.parser parser lexer io io.files call
7 io.streams.string io.encodings.utf8 html.templates ;
8 IN: html.templates.fhtml
10 ! We use a custom lexer so that %> ends a token even if not
11 ! followed by whitespace
12 TUPLE: template-lexer < lexer ;
14 : <template-lexer> ( lines -- lexer )
15 template-lexer new-lexer ;
17 M: template-lexer skip-word
20 { [ 2dup nth CHAR: " = ] [ drop 1+ ] }
21 { [ 2dup swap tail-slice "%>" head? ] [ drop 2 + ] }
24 ] change-lexer-column ;
28 : check-<% ( lexer -- col )
29 "<%" over line-text>> rot column>> start* ;
31 : found-<% ( accum lexer col -- accum )
34 [ column>> ] 2dip subseq parsed
36 ] 2keep 2 + >>column drop ;
38 : still-looking ( accum lexer -- accum )
40 [ line-text>> ] [ column>> ] bi tail
44 : parse-%> ( accum lexer -- accum )
47 [ found-<% ] [ [ still-looking ] keep parse-%> ] if*
52 : %> lexer get parse-%> ; parsing
54 : parse-template-lines ( lines -- quot )
56 V{ } clone lexer get parse-%> f (parse-until) >quotation
59 : parse-template ( string -- quot )
63 "html.templates.fhtml" use+
64 string-lines parse-template-lines
67 : eval-template ( string -- )
74 M: fhtml call-template* ( filename -- )
75 '[ _ path>> utf8 file-contents eval-template ] call( -- ) ;
77 INSTANCE: fhtml template