]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/utils/utils.factor
html.parser.utils: adding a simple html escape/unescape.
[factor.git] / extra / html / parser / utils / utils.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs kernel quoting sequences splitting ;
4 IN: html.parser.utils
5
6 : trim1 ( seq ch -- newseq )
7     [ [ ?head-slice drop ] [ ?tail-slice drop ] bi ] 2keep drop like ;
8
9 : single-quote ( str -- newstr ) "'" dup surround ;
10
11 : double-quote ( str -- newstr ) "\"" dup surround ;
12
13 : quote ( str -- newstr )
14     CHAR: ' over member?
15     [ double-quote ] [ single-quote ] if ;
16
17 : ?quote ( str -- newstr ) dup quoted? [ quote ] unless ;
18
19 CONSTANT: html-entities H{
20     { """ "\"" }
21     { "&lt;" "<" }
22     { "&gt;" ">" }
23     { "&amp;" "&" }
24     { "&#39;" "'" }
25 }
26
27 : html-unescape ( str -- str' )
28     html-entities [ replace ] assoc-each ;
29
30 : html-escape ( str -- str' )
31     html-entities [ swap replace ] assoc-each ;