]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/entities/entities.factor
674fc10fbb811f153195c4c813cf8ed7d455fd2d
[factor.git] / basis / xml / entities / entities.factor
1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: assocs kernel make namespaces sequences xml.state ;
4 IN: xml.entities
5
6 CONSTANT: entities-out
7     H{
8         { CHAR: < "&lt;"   }
9         { CHAR: > "&gt;"   }
10         { CHAR: & "&amp;"  }
11     }
12
13 CONSTANT: quoted-entities-out
14     H{
15         { CHAR: & "&amp;"  }
16         { CHAR: ' "&apos;" }
17         { CHAR: \" "&quot;" }
18         { CHAR: < "&lt;"   }
19     }
20
21 : escape-string-by ( str table -- escaped )
22     ! Convert <, >, &, ' and " to HTML entities.
23     [ '[ [ _ at ] [ % ] [ , ] ??if ] each ] "" make ;
24
25 : escape-string ( str -- newstr )
26     entities-out escape-string-by ;
27
28 : escape-quoted-string ( str -- newstr )
29     quoted-entities-out escape-string-by ;
30
31 CONSTANT: entities
32     H{
33         { "lt"    CHAR: <  }
34         { "gt"    CHAR: >  }
35         { "amp"   CHAR: &  }
36         { "apos"  CHAR: '  }
37         { "quot"  CHAR: \"  }
38     }
39
40 : with-entities ( entities quot -- )
41     [ extra-entities ] dip with-variable ; inline