factor/basis/xml/entities/entities.factor

43 lines
1.0 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.
2011-11-29 15:21:08 -05:00
USING: namespaces make kernel assocs sequences fry
io.files io.encodings.binary xml.state ;
2007-09-20 18:09:08 -04:00
IN: xml.entities
CONSTANT: entities-out
2008-01-30 16:03:02 -05:00
H{
2017-06-04 17:00:02 -04:00
{ char: < "&lt;" }
{ char: > "&gt;" }
{ char: & "&amp;" }
}
2008-01-30 16:03:02 -05:00
CONSTANT: quoted-entities-out
2008-01-30 16:03:02 -05:00
H{
2017-06-04 17:00:02 -04:00
{ char: & "&amp;" }
{ char: ' "&apos;" }
{ char: \" "&quot;" }
{ char: < "&lt;" }
}
2008-01-30 16:03:02 -05:00
: escape-string-by ( str table -- escaped )
2015-09-08 19:15:10 -04:00
! Convert <, >, &, ' and " to HTML entities.
[ '[ dup _ at [ % ] [ , ] ?if ] each ] "" make ;
2008-01-30 16:03:02 -05:00
: escape-string ( str -- newstr )
entities-out escape-string-by ;
: escape-quoted-string ( str -- newstr )
quoted-entities-out escape-string-by ;
CONSTANT: entities
2007-09-20 18:09:08 -04:00
H{
2017-06-04 17:00:02 -04:00
{ "lt" char: < }
{ "gt" char: > }
{ "amp" char: & }
{ "apos" char: ' }
{ "quot" char: \" }
}
2007-09-20 18:09:08 -04:00
: with-entities ( entities quot -- )
[ extra-entities ] dip with-variable ; inline