]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/tests/templating.factor
Create basis vocab root
[factor.git] / basis / xml / tests / templating.factor
1 USING: kernel xml sequences assocs tools.test io arrays namespaces
2     xml.data xml.utilities xml.writer generic sequences.deep ;
3 IN: xml.tests
4
5 : sub-tag
6     T{ name f f "sub" "http://littledan.onigirihouse.com/namespaces/replace" } ;
7
8 SYMBOL: ref-table
9
10 GENERIC: (r-ref) ( xml -- )
11 M: tag (r-ref)
12     sub-tag over at* [
13         ref-table get at
14         swap set-tag-children
15     ] [ 2drop ] if ;
16 M: object (r-ref) drop ;
17
18 : template ( xml -- )
19     [ (r-ref) ] deep-each ;
20
21 ! Example
22
23 : sample-doc
24     {
25         "<html xmlns:f='http://littledan.onigirihouse.com/namespaces/replace'>"
26         "<body>"
27         "<span f:sub='foo'/>"
28         "<div f:sub='bar'/>"
29         "<p f:sub='baz'>paragraph</p>"
30         "</body></html>"
31     } concat ;
32
33 : test-refs ( -- string )
34     [
35         H{
36             { "foo" { "foo" } }
37             { "bar" { "blah" T{ tag T{ name f "" "a" "" } V{ } f } } }
38             { "baz" f }
39         } ref-table set
40         sample-doc string>xml dup template xml>string
41     ] with-scope ;
42
43 [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns:f=\"http://littledan.onigirihouse.com/namespaces/replace\"><body><span f:sub=\"foo\">foo</span><div f:sub=\"bar\">blah<a/></div><p f:sub=\"baz\"/></body></html>" ] [ test-refs ] unit-test