2009-04-09 01:10:46 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Template library: default formatters
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
2009-06-29 16:24:23 -06:00
|
|
|
"bytes";
|
2009-04-09 01:10:46 -06:00
|
|
|
"fmt";
|
2009-04-13 20:29:23 -06:00
|
|
|
"io";
|
2009-06-29 16:24:23 -06:00
|
|
|
"strings";
|
2009-04-09 01:10:46 -06:00
|
|
|
)
|
|
|
|
|
2009-04-14 01:06:49 -06:00
|
|
|
// StringFormatter formats into the default string representation.
|
2009-04-09 01:10:46 -06:00
|
|
|
// It is stored under the name "str" and is the default formatter.
|
|
|
|
// You can override the default formatter by storing your default
|
|
|
|
// under the name "" in your custom formatter map.
|
2009-05-08 12:22:57 -06:00
|
|
|
func StringFormatter(w io.Writer, value interface{}, format string) {
|
2009-11-30 11:29:14 -07:00
|
|
|
if b, ok := value.([]byte); ok {
|
|
|
|
w.Write(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, value);
|
2009-04-09 01:10:46 -06:00
|
|
|
}
|
2009-04-15 01:05:47 -06:00
|
|
|
|
2009-11-03 23:52:10 -07:00
|
|
|
var (
|
2009-11-05 10:40:28 -07:00
|
|
|
esc_quot = strings.Bytes("""); // shorter than """
|
|
|
|
esc_apos = strings.Bytes("'"); // shorter than "'"
|
|
|
|
esc_amp = strings.Bytes("&");
|
|
|
|
esc_lt = strings.Bytes("<");
|
|
|
|
esc_gt = strings.Bytes(">");
|
2009-11-03 23:52:10 -07:00
|
|
|
)
|
2009-04-15 01:05:47 -06:00
|
|
|
|
2009-11-08 22:46:20 -07:00
|
|
|
// HTMLEscape writes to w the properly escaped HTML equivalent
|
2009-04-15 01:05:47 -06:00
|
|
|
// of the plain text data s.
|
2009-11-08 22:46:20 -07:00
|
|
|
func HTMLEscape(w io.Writer, s []byte) {
|
2009-11-03 23:52:10 -07:00
|
|
|
var esc []byte;
|
2009-04-15 01:05:47 -06:00
|
|
|
last := 0;
|
|
|
|
for i, c := range s {
|
2009-11-03 23:52:10 -07:00
|
|
|
switch c {
|
|
|
|
case '"':
|
2009-11-09 13:07:39 -07:00
|
|
|
esc = esc_quot
|
2009-11-03 23:52:10 -07:00
|
|
|
case '\'':
|
2009-11-09 13:07:39 -07:00
|
|
|
esc = esc_apos
|
2009-11-03 23:52:10 -07:00
|
|
|
case '&':
|
2009-11-09 13:07:39 -07:00
|
|
|
esc = esc_amp
|
2009-11-03 23:52:10 -07:00
|
|
|
case '<':
|
2009-11-09 13:07:39 -07:00
|
|
|
esc = esc_lt
|
2009-11-03 23:52:10 -07:00
|
|
|
case '>':
|
2009-11-09 13:07:39 -07:00
|
|
|
esc = esc_gt
|
2009-11-03 23:52:10 -07:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2009-04-15 01:05:47 -06:00
|
|
|
}
|
2009-11-03 23:52:10 -07:00
|
|
|
w.Write(s[last:i]);
|
|
|
|
w.Write(esc);
|
2009-11-09 22:23:52 -07:00
|
|
|
last = i + 1;
|
2009-04-15 01:05:47 -06:00
|
|
|
}
|
2009-11-20 12:45:05 -07:00
|
|
|
w.Write(s[last:]);
|
2009-04-15 01:05:47 -06:00
|
|
|
}
|
|
|
|
|
2009-11-08 22:46:20 -07:00
|
|
|
// HTMLFormatter formats arbitrary values for HTML
|
|
|
|
func HTMLFormatter(w io.Writer, value interface{}, format string) {
|
2009-06-29 16:24:23 -06:00
|
|
|
var b bytes.Buffer;
|
2009-04-15 01:05:47 -06:00
|
|
|
fmt.Fprint(&b, value);
|
2009-11-08 22:46:20 -07:00
|
|
|
HTMLEscape(w, b.Bytes());
|
2009-04-15 01:05:47 -06:00
|
|
|
}
|