2006-11-30 17:33:16 -05:00
|
|
|
IN: templating
|
2006-11-26 21:48:21 -05:00
|
|
|
USING: kernel xml sequences hashtables tools io arrays namespaces generic ;
|
|
|
|
|
|
|
|
SYMBOL: ref-table
|
|
|
|
|
2006-11-30 17:33:16 -05:00
|
|
|
: replace ( ref -- object )
|
2006-11-26 21:48:21 -05:00
|
|
|
reference-name ref-table get hash call ;
|
|
|
|
|
2006-11-30 17:33:16 -05:00
|
|
|
: 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 [
|
2006-11-30 17:33:16 -05:00
|
|
|
dup [ ref-string swap set ] hash-each
|
2006-11-26 21:48:21 -05:00
|
|
|
] bind over set-tag-props ;
|
|
|
|
M: reference (r-ref)
|
2006-11-30 17:33:16 -05:00
|
|
|
replace ;
|
2006-11-26 21:48:21 -05:00
|
|
|
M: object (r-ref) ;
|
|
|
|
|
2006-11-30 17:33:16 -05:00
|
|
|
: 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
|
2006-11-30 17:33:16 -05:00
|
|
|
template ;
|
2006-11-26 21:48:21 -05:00
|
|
|
|