esc.go (view raw)
1package blackfriday
2
3import (
4 "html"
5 "io"
6)
7
8type escMap struct {
9 char byte
10 seq []byte
11}
12
13var htmlEscaper = []escMap{
14 {'&', []byte("&")},
15 {'<', []byte("<")},
16 {'>', []byte(">")},
17 {'"', []byte(""")},
18}
19
20func escapeHTML(w io.Writer, s []byte) {
21 var start, end int
22 var sEnd byte
23 for end < len(s) {
24 sEnd = s[end]
25 if sEnd == '&' || sEnd == '<' || sEnd == '>' || sEnd == '"' {
26 for i := 0; i < len(htmlEscaper); i++ {
27 if sEnd == htmlEscaper[i].char {
28 w.Write(s[start:end])
29 w.Write(htmlEscaper[i].seq)
30 start = end + 1
31 break
32 }
33 }
34 }
35 end++
36 }
37 if start < len(s) && end <= len(s) {
38 w.Write(s[start:end])
39 }
40}
41
42func escLink(w io.Writer, text []byte) {
43 unesc := html.UnescapeString(string(text))
44 escapeHTML(w, []byte(unesc))
45}