1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel combinators assocs
4 namespaces sequences splitting words
5 fry urls multiline present
16 html.templates.chloe.compiler
17 html.templates.chloe.syntax
20 http.server.redirection
24 IN: furnace.chloe-tags
27 : parse-query-attr ( string -- assoc )
28 [ f ] [ "," split [ dup value ] H{ } map>assoc ] if-empty ;
30 : a-url-path ( href rest -- string )
32 [ [ "/" ?tail drop "/" ] dip present 3append ] when* ;
34 : a-url ( href rest query value-name -- url )
35 dup [ [ 3drop ] dip value ] [
38 swap parse-query-attr >>query
39 -rot a-url-path >>path
43 : compile-a-url ( tag -- )
45 [ "href" optional-attr compile-attr ]
46 [ "rest" optional-attr compile-attr ]
47 [ "query" optional-attr compile-attr ]
48 [ "value" optional-attr compile-attr ]
49 } cleave [ a-url ] [code] ;
52 [ compile-children>xml-string ] [ compile-a-url ] bi
53 [ add-atom-feed ] [code] ;
55 CHLOE: write-atom drop [ write-atom-feeds ] [code] ;
57 : compile-link-attrs ( tag -- )
58 ! Side-effects current namespace.
59 '[ [ [ _ ] dip link-attr ] each-responder ] [code] ;
61 : process-attrs ( assoc -- newassoc )
62 [ "@" ?head [ value present ] when ] assoc-map ;
64 : non-chloe-attrs ( tag -- )
65 attrs>> non-chloe-attrs-only [ process-attrs ] [code-with] ;
69 [ compile-link-attrs ]
71 [ present swap "href" swap [ set-at ] keep ] [code] ;
76 [ compile-children>xml-string ] bi
77 [ <unescaped> [XML <a><-></a> XML] second swap >>attrs ]
79 ] compile-with-scope ;
82 compile-a-url [ [XML <base href=<->/> XML] ] [xml-code] ;
84 : hidden-nested-fields ( -- xml )
85 nested-forms get " " join f like nested-forms-key
88 : render-hidden ( for -- xml )
89 [ "," split [ hidden render>xml ] map ] [ f ] if* ;
91 : compile-hidden-form-fields ( for -- )
96 [XML <div style="display: none;"><-><-><-></div> XML]
99 : (compile-form-attrs) ( method action -- )
100 ! Leaves an assoc on the stack at runtime
101 [ compile-attr [ "method" pick set-at ] [code] ]
102 [ compile-attr [ resolve-base-path "action" pick set-at ] [code] ]
105 : compile-method/action ( tag -- )
106 ! generated code is ( assoc -- assoc )
107 [ "method" optional-attr "post" or ]
108 [ "action" required-attr ] bi
109 (compile-form-attrs) ;
111 : compile-form-attrs ( tag -- )
113 [ compile-link-attrs ]
114 [ compile-method/action ] tri ;
116 : hidden-fields ( tag -- )
117 "for" optional-attr compile-hidden-form-fields ;
121 [ compile-form-attrs ]
123 [ compile-children>xml-string ] tri
125 <unescaped> [XML <form><-><-></form> XML] second
129 ] compile-with-scope ;
131 : button-tag-markup ( -- xml )
133 <t:form class="inline" xmlns:t="http://factorcode.org/chloe/1.0">
134 <div style="display: inline;"><button type="submit"></button></div>
138 : add-tag-attrs ( attrs tag -- )
139 attrs>> swap assoc-union! drop ;
144 [ [ attrs>> chloe-attrs-only ] dip add-tag-attrs ]
145 [ [ attrs>> non-chloe-attrs-only ] dip "button" deep-tag-named add-tag-attrs ]
146 [ [ children>> ] dip "button" deep-tag-named children<< ]
148 } 2cleave compile-chloe-tag ;