1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel sequences combinators kernel fry
4 namespaces make classes.tuple assocs splitting words arrays io
5 io.files io.files.info io.encodings.utf8 io.streams.string
6 unicode.case mirrors math urls present multiline quotations xml
8 xml.data xml.writer xml.syntax strings
13 html.templates.chloe.compiler
14 html.templates.chloe.components
15 html.templates.chloe.syntax ;
16 IN: html.templates.chloe
22 CHLOE: chloe compile-children ;
24 CHLOE: title compile-children>string [ set-title ] [code] ;
28 "head" tag-stack get member?
29 "title" tag-stack get member? not and
30 [ get-title [XML <title><-></title> XML] ]
35 dup "include" optional-attr [
36 utf8 file-contents [ add-style ] [code-with]
38 compile-children>string [ add-style ] [code]
44 [XML <style type="text/css"> <-> </style> XML]
48 [ "index" value even? swap when ] process-children ;
51 [ "index" value odd? swap when ] process-children ;
53 : (bind-tag) ( tag quot -- )
55 [ "name" required-attr compile-attr ] keep
56 ] dip process-children ; inline
58 CHLOE: each [ with-each-value ] (bind-tag) ;
60 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
62 CHLOE: bind [ with-form ] (bind-tag) ;
66 CHLOE: call-next-template
67 drop reset-buffer \ call-next-template , ;
69 CHLOE: validation-errors
70 drop [ render-validation-errors ] [code] ;
72 : attr>word ( value -- word/f )
73 ":" split1 swap lookup ;
75 : if>quot ( tag -- quot )
77 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
78 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
83 CHLOE: if dup if>quot [ swap when ] append process-children ;
100 SYMBOL: template-cache
102 H{ } template-cache set-global
104 TUPLE: cached-template path last-modified quot ;
106 : load-template ( chloe -- cached-template )
109 [ file-info modified>> ]
110 [ file>xml compile-template ] tri
111 \ cached-template boa ;
113 \ load-template DEBUG add-input-logging
115 : cached-template ( chloe -- cached-template/f )
116 template-cache get at* [
118 [ path>> file-info modified>> ]
124 : template-quot ( chloe -- quot )
125 dup cached-template [ ] [
126 [ load-template dup ] keep
127 template-cache get set-at
131 template-cache get clear-assoc ;
133 M: chloe call-template*
134 template-quot call( -- ) ;
136 INSTANCE: chloe template