Retro68/gcc/libgo/go/text/template/template.go

219 lines
6.4 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2011 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.
package template
import (
"reflect"
2017-04-10 11:32:00 +00:00
"sync"
2012-03-27 23:13:14 +00:00
"text/template/parse"
)
// common holds the information shared by related templates.
type common struct {
2017-04-10 11:32:00 +00:00
tmpl map[string]*Template // Map from name to defined templates.
option option
2012-03-27 23:13:14 +00:00
// We use two maps, one for parsing and one for execution.
// This separation makes the API cleaner since it doesn't
// expose reflection to the client.
2017-04-10 11:32:00 +00:00
muFuncs sync.RWMutex // protects parseFuncs and execFuncs
2012-03-27 23:13:14 +00:00
parseFuncs FuncMap
execFuncs map[string]reflect.Value
}
// Template is the representation of a parsed template. The *parse.Tree
// field is exported only for use by html/template and should be treated
// as unexported by all other clients.
type Template struct {
name string
*parse.Tree
*common
leftDelim string
rightDelim string
}
2017-04-10 11:32:00 +00:00
// New allocates a new, undefined template with the given name.
2012-03-27 23:13:14 +00:00
func New(name string) *Template {
2017-04-10 11:32:00 +00:00
t := &Template{
2012-03-27 23:13:14 +00:00
name: name,
}
2017-04-10 11:32:00 +00:00
t.init()
return t
2012-03-27 23:13:14 +00:00
}
// Name returns the name of the template.
func (t *Template) Name() string {
return t.name
}
2017-04-10 11:32:00 +00:00
// New allocates a new, undefined template associated with the given one and with the same
2012-03-27 23:13:14 +00:00
// delimiters. The association, which is transitive, allows one template to
// invoke another with a {{template}} action.
func (t *Template) New(name string) *Template {
t.init()
2017-04-10 11:32:00 +00:00
nt := &Template{
2012-03-27 23:13:14 +00:00
name: name,
common: t.common,
leftDelim: t.leftDelim,
rightDelim: t.rightDelim,
}
2017-04-10 11:32:00 +00:00
return nt
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
// init guarantees that t has a valid common structure.
2012-03-27 23:13:14 +00:00
func (t *Template) init() {
if t.common == nil {
2017-04-10 11:32:00 +00:00
c := new(common)
c.tmpl = make(map[string]*Template)
c.parseFuncs = make(FuncMap)
c.execFuncs = make(map[string]reflect.Value)
t.common = c
2012-03-27 23:13:14 +00:00
}
}
// Clone returns a duplicate of the template, including all associated
// templates. The actual representation is not copied, but the name space of
// associated templates is, so further calls to Parse in the copy will add
// templates to the copy but not to the original. Clone can be used to prepare
// common templates and use them with variant definitions for other templates
// by adding the variants after the clone is made.
func (t *Template) Clone() (*Template, error) {
nt := t.copy(nil)
nt.init()
2017-04-10 11:32:00 +00:00
if t.common == nil {
return nt, nil
}
2012-03-27 23:13:14 +00:00
for k, v := range t.tmpl {
2017-04-10 11:32:00 +00:00
if k == t.name {
nt.tmpl[t.name] = nt
2012-03-27 23:13:14 +00:00
continue
}
// The associated templates share nt's common structure.
tmpl := v.copy(nt.common)
nt.tmpl[k] = tmpl
}
2017-04-10 11:32:00 +00:00
t.muFuncs.RLock()
defer t.muFuncs.RUnlock()
2012-03-27 23:13:14 +00:00
for k, v := range t.parseFuncs {
nt.parseFuncs[k] = v
}
for k, v := range t.execFuncs {
nt.execFuncs[k] = v
}
return nt, nil
}
// copy returns a shallow copy of t, with common set to the argument.
func (t *Template) copy(c *common) *Template {
nt := New(t.name)
nt.Tree = t.Tree
nt.common = c
nt.leftDelim = t.leftDelim
nt.rightDelim = t.rightDelim
return nt
}
2017-04-10 11:32:00 +00:00
// AddParseTree adds parse tree for template with given name and associates it with t.
// If the template does not already exist, it will create a new one.
// If the template does exist, it will be replaced.
2012-03-27 23:13:14 +00:00
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
2017-04-10 11:32:00 +00:00
t.init()
// If the name is the name of this template, overwrite this template.
nt := t
if name != t.name {
nt = t.New(name)
}
// Even if nt == t, we need to install it in the common.tmpl map.
if replace, err := t.associate(nt, tree); err != nil {
return nil, err
} else if replace {
nt.Tree = tree
2012-03-27 23:13:14 +00:00
}
return nt, nil
}
2017-04-10 11:32:00 +00:00
// Templates returns a slice of defined templates associated with t.
2012-03-27 23:13:14 +00:00
func (t *Template) Templates() []*Template {
2014-09-21 17:33:12 +00:00
if t.common == nil {
return nil
}
2012-03-27 23:13:14 +00:00
// Return a slice so we don't expose the map.
m := make([]*Template, 0, len(t.tmpl))
for _, v := range t.tmpl {
m = append(m, v)
}
return m
}
// Delims sets the action delimiters to the specified strings, to be used in
// subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
// definitions will inherit the settings. An empty delimiter stands for the
// corresponding default: {{ or }}.
// The return value is the template, so calls can be chained.
func (t *Template) Delims(left, right string) *Template {
2017-04-10 11:32:00 +00:00
t.init()
2012-03-27 23:13:14 +00:00
t.leftDelim = left
t.rightDelim = right
return t
}
// Funcs adds the elements of the argument map to the template's function map.
// It panics if a value in the map is not a function with appropriate return
2017-04-10 11:32:00 +00:00
// type or if the name cannot be used syntactically as a function in a template.
// It is legal to overwrite elements of the map. The return value is the template,
// so calls can be chained.
2012-03-27 23:13:14 +00:00
func (t *Template) Funcs(funcMap FuncMap) *Template {
t.init()
2017-04-10 11:32:00 +00:00
t.muFuncs.Lock()
defer t.muFuncs.Unlock()
2012-03-27 23:13:14 +00:00
addValueFuncs(t.execFuncs, funcMap)
addFuncs(t.parseFuncs, funcMap)
return t
}
2017-04-10 11:32:00 +00:00
// Lookup returns the template with the given name that is associated with t.
// It returns nil if there is no such template or the template has no definition.
2012-03-27 23:13:14 +00:00
func (t *Template) Lookup(name string) *Template {
if t.common == nil {
return nil
}
return t.tmpl[name]
}
2017-04-10 11:32:00 +00:00
// Parse defines the template by parsing the text. Nested template definitions will be
2012-03-27 23:13:14 +00:00
// associated with the top-level template t. Parse may be called multiple times
2017-04-10 11:32:00 +00:00
// to parse definitions of templates to associate with t.
2012-03-27 23:13:14 +00:00
func (t *Template) Parse(text string) (*Template, error) {
t.init()
2017-04-10 11:32:00 +00:00
t.muFuncs.RLock()
2012-03-27 23:13:14 +00:00
trees, err := parse.Parse(t.name, text, t.leftDelim, t.rightDelim, t.parseFuncs, builtins)
2017-04-10 11:32:00 +00:00
t.muFuncs.RUnlock()
2012-03-27 23:13:14 +00:00
if err != nil {
return nil, err
}
// Add the newly parsed trees, including the one for t, into our common structure.
for name, tree := range trees {
2017-04-10 11:32:00 +00:00
if _, err := t.AddParseTree(name, tree); err != nil {
2012-03-27 23:13:14 +00:00
return nil, err
}
}
return t, nil
}
// associate installs the new template into the group of templates associated
2017-04-10 11:32:00 +00:00
// with t. The two are already known to share the common structure.
// The boolean return value reports whether to store this tree as t.Tree.
2012-03-27 23:13:14 +00:00
func (t *Template) associate(new *Template, tree *parse.Tree) (bool, error) {
if new.common != t.common {
panic("internal error: associate not common")
}
2017-04-10 11:32:00 +00:00
if t.tmpl[new.name] != nil && parse.IsEmptyTree(tree.Root) {
// If a template by that name exists,
// don't replace it with an empty template.
return false, nil
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
t.tmpl[new.name] = new
2012-03-27 23:13:14 +00:00
return true, nil
}