]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/entities/entities.factor
Merge branch 'master' into new_ui
[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 values
4 io.files io.encodings.binary xml.state ;
5 IN: xml.entities
6
7 : entities-out
8     H{
9         { CHAR: < "&lt;"   }
10         { CHAR: > "&gt;"   }
11         { CHAR: & "&amp;"  }
12     } ;
13
14 : quoted-entities-out
15     H{
16         { CHAR: & "&amp;"  }
17         { CHAR: ' "&apos;" }
18         { CHAR: " "&quot;" }
19     } ;
20
21 : escape-string-by ( str table -- escaped )
22     #! Convert <, >, &, ' and " to HTML entities.
23     [ '[ dup _ 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 : 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     [ swap extra-entities set call ] with-scope ; inline