]> gitweb.factorcode.org Git - factor.git/blob - basis/html/templates/chloe/chloe.factor
45e59c3b6d06e880d95cf6db785111dbf2891071
[factor.git] / basis / html / templates / chloe / chloe.factor
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 classes.tuple assocs splitting words arrays memoize
5 io io.files io.encodings.utf8 io.streams.string unicode.case
6 mirrors math urls present multiline quotations xml xml.data
7 html.forms
8 html.elements
9 html.components
10 html.templates
11 html.templates.chloe.compiler
12 html.templates.chloe.components
13 html.templates.chloe.syntax ;
14 IN: html.templates.chloe
15
16 ! Chloe is Ed's favorite web designer
17 TUPLE: chloe path ;
18
19 C: <chloe> chloe
20
21 CHLOE: chloe compile-children ;
22
23 CHLOE: title compile-children>string [ set-title ] [code] ;
24
25 CHLOE: write-title
26     drop
27     "head" tag-stack get member?
28     "title" tag-stack get member? not and
29     [ <title> write-title </title> ] [ write-title ] ? [code] ;
30
31 CHLOE: style
32     dup "include" optional-attr [
33         utf8 file-contents [ add-style ] [code-with]
34     ] [
35         compile-children>string [ add-style ] [code]
36     ] ?if ;
37
38 CHLOE: write-style
39     drop [ <style> write-style </style> ] [code] ;
40
41 CHLOE: even
42     [ "index" value even? swap when ] process-children ;
43
44 CHLOE: odd
45     [ "index" value odd? swap when ] process-children ;
46
47 : (bind-tag) ( tag quot -- )
48     [
49         [ "name" required-attr compile-attr ] keep
50     ] dip process-children ; inline
51
52 CHLOE: each [ with-each-value ] (bind-tag) ;
53
54 CHLOE: bind-each [ with-each-object ] (bind-tag) ;
55
56 CHLOE: bind [ with-form ] (bind-tag) ;
57
58 CHLOE: comment drop ;
59
60 CHLOE: call-next-template
61     drop reset-buffer \ call-next-template , ;
62
63 : attr>word ( value -- word/f )
64     ":" split1 swap lookup ;
65
66 : if>quot ( tag -- quot )
67     [
68         [ "code" optional-attr [ attr>word [ , ] [ f , ] if* ] [ t , ] if* ]
69         [ "value" optional-attr [ , \ value , ] [ t , ] if* ]
70         bi
71         \ and ,
72     ] [ ] make ;
73
74 CHLOE: if dup if>quot [ swap when ] append process-children ;
75
76 CHLOE-SINGLETON: label
77 CHLOE-SINGLETON: link
78 CHLOE-SINGLETON: inspector
79 CHLOE-SINGLETON: comparison
80 CHLOE-SINGLETON: html
81 CHLOE-SINGLETON: hidden
82
83 CHLOE-TUPLE: farkup
84 CHLOE-TUPLE: field
85 CHLOE-TUPLE: textarea
86 CHLOE-TUPLE: password
87 CHLOE-TUPLE: choice
88 CHLOE-TUPLE: checkbox
89 CHLOE-TUPLE: code
90
91 : read-template ( chloe -- xml )
92     path>> ".xml" append utf8 <file-reader> read-xml ;
93
94 MEMO: template-quot ( chloe -- quot )
95     read-template compile-template ;
96
97 MEMO: nested-template-quot ( chloe -- quot )
98     read-template compile-nested-template ;
99
100 : reset-templates ( -- )
101     { template-quot nested-template-quot } [ reset-memoized ] each ;
102
103 M: chloe call-template*
104     nested-template? get
105     [ nested-template-quot ] [ template-quot ] if
106     assert-depth ;
107
108 INSTANCE: chloe template