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
23 QUALIFIED-WITH: assocs a
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 [ >r 3drop r> 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>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 attrs>> '[ [ [ _ ] dip link-attr ] each-responder ] [code] ;
61 : a-start-tag ( tag -- )
63 [ non-chloe-attrs-only compile-attrs ]
64 [ compile-link-attrs ]
69 : a-end-tag ( tag -- )
70 drop [ </a> ] [code] ;
74 [ a-start-tag ] [ compile-children ] [ a-end-tag ] tri
75 ] compile-with-scope ;
78 compile-a-url [ <base =href base/> ] [code] ;
80 : compile-hidden-form-fields ( for -- )
82 <div "display: none;" =style div>
83 _ [ "," split [ hidden render ] each ] when*
84 nested-forms get " " join f like nested-forms-key hidden-form-field
85 [ modify-form ] each-responder
89 : compile-form-attrs ( method action attrs -- )
91 [ compile-attr [ =method ] [code] ]
92 [ compile-attr [ resolve-base-path =action ] [code] ]
97 : form-start-tag ( tag -- )
99 [ "method" optional-attr "post" or ]
100 [ "action" required-attr ]
101 [ attrs>> non-chloe-attrs-only ] tri
104 [ "for" optional-attr compile-hidden-form-fields ] bi ;
106 : form-end-tag ( tag -- )
107 drop [ </form> ] [code] ;
112 [ compile-link-attrs ]
117 ] compile-with-scope ;
119 STRING: button-tag-markup
120 <t:form class="inline" xmlns:t="http://factorcode.org/chloe/1.0">
121 <div style="display: inline;"><button type="submit"></button></div>
125 : add-tag-attrs ( attrs tag -- )
126 attrs>> swap update ;
129 button-tag-markup string>xml body>>
131 [ [ attrs>> chloe-attrs-only ] dip add-tag-attrs ]
132 [ [ attrs>> non-chloe-attrs-only ] dip "button" deep-tag-named add-tag-attrs ]
133 [ [ children>> ] dip "button" deep-tag-named (>>children) ]
135 } 2cleave compile-chloe-tag ;