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
5 memoize io io.files io.encodings.utf8 io.streams.string
6 unicode.case mirrors math urls present multiline quotations xml
12 html.templates.chloe.compiler
13 html.templates.chloe.components
14 html.templates.chloe.syntax ;
15 IN: html.templates.chloe
17 ! Chloe is Ed's favorite web designer
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 [ <title> write-title </title> ] [ write-title ] ? [code] ;
33 dup "include" optional-attr [
34 utf8 file-contents [ add-style ] [code-with]
36 compile-children>string [ add-style ] [code]
40 drop [ <style> write-style </style> ] [code] ;
43 [ "index" value even? swap when ] process-children ;
46 [ "index" value odd? swap when ] process-children ;
48 : (bind-tag) ( tag quot -- )
50 [ "name" required-attr compile-attr ] keep
51 ] dip process-children ; inline
53 CHLOE: each [ with-each-value ] (bind-tag) ;
55 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
57 CHLOE: bind [ with-form ] (bind-tag) ;
61 CHLOE: call-next-template
62 drop reset-buffer \ call-next-template , ;
64 : attr>word ( value -- word/f )
65 ":" split1 swap lookup ;
67 : if>quot ( tag -- quot )
69 [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
70 [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
75 CHLOE: if dup if>quot [ swap when ] append process-children ;
77 CHLOE-SINGLETON: label
79 CHLOE-SINGLETON: inspector
80 CHLOE-SINGLETON: comparison
82 CHLOE-SINGLETON: hidden
92 : read-template ( chloe -- xml )
93 path>> ".xml" append utf8 <file-reader> read-xml ;
95 MEMO: template-quot ( chloe -- quot )
96 read-template compile-template ;
98 MEMO: nested-template-quot ( chloe -- quot )
99 read-template compile-nested-template ;
101 : reset-templates ( -- )
102 { template-quot nested-template-quot } [ reset-memoized ] each ;
104 M: chloe call-template*
106 [ nested-template-quot ] [ template-quot ] if
109 INSTANCE: chloe template