Commit 9192dd8e authored by Rob Pike's avatar Rob Pike

Start list of default formatters for template variables.

The HTML one here is just a stub - should use an HTML library to do the right thing.

R=rsc
DELTA=54  (47 added, 2 deleted, 5 changed)
OCL=27250
CL=27250
parent 44828014
......@@ -120,6 +120,6 @@ strconv.dirinstall: math.dirinstall os.dirinstall utf8.install
sync.dirinstall:
syscall.dirinstall: sync.dirinstall
tabwriter.dirinstall: os.dirinstall io.dirinstall container.dirinstall
template.dirinstall: bufio.install fmt.dirinstall io.dirinstall os.dirinstall reflect.dirinstall strings.install
template.dirinstall: fmt.dirinstall io.dirinstall os.dirinstall reflect.dirinstall strings.install
time.dirinstall: once.install os.dirinstall io.dirinstall
......@@ -32,18 +32,26 @@ coverage: packages
$(AS) $*.s
O1=\
format.$O\
O2=\
template.$O\
template.a: a1
template.a: a1 a2
a1: $(O1)
$(AR) grc template.a template.$O
$(AR) grc template.a format.$O
rm -f $(O1)
a2: $(O2)
$(AR) grc template.a template.$O
rm -f $(O2)
newpkg: clean
$(AR) grc template.a
$(O1): newpkg
$(O2): a1
nuke: clean
rm -f $(GOROOT)/pkg/template.a
......
// 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 (
"fmt";
"reflect";
)
// HtmlFormatter formats arbitrary values for HTML
// TODO: do something for real.
func HtmlFormatter(v reflect.Value) string {
s := fmt.Sprint(reflect.Indirect(v).Interface());
return s;
}
// StringFormatter formats returns the default string representation.
// 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.
func StringFormatter(v reflect.Value) string {
s := fmt.Sprint(reflect.Indirect(v).Interface());
return s;
}
......@@ -4,16 +4,15 @@
// Template library. See http://code.google.com/p/json-template/wiki/Reference
// TODO: document this here as well.
package template
import (
"bufio";
"fmt";
"io";
"os";
"reflect";
"strings";
"template";
)
var ErrLBrace = os.NewError("unexpected opening brace")
......@@ -51,6 +50,13 @@ const (
// names to the functions that implement them.
type FormatterMap map[string] func(reflect.Value) string
// Built-in formatters.
var builtins = FormatterMap {
"html" : HtmlFormatter,
"str" : StringFormatter,
"" : StringFormatter,
}
type template struct {
errorchan chan *os.Error; // for erroring out
linenum *int; // shared by all templates derived from this one
......@@ -439,11 +445,13 @@ func (t *template) evalVariable(name_formatter string) string {
formatter = name_formatter[bar+1:len(name_formatter)];
}
val := t.varValue(name);
// is it in user-supplied map?
if fn, ok := t.fmap[formatter]; ok {
return fn(val)
}
if formatter == "" {
return fmt.Sprint(val.Interface())
// is it in builtin map?
if fn, ok := builtins[formatter]; ok {
return fn(val)
}
t.error(ErrNoFormatter, ": ", formatter);
panic("notreached");
......
......@@ -143,9 +143,11 @@ var tests = []*Test {
&Test{
"{.section pdata }\n"
"{header|uppercase}={integer|+1}\n"
"{header|html}={integer|str}\n"
"{.end}\n",
"HEADER=78\n"
"Header=77\n"
},
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment