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 dup "include" optional-attr [
29 utf8 file-contents [ add-style ] [code-with]
31 compile-children>string [ add-style ] [code]
37 [XML <style type="text/css"> <-> </style> XML]
41 dup "include" optional-attr [
42 utf8 file-contents [ add-script ] [code-with]
44 compile-children>string [ add-script ] [code]
49 get-script "*/" "/*" surround xml.data:<cdata>
50 [XML <script type="text/javascript">/* <-> */</script> XML]
54 [ "name" optional-attr ]
55 [ "content" optional-attr ] bi
56 '[ _ _ add-meta ] [code] ;
59 drop [ get-meta ] [xml-code] ;
62 [ "index" value even? swap when ] process-children ;
65 [ "index" value odd? swap when ] process-children ;
67 : (bind-tag) ( tag quot -- )
69 [ "name" required-attr compile-attr ] keep
70 ] dip process-children ; inline
72 CHLOE: each [ with-each-value ] (bind-tag) ;
74 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
76 CHLOE: bind [ with-form ] (bind-tag) ;
80 CHLOE: call-next-template
81 drop reset-buffer \ call-next-template , ;
83 CHLOE: validation-errors
84 drop [ render-validation-errors ] [code] ;
86 : attr>word ( value -- word/f )
87 ":" split1 swap lookup-word ;
89 : if>quot ( tag -- quot )
91 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
92 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
97 CHLOE: if dup if>quot [ swap when ] append process-children ;
102 COMPONENT: comparison
114 SYMBOL: template-cache
116 H{ } template-cache set-global
118 TUPLE: cached-template-state path last-modified quot ;
120 : load-template ( chloe -- cached-template )
123 [ file-info modified>> ]
124 [ file>xml compile-template ] tri
125 \ cached-template-state boa ;
127 \ load-template DEBUG add-input-logging
129 : cached-template ( chloe -- cached-template/f )
130 template-cache get at* [
132 [ path>> file-info modified>> ]
138 : template-quot ( chloe -- quot )
139 dup cached-template [ ] [
140 [ load-template dup ] keep
141 template-cache get set-at
145 template-cache get clear-assoc ;
147 M: chloe call-template*
148 template-quot call( -- ) ;
150 INSTANCE: chloe template