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 dup "include" optional-attr [
41 utf8 file-contents [ add-script ] [code-with]
43 compile-children>string [ add-script ] [code]
49 [XML <script type="text/javascript"> <-> </script> XML]
53 [ "name" optional-attr ]
54 [ "content" optional-attr ] bi
55 '[ _ _ add-meta ] [code] ;
58 drop [ get-meta ] [xml-code] ;
61 [ "index" value even? swap when ] process-children ;
64 [ "index" value odd? swap when ] process-children ;
66 : (bind-tag) ( tag quot -- )
68 [ "name" required-attr compile-attr ] keep
69 ] dip process-children ; inline
71 CHLOE: each [ with-each-value ] (bind-tag) ;
73 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
75 CHLOE: bind [ with-form ] (bind-tag) ;
79 CHLOE: call-next-template
80 drop reset-buffer \ call-next-template , ;
82 CHLOE: validation-errors
83 drop [ render-validation-errors ] [code] ;
85 : attr>word ( value -- word/f )
86 ":" split1 swap lookup-word ;
88 : if>quot ( tag -- quot )
90 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
91 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
96 CHLOE: if dup if>quot [ swap when ] append process-children ;
101 COMPONENT: comparison
113 SYMBOL: template-cache
115 H{ } template-cache set-global
117 TUPLE: cached-template-state path last-modified quot ;
119 : load-template ( chloe -- cached-template )
122 [ file-info modified>> ]
123 [ file>xml compile-template ] tri
124 \ cached-template-state boa ;
126 \ load-template DEBUG add-input-logging
128 : cached-template ( chloe -- cached-template/f )
129 template-cache get at* [
131 [ path>> file-info modified>> ]
137 : template-quot ( chloe -- quot )
138 dup cached-template [ ] [
139 [ load-template dup ] keep
140 template-cache get set-at
144 template-cache get clear-assoc ;
146 M: chloe call-template*
147 template-quot call( -- ) ;
149 INSTANCE: chloe template