factor/basis/xml/writer/writer.factor

142 lines
3.5 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2005, 2006 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: hashtables kernel math namespaces sequences strings
2008-08-27 18:02:54 -04:00
assocs combinators io io.streams.string accessors
2008-04-15 07:10:08 -04:00
xml.data wrap xml.entities unicode.categories ;
2007-09-20 18:09:08 -04:00
IN: xml.writer
2007-12-29 01:33:21 -05:00
SYMBOL: xml-pprint?
SYMBOL: sensitive-tags
SYMBOL: indentation
SYMBOL: indenter
" " indenter set-global
: sensitive? ( tag -- ? )
sensitive-tags get swap [ names-match? ] curry contains? ;
2008-01-30 16:03:02 -05:00
: indent-string ( -- string )
2008-01-31 01:47:11 -05:00
xml-pprint? get
[ indentation get indenter get <repetition> concat ]
[ "" ] if ;
2008-01-30 16:03:02 -05:00
2007-12-29 01:33:21 -05:00
: ?indent ( -- )
2008-01-30 16:03:02 -05:00
xml-pprint? get [ nl indent-string write ] when ;
2007-12-29 01:33:21 -05:00
: indent ( -- )
xml-pprint? get [ 1 indentation +@ ] when ;
: unindent ( -- )
xml-pprint? get [ -1 indentation +@ ] when ;
: trim-whitespace ( string -- no-whitespace )
2008-04-13 04:52:40 -04:00
[ blank? ] trim ;
2007-12-29 01:33:21 -05:00
: ?filter-children ( children -- no-whitespace )
xml-pprint? get [
[ dup string? [ trim-whitespace ] when ] map
2008-09-06 20:13:59 -04:00
[ [ empty? ] [ string? ] bi and not ] filter
2007-12-29 01:33:21 -05:00
] when ;
: name>string ( name -- string )
[ main>> ] [ space>> ] bi [ ":" swap 3append ] unless-empty ;
2007-09-20 18:09:08 -04:00
: print-name ( name -- )
name>string write ;
2007-09-20 18:09:08 -04:00
2007-12-23 14:57:39 -05:00
: print-attrs ( assoc -- )
2007-09-20 18:09:08 -04:00
[
2007-12-23 14:57:39 -05:00
" " write
2007-09-20 18:09:08 -04:00
swap print-name
"=\"" write
2007-12-29 01:33:21 -05:00
escape-quoted-string write
2007-09-20 18:09:08 -04:00
"\"" write
2007-12-23 14:57:39 -05:00
] assoc-each ;
2007-09-20 18:09:08 -04:00
GENERIC: write-item ( object -- )
M: string write-item
2008-01-31 01:47:11 -05:00
escape-string dup empty? not xml-pprint? get and
2008-01-30 16:03:02 -05:00
[ nl 80 indent-string indented-break ] when write ;
2007-09-20 18:09:08 -04:00
2007-12-29 01:33:21 -05:00
: write-tag ( tag -- )
2008-01-30 16:03:02 -05:00
?indent CHAR: < write1
2008-08-27 18:02:54 -04:00
dup print-name attrs>> print-attrs ;
2007-12-29 01:33:21 -05:00
2008-04-15 07:10:08 -04:00
: write-start-tag ( tag -- )
write-tag ">" write ;
2007-12-29 01:33:21 -05:00
M: contained-tag write-item
write-tag "/>" write ;
: write-children ( tag -- )
2008-08-27 18:02:54 -04:00
indent children>> ?filter-children
2008-01-30 16:03:02 -05:00
[ write-item ] each unindent ;
2007-12-29 01:33:21 -05:00
: write-end-tag ( tag -- )
?indent "</" write print-name CHAR: > write1 ;
2007-09-20 18:09:08 -04:00
M: open-tag write-item
2008-04-15 07:10:08 -04:00
xml-pprint? get >r
{
[ sensitive? not xml-pprint? get and xml-pprint? set ]
[ write-start-tag ]
[ write-children ]
[ write-end-tag ]
} cleave
r> xml-pprint? set ;
2007-09-20 18:09:08 -04:00
M: comment write-item
2008-08-27 18:02:54 -04:00
"<!--" write text>> write "-->" write ;
2007-09-20 18:09:08 -04:00
M: directive write-item
2008-08-27 18:02:54 -04:00
"<!" write text>> write CHAR: > write1 ;
2007-09-20 18:09:08 -04:00
M: instruction write-item
2008-08-27 18:02:54 -04:00
"<?" write text>> write "?>" write ;
2007-09-20 18:09:08 -04:00
: write-prolog ( xml -- )
2008-08-27 18:02:54 -04:00
"<?xml version=\"" write dup version>> write
"\" encoding=\"" write dup encoding>> write
standalone>> [ "\" standalone=\"yes" write ] when
2008-01-30 16:03:02 -05:00
"\"?>" write ;
2007-09-20 18:09:08 -04:00
: write-chunk ( seq -- )
[ write-item ] each ;
: write-xml ( xml -- )
2008-04-15 07:10:08 -04:00
{
2008-08-27 18:02:54 -04:00
[ prolog>> write-prolog ]
[ before>> write-chunk ]
[ body>> write-item ]
[ after>> write-chunk ]
2008-04-15 07:10:08 -04:00
} cleave ;
2007-09-20 18:09:08 -04:00
M: xml write-item
body>> write-item ;
2007-09-20 18:09:08 -04:00
: print-xml ( xml -- )
write-xml nl ;
: xml>string ( xml -- string )
[ write-xml ] with-string-writer ;
2007-09-20 18:09:08 -04:00
2007-12-29 01:33:21 -05:00
: with-xml-pprint ( sensitive-tags quot -- )
[
swap [ assure-name ] map sensitive-tags set
0 indentation set
xml-pprint? on
call
] with-scope ; inline
: pprint-xml-but ( xml sensitive-tags -- )
[ print-xml ] with-xml-pprint ;
: pprint-xml ( xml -- )
f pprint-xml-but ;
: pprint-xml>string-but ( xml sensitive-tags -- string )
[ xml>string ] with-xml-pprint ;
: pprint-xml>string ( xml -- string )
f pprint-xml>string-but ;