1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-30 04:29:13 +01:00
blackfriday/esc.go
2016-09-10 14:33:37 +03:00

46 lines
784 B
Go

package blackfriday
import (
"html"
"io"
)
type escMap struct {
char byte
seq []byte
}
var htmlEscaper = []escMap{
{'&', []byte("&")},
{'<', []byte("&lt;")},
{'>', []byte("&gt;")},
{'"', []byte("&quot;")},
}
func escapeHTML(w io.Writer, s []byte) {
var start, end int
var sEnd byte
for end < len(s) {
sEnd = s[end]
if sEnd == '&' || sEnd == '<' || sEnd == '>' || sEnd == '"' {
for i := 0; i < len(htmlEscaper); i++ {
if sEnd == htmlEscaper[i].char {
w.Write(s[start:end])
w.Write(htmlEscaper[i].seq)
start = end + 1
break
}
}
}
end++
}
if start < len(s) && end <= len(s) {
w.Write(s[start:end])
}
}
func escLink(w io.Writer, text []byte) {
unesc := html.UnescapeString(string(text))
escapeHTML(w, []byte(unesc))
}