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