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
13 html.templates.chloe.compiler
14 html.templates.chloe.components
15 html.templates.chloe.syntax ;
16 IN: html.templates.chloe
18 ! Chloe is Ed's favorite web designer
23 CHLOE: chloe compile-children ;
25 CHLOE: title compile-children>string [ set-title ] [code] ;
29 "head" tag-stack get member?
30 "title" tag-stack get member? not and
31 [ <title> write-title </title> ] [ write-title ] ? [code] ;
34 dup "include" optional-attr [
35 utf8 file-contents [ add-style ] [code-with]
37 compile-children>string [ add-style ] [code]
42 <style "text/css" =type style>
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 ;
99 SYMBOL: template-cache
101 H{ } template-cache set-global
103 TUPLE: cached-template path last-modified quot ;
105 : load-template ( chloe -- cached-template )
108 [ file-info modified>> ]
109 [ utf8 <file-reader> read-xml compile-template ] tri
110 \ cached-template boa ;
112 \ load-template DEBUG add-input-logging
114 : cached-template ( chloe -- cached-template/f )
115 template-cache get at* [
117 [ path>> file-info modified>> ]
123 : template-quot ( chloe -- quot )
124 dup cached-template [ ] [
125 [ load-template dup ] keep
126 template-cache get set-at
130 template-cache get clear-assoc ;
132 M: chloe call-template*
133 template-quot assert-depth ;
135 INSTANCE: chloe template