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.chloe.syntax ;
7 IN: html.templates.chloe.compiler
9 : chloe-attrs-only ( assoc -- assoc' )
10 [ drop url>> chloe-ns = ] assoc-filter ;
12 : non-chloe-attrs-only ( assoc -- assoc' )
13 [ drop url>> chloe-ns = not ] assoc-filter ;
15 : chloe-tag? ( tag -- ? )
16 dup xml? [ body>> ] when
18 { [ dup tag? not ] [ f ] }
19 { [ dup url>> chloe-ns = 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 : expand-attr ( value -- )
46 [ value present write ] [code-with] ;
48 : compile-attr ( value -- )
49 reset-buffer "@" ?head [ , [ value present ] % ] [ , ] if ;
51 : compile-attrs ( assoc -- )
54 swap name>string [write]
56 "@" ?head [ expand-attr ] [ escape-quoted-string [write] ] if
60 : compile-start-tag ( tag -- )
62 [ name>string [write] ] [ compile-attrs ] bi
65 : compile-end-tag ( tag -- )
70 : compile-tag ( tag -- )
72 [ main>> tag-stack get push ]
76 [ drop tag-stack get pop* ]
79 : compile-chloe-tag ( tag -- )
80 ! "Unknown chloe tag: " prepend throw
81 dup main>> dup tags get at
82 [ curry assert-depth ] [ 2drop ] ?if ;
84 : compile-element ( element -- )
86 { [ dup chloe-tag? ] [ compile-chloe-tag ] }
87 { [ dup [ tag? ] [ xml? ] bi or ] [ compile-tag ] }
88 { [ dup string? ] [ escape-string [write] ] }
89 { [ dup comment? ] [ drop ] }
90 [ [ write-item ] [code-with] ]
93 : with-compiler ( quot -- quot' )
95 SBUF" " string-buffer set
96 V{ } clone tag-stack set
101 : compile-nested-template ( xml -- quot )
102 [ compile-element ] with-compiler ;
104 : compile-chunk ( seq -- )
105 [ compile-element ] each ;
107 : compile-quot ( quot -- )
110 SBUF" " string-buffer set
113 ] [ ] make , ; inline
115 : process-children ( tag quot -- )
116 [ [ compile-children ] compile-quot ] [ % ] bi* ; inline
118 : compile-children>string ( tag -- )
119 [ with-string-writer ] process-children ;
121 : compile-with-scope ( quot -- )
122 compile-quot [ with-scope ] [code] ; inline
124 : compile-template ( xml -- quot )
127 [ prolog>> [ write-prolog ] [code-with] ]
128 [ before>> compile-chunk ]
130 [ after>> compile-chunk ]