1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit delegate delegate.protocols kernel
5 sequences slots strings vectors words ;
8 TUPLE: interpolated var ;
9 C: <interpolated> interpolated
12 { space maybe{ string } }
14 { url maybe{ string } } ;
17 : ?= ( object/f object/f -- ? )
18 2dup and [ = ] [ 2drop t ] if ;
20 : names-match? ( name1 name2 -- ? )
22 [ [ space>> ] bi@ ?= ]
27 : <simple-name> ( string -- name )
30 : <null-name> ( string -- name )
33 : assure-name ( string/name -- name )
34 dup name? [ <null-name> ] unless ;
36 TUPLE: attrs { alist sequence } ;
39 : attr@ ( key alist -- index {key,value} )
40 [ assure-name ] dip alist>>
41 [ first names-match? ] with find ;
44 attr@ nip [ second t ] [ f f ] if* ;
49 [ assure-name swap 2array ] dip
50 [ alist>> ?push ] keep alist<<
53 M: attrs assoc-size alist>> length ;
54 M: attrs new-assoc drop <vector> <attrs> ;
55 M: attrs >alist alist>> ;
57 : >attrs ( assoc -- attrs )
60 [ [ assure-name ] dip ] assoc-map
63 drop dup attrs? [ >attrs ] unless ;
68 [ nip ] [ attr@ drop ] 2bi
69 [ swap alist>> remove-nth! drop ] [ drop ] if* ;
72 alist>> clone <attrs> ;
76 TUPLE: opener { name name } { attrs attrs } ;
79 TUPLE: closer { name name } ;
82 TUPLE: contained { name name } { attrs attrs } ;
83 C: <contained> contained
85 TUPLE: comment { text string } ;
88 TUPLE: cdata { text string } ;
93 TUPLE: element-decl < directive
95 { content-spec string } ;
96 C: <element-decl> element-decl
98 TUPLE: attlist-decl < directive
100 { att-defs string } ;
101 C: <attlist-decl> attlist-decl
103 TUPLE: entity-decl < directive
107 C: <entity-decl> entity-decl
109 TUPLE: system-id { system-literal string } ;
110 C: <system-id> system-id
112 TUPLE: public-id { pubid-literal string } { system-literal string } ;
113 C: <public-id> public-id
115 UNION: id system-id public-id ;
118 { directives sequence }
120 { parameter-entities assoc } ;
123 TUPLE: doctype-decl < directive
125 { external-id maybe{ id } }
126 { internal-subset maybe{ dtd } } ;
127 C: <doctype-decl> doctype-decl
129 TUPLE: notation-decl < directive
132 C: <notation-decl> notation-decl
134 TUPLE: instruction { text string } ;
135 C: <instruction> instruction
140 { standalone boolean } ;
146 { children sequence } ;
148 : <tag> ( name attrs children -- tag )
149 [ assure-name ] [ T{ attrs } assoc-like ] [ ] tri*
152 : attr ( tag/xml name -- string )
155 : set-attr ( tag/xml value name -- )
158 ! They also follow the sequence protocol (for children)
159 CONSULT: sequence-protocol tag children>> ;
160 INSTANCE: tag sequence
162 ! They also follow the assoc protocol (for attributes)
163 CONSULT: assoc-protocol tag attrs>> ;
166 CONSULT: name tag name>> ;
170 [ name>> ] keep attrs>>
171 rot dup [ V{ } like ] when <tag>
174 MACRO: clone-slots ( class -- quot )
177 [ name>> reader-word '[ _ execute clone ] ] map
179 ] [ '[ _ boa ] ] bi compose ;
191 CONSULT: sequence-protocol xml body>> ;
192 INSTANCE: xml sequence
194 CONSULT: tag xml body>> ;
196 CONSULT: name xml body>> ;
199 : tag>xml ( xml tag -- newxml )
200 [ [ prolog>> ] [ before>> ] [ after>> ] tri ] dip
203 : sequence>xml ( xml seq -- newxml )
204 over body>> like tag>xml ;
211 swap dup xml? [ nip ] [
212 dup tag? [ tag>xml ] [ sequence>xml ] if
215 ! tag with children=f is contained
216 : <contained-tag> ( name attrs -- tag )
219 PREDICATE: contained-tag < tag children>> empty? ;
220 PREDICATE: open-tag < tag children>> empty? not ;
222 TUPLE: unescaped string ;
223 C: <unescaped> unescaped
226 tag comment cdata string directive instruction unescaped ;
228 TUPLE: xml-chunk seq ;
229 C: <xml-chunk> xml-chunk
231 CONSULT: sequence-protocol xml-chunk seq>> ;
232 INSTANCE: xml-chunk sequence