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 compiler.units html.templates io
5 io.encodings.utf8 io.files kernel lexer lexer.private math
6 namespaces parser parser.notes quotations sequences splitting
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
19 2dup swap tail-slice "%>" head?
20 [ drop 2 + ] [ (skip-word) ] if
21 ] change-lexer-column ;
25 : check-<% ( lexer -- col )
26 "<%" swap [ line-text>> ] [ column>> ] bi subseq-start-from ;
28 : found-<% ( accum lexer col -- accum )
31 [ column>> ] 2dip subseq suffix!
33 ] 2keep 2 + >>column drop ;
35 : still-looking ( accum lexer -- accum )
37 [ line-text>> ] [ column>> ] bi tail
38 suffix! \ print suffix!
41 : parse-%> ( accum lexer -- accum )
44 [ found-<% ] [ [ still-looking ] keep parse-%> ] if*
49 SYNTAX: %> lexer get parse-%> ;
51 : parse-template-lines ( lines -- quot )
53 V{ } clone lexer get parse-%> f (parse-until) >quotation
56 : parse-template ( string -- quot )
60 "html.templates.fhtml" use-vocab
61 split-lines parse-template-lines
63 ] with-compilation-unit ;
65 : eval-template ( string -- )
66 parse-template call( -- ) ;
72 M: fhtml call-template*
73 path>> utf8 file-contents eval-template ;
75 INSTANCE: fhtml template