1 ! Copyright (C) 2008 Slava Pestov.
2 ! See https://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 ;
9 IN: html.templates.chloe
15 CHLOE: chloe compile-children ;
17 CHLOE: title compile-children>string [ set-title ] [code] ;
21 "head" tag-stack get member?
22 "title" tag-stack get member? not and
23 [ get-title [XML <title><-></title> XML] ]
28 [ "include" optional-attr ]
29 [ utf8 file-contents [ add-style ] [code-with] ]
30 [ compile-children>string [ add-style ] [code] ] ??if ;
35 [XML <style type="text/css"> <-> </style> XML]
39 [ "include" optional-attr ]
40 [ utf8 file-contents [ add-script ] [code-with] ]
41 [ compile-children>string [ add-script ] [code] ] ??if ;
45 get-script "*/" "/*" surround xml.data:<cdata>
46 [XML <script type="text/javascript">/* <-> */</script> XML]
50 [ "name" optional-attr ]
51 [ "content" optional-attr ] bi
52 '[ _ _ add-meta ] [code] ;
55 drop [ get-meta ] [xml-code] ;
58 [ "index" value even? swap when ] process-children ;
61 [ "index" value odd? swap when ] process-children ;
63 : (bind-tag) ( tag quot -- )
65 [ "name" required-attr compile-attr ] keep
66 ] dip process-children ; inline
68 CHLOE: each [ with-each-value ] (bind-tag) ;
70 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
72 CHLOE: bind [ with-form ] (bind-tag) ;
76 CHLOE: call-next-template
77 drop reset-buffer \ call-next-template , ;
79 CHLOE: validation-errors
80 drop [ render-validation-errors ] [code] ;
82 : attr>word ( value -- word/f )
83 ":" split1 swap lookup-word ;
85 : if>quot ( tag -- quot )
87 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
88 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
93 CHLOE: if dup if>quot [ swap when ] append process-children ;
110 SYMBOL: template-cache
112 H{ } template-cache set-global
114 TUPLE: cached-template-state path last-modified quot ;
116 : load-template ( chloe -- cached-template )
119 [ file-info modified>> ]
120 [ file>xml compile-template ] tri
121 \ cached-template-state boa ;
123 \ load-template DEBUG add-input-logging
125 : cached-template ( chloe -- cached-template/f )
126 template-cache get at* [
128 [ path>> file-info modified>> ]
134 : template-quot ( chloe -- quot )
135 [ cached-template ] [
136 [ load-template dup ] keep
137 template-cache get set-at
141 template-cache get clear-assoc ;
143 M: chloe call-template*
144 template-quot call( -- ) ;
146 INSTANCE: chloe template