1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs html.components html.forms
4 html.templates html.templates.chloe.compiler
5 html.templates.chloe.components html.templates.chloe.syntax
6 io.encodings.utf8 io.files io.files.info kernel logging make
7 math namespaces sequences splitting words xml xml.syntax ;
8 IN: html.templates.chloe
14 CHLOE: chloe compile-children ;
16 CHLOE: title compile-children>string [ set-title ] [code] ;
20 "head" tag-stack get member?
21 "title" tag-stack get member? not and
22 [ get-title [XML <title><-></title> XML] ]
27 dup "include" optional-attr [
28 utf8 file-contents [ add-style ] [code-with]
30 compile-children>string [ add-style ] [code]
36 [XML <style type="text/css"> <-> </style> XML]
40 [ "index" value even? swap when ] process-children ;
43 [ "index" value odd? swap when ] process-children ;
45 : (bind-tag) ( tag quot -- )
47 [ "name" required-attr compile-attr ] keep
48 ] dip process-children ; inline
50 CHLOE: each [ with-each-value ] (bind-tag) ;
52 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
54 CHLOE: bind [ with-form ] (bind-tag) ;
58 CHLOE: call-next-template
59 drop reset-buffer \ call-next-template , ;
61 CHLOE: validation-errors
62 drop [ render-validation-errors ] [code] ;
64 : attr>word ( value -- word/f )
65 ":" split1 swap lookup-word ;
67 : if>quot ( tag -- quot )
69 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
70 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
75 CHLOE: if dup if>quot [ swap when ] append process-children ;
92 SYMBOL: template-cache
94 H{ } template-cache set-global
96 TUPLE: cached-template-state path last-modified quot ;
98 : load-template ( chloe -- cached-template )
101 [ file-info modified>> ]
102 [ file>xml compile-template ] tri
103 \ cached-template-state boa ;
105 \ load-template DEBUG add-input-logging
107 : cached-template ( chloe -- cached-template/f )
108 template-cache get at* [
110 [ path>> file-info modified>> ]
116 : template-quot ( chloe -- quot )
117 dup cached-template [ ] [
118 [ load-template dup ] keep
119 template-cache get set-at
123 template-cache get clear-assoc ;
125 M: chloe call-template*
126 template-quot call( -- ) ;
128 INSTANCE: chloe template