factor/libs/xml/example2.factor

37 lines
785 B
Factor
Raw Normal View History

IN: templating
2006-11-26 21:48:21 -05:00
USING: kernel xml sequences hashtables tools io arrays namespaces generic ;
SYMBOL: ref-table
: replace ( ref -- object )
2006-11-26 21:48:21 -05:00
reference-name ref-table get hash call ;
: ref-string ( seq -- seq )
[
dup reference? [ replace ] when
] map ;
2006-11-26 21:48:21 -05:00
GENERIC: (r-ref) ( xml -- object )
M: any-tag (r-ref)
dup tag-props dup [
dup [ ref-string swap set ] hash-each
2006-11-26 21:48:21 -05:00
] bind over set-tag-props ;
M: reference (r-ref)
replace ;
2006-11-26 21:48:21 -05:00
M: object (r-ref) ;
: template ( xml -- xml )
2006-11-26 21:48:21 -05:00
[ (r-ref) ] xml-map ;
! Example
: test-refs
H{
{ "foo" [ "foo" ] }
{ "bar" [ [ .s ] string-out ] }
{ "baz" [ "<a/>" string>xml delegate ] }
} ref-table set
"<x>%foo;<y prop='blah%foo;'>%bar;</y>%baz;</x>" string>xml
template ;
2006-11-26 21:48:21 -05:00