1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs namespaces kernel sequences accessors combinators
4 strings splitting io io.streams.string present xml.writer
5 xml.data xml.entities html.forms html.templates.chloe.syntax ;
6 IN: html.templates.chloe.compiler
8 : chloe-attrs-only ( assoc -- assoc' )
9 [ drop url>> chloe-ns = ] assoc-filter ;
11 : non-chloe-attrs-only ( assoc -- assoc' )
12 [ drop url>> chloe-ns = not ] assoc-filter ;
14 : chloe-tag? ( tag -- ? )
15 dup xml? [ body>> ] when
17 { [ dup tag? not ] [ f ] }
18 { [ dup url>> chloe-ns = not ] [ f ] }
26 DEFER: compile-element
28 : compile-children ( tag -- )
29 [ compile-element ] each ;
31 : [write] ( string -- ) string-buffer get push-all ;
35 [ >string , \ write , ] [ delete-all ] bi
41 : [code-with] ( obj quot -- )
42 reset-buffer [ , ] [ % ] bi* ;
44 : expand-attr ( value -- )
45 [ value present write ] [code-with] ;
47 : compile-attr ( value -- )
48 reset-buffer "@" ?head [ , [ value present ] % ] [ , ] if ;
50 : compile-attrs ( assoc -- )
53 swap name>string [write]
55 "@" ?head [ expand-attr ] [ escape-quoted-string [write] ] if
59 : compile-start-tag ( tag -- )
61 [ name>string [write] ] [ compile-attrs ] bi
64 : compile-end-tag ( tag -- )
69 : compile-tag ( tag -- )
71 [ main>> tag-stack get push ]
75 [ drop tag-stack get pop* ]
78 : compile-chloe-tag ( tag -- )
79 ! "Unknown chloe tag: " prepend throw
80 dup main>> dup tags get at
81 [ curry assert-depth ] [ 2drop ] ?if ;
83 : compile-element ( element -- )
85 { [ dup chloe-tag? ] [ compile-chloe-tag ] }
86 { [ dup [ tag? ] [ xml? ] bi or ] [ compile-tag ] }
87 { [ dup string? ] [ escape-string [write] ] }
88 { [ dup comment? ] [ drop ] }
89 [ [ write-item ] [code-with] ]
92 : with-compiler ( quot -- quot' )
94 SBUF" " string-buffer set
95 V{ } clone tag-stack set
100 : compile-nested-template ( xml -- quot )
101 [ compile-element ] with-compiler ;
103 : compile-chunk ( seq -- )
104 [ compile-element ] each ;
106 : compile-quot ( quot -- )
109 SBUF" " string-buffer set
112 ] [ ] make , ; inline
114 : process-children ( tag quot -- )
115 [ [ compile-children ] compile-quot ] [ % ] bi* ; inline
117 : compile-children>string ( tag -- )
118 [ with-string-writer ] process-children ;
120 : compile-with-scope ( quot -- )
121 compile-quot [ with-scope ] [code] ; inline
123 : compile-template ( xml -- quot )
126 [ prolog>> [ write-prolog ] [code-with] ]
127 [ before>> compile-chunk ]
129 [ after>> compile-chunk ]