package template
import (
"bytes"
"fmt"
"io"
)
func StringFormatter(w io.Writer, value interface{}, format string) {
if b, ok := value.([]byte); ok {
w.Write(b)
return
}
fmt.Fprint(w, value)
}
var (
esc_quot = []byte(""")
esc_apos = []byte("'")
esc_amp = []byte("&")
esc_lt = []byte("<")
esc_gt = []byte(">")
)
func HTMLEscape(w io.Writer, s []byte) {
var esc []byte
last := 0
for i, c := range s {
switch c {
case '"':
esc = esc_quot
case '\'':
esc = esc_apos
case '&':
esc = esc_amp
case '<':
esc = esc_lt
case '>':
esc = esc_gt
default:
continue
}
w.Write(s[last:i])
w.Write(esc)
last = i + 1
}
w.Write(s[last:])
}
func HTMLFormatter(w io.Writer, value interface{}, format string) {
var b bytes.Buffer
fmt.Fprint(&b, value)
HTMLEscape(w, b.Bytes())
}