1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs namespaces make kernel sequences accessors
4 combinators strings splitting io io.streams.string present
5 xml.writer xml.data xml.entities html.forms
6 html.templates html.templates.chloe.syntax ;
7 IN: html.templates.chloe.compiler
9 : chloe-attrs-only ( assoc -- assoc' )
10 [ drop chloe-name? ] assoc-filter ;
12 : non-chloe-attrs-only ( assoc -- assoc' )
13 [ drop chloe-name? not ] assoc-filter ;
15 : chloe-tag? ( tag -- ? )
16 dup xml? [ body>> ] when
18 { [ dup tag? not ] [ f ] }
19 { [ dup chloe-name? not ] [ f ] }
27 DEFER: compile-element
29 : compile-children ( tag -- )
30 [ compile-element ] each ;
32 : [write] ( string -- ) string-buffer get push-all ;
36 [ >string , \ write , ] [ delete-all ] bi
42 : [code-with] ( obj quot -- )
43 reset-buffer [ , ] [ % ] bi* ;
45 : [xml-code] ( quot -- )
46 [ write-xml ] compose [code] ;
48 : expand-attr ( value -- )
49 [ value present write ] [code-with] ;
51 : compile-attr ( value -- )
52 reset-buffer "@" ?head [ , [ value present ] % ] [ , ] if ;
54 : compile-attrs ( assoc -- )
57 swap name>string [write]
59 "@" ?head [ expand-attr ] [ escape-quoted-string [write] ] if
63 : compile-start-tag ( tag -- )
65 [ name>string [write] ] [ attrs>> compile-attrs ] bi
68 : compile-end-tag ( tag -- )
73 : compile-tag ( tag -- )
75 [ main>> tag-stack get push ]
82 ERROR: unknown-chloe-tag tag ;
84 : compile-chloe-tag ( tag -- )
85 dup main>> dup tags get at
90 : compile-element ( element -- )
92 { [ dup chloe-tag? ] [ compile-chloe-tag ] }
93 { [ dup [ tag? ] [ xml? ] bi or ] [ compile-tag ] }
94 { [ dup string? ] [ escape-string [write] ] }
95 { [ dup comment? ] [ drop ] }
96 [ [ write-xml ] [code-with] ]
99 : with-compiler ( quot -- quot' )
101 SBUF" " string-buffer set
102 V{ } clone tag-stack set
107 : compile-chunk ( seq -- )
108 [ compile-element ] each ;
110 : compile-quot ( quot -- )
113 SBUF" " string-buffer set
116 ] [ ] make , ; inline
118 : process-children ( tag quot -- )
119 [ [ compile-children ] compile-quot ] [ % ] bi* ; inline
121 : compile-children>string ( tag -- )
122 [ with-string-writer ] process-children ;
124 : compile-with-scope ( quot -- )
125 compile-quot [ with-scope ] [code] ; inline
127 : if-not-nested ( quot -- )
128 nested-template? get swap unless ; inline
130 : compile-prologue ( xml -- )
132 [ prolog>> [ write-xml ] [code-with] ]
133 [ before>> compile-chunk ]
136 [ if-not-nested ] [code] ;
138 : compile-epilogue ( xml -- )
139 [ after>> compile-chunk ] compile-quot
140 [ if-not-nested ] [code] ;
142 : compile-template ( xml -- quot )