mirror of
https://github.com/danog/blackfriday.git
synced 2024-11-30 04:29:13 +01:00
46 lines
784 B
Go
46 lines
784 B
Go
package blackfriday
|
|
|
|
import (
|
|
"html"
|
|
"io"
|
|
)
|
|
|
|
type escMap struct {
|
|
char byte
|
|
seq []byte
|
|
}
|
|
|
|
var htmlEscaper = []escMap{
|
|
{'&', []byte("&")},
|
|
{'<', []byte("<")},
|
|
{'>', []byte(">")},
|
|
{'"', []byte(""")},
|
|
}
|
|
|
|
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))
|
|
}
|