XML fry
parent
39e49c3554
commit
c33dd1105b
|
@ -8,9 +8,11 @@ IN: xml.elements
|
||||||
|
|
||||||
: take-interpolated ( quot -- interpolated )
|
: take-interpolated ( quot -- interpolated )
|
||||||
interpolating? get [
|
interpolating? get [
|
||||||
drop pass-blank
|
drop get-char CHAR: > =
|
||||||
" \t\r\n-" take-to <interpolated>
|
[ next f ] [
|
||||||
pass-blank "->" expect
|
pass-blank " \t\r\n-" take-to
|
||||||
|
pass-blank "->" expect
|
||||||
|
] if <interpolated>
|
||||||
] [ call ] if ; inline
|
] [ call ] if ; inline
|
||||||
|
|
||||||
: interpolate-quote ( -- interpolated )
|
: interpolate-quote ( -- interpolated )
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: tools.test xml.interpolate multiline kernel assocs
|
USING: tools.test xml.interpolate multiline kernel assocs
|
||||||
sequences accessors xml.writer xml.interpolate.private
|
sequences accessors xml.writer xml.interpolate.private
|
||||||
locals ;
|
locals splitting ;
|
||||||
IN: xml.interpolate.tests
|
IN: xml.interpolate.tests
|
||||||
|
|
||||||
[ "a" "c" { "a" "c" } ] [
|
[ "a" "c" { "a" "c" f } ] [
|
||||||
"<?xml version='1.0'?><x><-a-><b val=<-c->/></x>"
|
"<?xml version='1.0'?><x><-a-><b val=<-c->/><-></x>"
|
||||||
interpolated-doc
|
interpolated-doc
|
||||||
[ second var>> ]
|
[ second var>> ]
|
||||||
[ fourth "val" swap at var>> ]
|
[ fourth "val" swap at var>> ]
|
||||||
|
@ -27,3 +27,20 @@ IN: xml.interpolate.tests
|
||||||
XML> pprint-xml>string
|
XML> pprint-xml>string
|
||||||
]
|
]
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
[ {" <?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<doc>
|
||||||
|
<item>
|
||||||
|
one
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
two
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
three
|
||||||
|
</item>
|
||||||
|
</doc>"} ] [
|
||||||
|
"one two three" " " split
|
||||||
|
[ [XML <item><-></item> XML] ] map
|
||||||
|
<XML <doc><-></doc> XML> pprint-xml>string
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: xml xml.state kernel sequences fry assocs xml.data
|
USING: xml xml.state kernel sequences fry assocs xml.data
|
||||||
accessors strings make multiline parser namespaces macros
|
accessors strings make multiline parser namespaces macros
|
||||||
sequences.deep ;
|
sequences.deep generalizations locals words combinators
|
||||||
|
math ;
|
||||||
IN: xml.interpolate
|
IN: xml.interpolate
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
@ -41,32 +42,48 @@ M: interpolated interpolate-item
|
||||||
: interpolate-xml-doc ( table xml -- xml )
|
: interpolate-xml-doc ( table xml -- xml )
|
||||||
(clone) [ interpolate-tag ] change-body ;
|
(clone) [ interpolate-tag ] change-body ;
|
||||||
|
|
||||||
|
GENERIC# (each-interpolated) 1 ( item quot -- ) inline
|
||||||
|
M: interpolated (each-interpolated) call ;
|
||||||
|
M: tag (each-interpolated)
|
||||||
|
swap attrs>> values
|
||||||
|
[ interpolated? ] filter
|
||||||
|
swap each ;
|
||||||
|
M: object (each-interpolated) 2drop ;
|
||||||
|
|
||||||
|
: each-interpolated ( xml quot -- )
|
||||||
|
'[ _ (each-interpolated) ] deep-each ; inline
|
||||||
|
|
||||||
|
:: number<-> ( doc -- doc )
|
||||||
|
0 :> n! doc [
|
||||||
|
dup var>> [ n >>var n 1+ n! ] unless drop
|
||||||
|
] each-interpolated doc ;
|
||||||
|
|
||||||
MACRO: interpolate-xml ( string -- doc )
|
MACRO: interpolate-xml ( string -- doc )
|
||||||
interpolated-doc '[ _ interpolate-xml-doc ] ;
|
interpolated-doc number<-> '[ _ interpolate-xml-doc ] ;
|
||||||
|
|
||||||
MACRO: interpolate-chunk ( string -- chunk )
|
MACRO: interpolate-chunk ( string -- chunk )
|
||||||
interpolated-chunk '[ _ interpolate-sequence ] ;
|
interpolated-chunk number<-> '[ _ interpolate-sequence ] ;
|
||||||
|
|
||||||
: >search-hash ( seq -- hash )
|
: >search-hash ( seq -- hash )
|
||||||
[ dup search ] H{ } map>assoc ;
|
[ dup search ] H{ } map>assoc ;
|
||||||
|
|
||||||
GENERIC: extract-item ( item -- )
|
|
||||||
M: interpolated extract-item var>> , ;
|
|
||||||
M: tag extract-item
|
|
||||||
attrs>> values
|
|
||||||
[ interpolated? ] filter
|
|
||||||
[ var>> , ] each ;
|
|
||||||
M: object extract-item drop ;
|
|
||||||
|
|
||||||
: extract-variables ( xml -- seq )
|
: extract-variables ( xml -- seq )
|
||||||
[ [ extract-item ] deep-each ] { } make ;
|
[ [ var>> , ] each-interpolated ] { } make ;
|
||||||
|
|
||||||
|
: collect ( accum seq -- accum )
|
||||||
|
{
|
||||||
|
{ [ dup [ ] all? ] [ >search-hash parsed ] } ! locals
|
||||||
|
{ [ dup [ not ] all? ] [ ! fry
|
||||||
|
length parsed \ narray parsed \ <enum> parsed
|
||||||
|
] }
|
||||||
|
[ drop "XML interpolation contains both fry and locals" throw ] ! mixed
|
||||||
|
} cond ;
|
||||||
|
|
||||||
: parse-def ( accum delimiter word -- accum )
|
: parse-def ( accum delimiter word -- accum )
|
||||||
[
|
[
|
||||||
parse-multiline-string [
|
parse-multiline-string
|
||||||
interpolated-chunk extract-variables
|
[ interpolated-chunk extract-variables collect ] keep
|
||||||
>search-hash parsed
|
parsed
|
||||||
] keep parsed
|
|
||||||
] dip parsed ;
|
] dip parsed ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
Loading…
Reference in New Issue