github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/texttemplate/template.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package template 6 7 import ( 8 "reflect" 9 "sync" 10 11 "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" 12 ) 13 14 // common holds the information shared by related templates. 15 type common struct { 16 muTmpl sync.RWMutex // protects tmpl (temporary Hugo-fix) 17 tmpl map[string]*Template // Map from name to defined templates. 18 option option 19 // We use two maps, one for parsing and one for execution. 20 // This separation makes the API cleaner since it doesn't 21 // expose reflection to the client. 22 muFuncs sync.RWMutex // protects parseFuncs and execFuncs 23 parseFuncs FuncMap 24 execFuncs map[string]reflect.Value 25 } 26 27 // Template is the representation of a parsed template. The *parse.Tree 28 // field is exported only for use by html/template and should be treated 29 // as unexported by all other clients. 30 type Template struct { 31 name string 32 *parse.Tree 33 *common 34 leftDelim string 35 rightDelim string 36 } 37 38 // New allocates a new, undefined template with the given name. 39 func New(name string) *Template { 40 t := &Template{ 41 name: name, 42 } 43 t.init() 44 return t 45 } 46 47 // Name returns the name of the template. 48 func (t *Template) Name() string { 49 return t.name 50 } 51 52 // New allocates a new, undefined template associated with the given one and with the same 53 // delimiters. The association, which is transitive, allows one template to 54 // invoke another with a {{template}} action. 55 // 56 // Because associated templates share underlying data, template construction 57 // cannot be done safely in parallel. Once the templates are constructed, they 58 // can be executed in parallel. 59 func (t *Template) New(name string) *Template { 60 t.init() 61 nt := &Template{ 62 name: name, 63 common: t.common, 64 leftDelim: t.leftDelim, 65 rightDelim: t.rightDelim, 66 } 67 return nt 68 } 69 70 // init guarantees that t has a valid common structure. 71 func (t *Template) init() { 72 if t.common == nil { 73 c := new(common) 74 c.tmpl = make(map[string]*Template) 75 c.parseFuncs = make(FuncMap) 76 c.execFuncs = make(map[string]reflect.Value) 77 t.common = c 78 } 79 } 80 81 // Clone returns a duplicate of the template, including all associated 82 // templates. The actual representation is not copied, but the name space of 83 // associated templates is, so further calls to Parse in the copy will add 84 // templates to the copy but not to the original. Clone can be used to prepare 85 // common templates and use them with variant definitions for other templates 86 // by adding the variants after the clone is made. 87 func (t *Template) Clone() (*Template, error) { 88 nt := t.copy(nil) 89 nt.init() 90 if t.common == nil { 91 return nt, nil 92 } 93 // temporary Hugo-fix 94 t.muTmpl.RLock() 95 defer t.muTmpl.RUnlock() 96 for k, v := range t.tmpl { 97 if k == t.name { 98 nt.tmpl[t.name] = nt 99 continue 100 } 101 // The associated templates share nt's common structure. 102 tmpl := v.copy(nt.common) 103 nt.tmpl[k] = tmpl 104 } 105 t.muFuncs.RLock() 106 defer t.muFuncs.RUnlock() 107 for k, v := range t.parseFuncs { 108 nt.parseFuncs[k] = v 109 } 110 for k, v := range t.execFuncs { 111 nt.execFuncs[k] = v 112 } 113 return nt, nil 114 } 115 116 // copy returns a shallow copy of t, with common set to the argument. 117 func (t *Template) copy(c *common) *Template { 118 return &Template{ 119 name: t.name, 120 Tree: t.Tree, 121 common: c, 122 leftDelim: t.leftDelim, 123 rightDelim: t.rightDelim, 124 } 125 } 126 127 // AddParseTree associates the argument parse tree with the template t, giving 128 // it the specified name. If the template has not been defined, this tree becomes 129 // its definition. If it has been defined and already has that name, the existing 130 // definition is replaced; otherwise a new template is created, defined, and returned. 131 func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) { 132 // temporary Hugo-fix 133 t.muTmpl.Lock() 134 defer t.muTmpl.Unlock() 135 t.init() 136 nt := t 137 if name != t.name { 138 nt = t.New(name) 139 } 140 // Even if nt == t, we need to install it in the common.tmpl map. 141 if t.associate(nt, tree) || nt.Tree == nil { 142 nt.Tree = tree 143 } 144 return nt, nil 145 } 146 147 // Templates returns a slice of defined templates associated with t. 148 func (t *Template) Templates() []*Template { 149 if t.common == nil { 150 return nil 151 } 152 // Return a slice so we don't expose the map. 153 // temporary Hugo-fix 154 t.muTmpl.RLock() 155 defer t.muTmpl.RUnlock() 156 m := make([]*Template, 0, len(t.tmpl)) 157 for _, v := range t.tmpl { 158 m = append(m, v) 159 } 160 return m 161 } 162 163 // Delims sets the action delimiters to the specified strings, to be used in 164 // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template 165 // definitions will inherit the settings. An empty delimiter stands for the 166 // corresponding default: {{ or }}. 167 // The return value is the template, so calls can be chained. 168 func (t *Template) Delims(left, right string) *Template { 169 t.init() 170 t.leftDelim = left 171 t.rightDelim = right 172 return t 173 } 174 175 // Funcs adds the elements of the argument map to the template's function map. 176 // It must be called before the template is parsed. 177 // It panics if a value in the map is not a function with appropriate return 178 // type or if the name cannot be used syntactically as a function in a template. 179 // It is legal to overwrite elements of the map. The return value is the template, 180 // so calls can be chained. 181 func (t *Template) Funcs(funcMap FuncMap) *Template { 182 t.init() 183 t.muFuncs.Lock() 184 defer t.muFuncs.Unlock() 185 addValueFuncs(t.execFuncs, funcMap) 186 addFuncs(t.parseFuncs, funcMap) 187 return t 188 } 189 190 // Lookup returns the template with the given name that is associated with t. 191 // It returns nil if there is no such template or the template has no definition. 192 func (t *Template) Lookup(name string) *Template { 193 if t.common == nil { 194 return nil 195 } 196 // temporary Hugo-fix 197 t.muTmpl.RLock() 198 defer t.muTmpl.RUnlock() 199 return t.tmpl[name] 200 } 201 202 // Parse parses text as a template body for t. 203 // Named template definitions ({{define ...}} or {{block ...}} statements) in text 204 // define additional templates associated with t and are removed from the 205 // definition of t itself. 206 // 207 // Templates can be redefined in successive calls to Parse. 208 // A template definition with a body containing only white space and comments 209 // is considered empty and will not replace an existing template's body. 210 // This allows using Parse to add new named template definitions without 211 // overwriting the main template body. 212 func (t *Template) Parse(text string) (*Template, error) { 213 t.init() 214 t.muFuncs.RLock() 215 trees, err := parse.Parse(t.name, text, t.leftDelim, t.rightDelim, t.parseFuncs, builtins()) 216 t.muFuncs.RUnlock() 217 if err != nil { 218 return nil, err 219 } 220 // Add the newly parsed trees, including the one for t, into our common structure. 221 for name, tree := range trees { 222 if _, err := t.AddParseTree(name, tree); err != nil { 223 return nil, err 224 } 225 } 226 return t, nil 227 } 228 229 // associate installs the new template into the group of templates associated 230 // with t. The two are already known to share the common structure. 231 // The boolean return value reports whether to store this tree as t.Tree. 232 func (t *Template) associate(new *Template, tree *parse.Tree) bool { 233 if new.common != t.common { 234 panic("internal error: associate not common") 235 } 236 if old := t.tmpl[new.name]; old != nil && parse.IsEmptyTree(tree.Root) && old.Tree != nil { 237 // If a template by that name exists, 238 // don't replace it with an empty template. 239 return false 240 } 241 t.tmpl[new.name] = new 242 return true 243 }