| 1 |
package tplimpl |
| 2 |
|
| 3 |
import ( |
| 4 |
"iter" |
| 5 |
"regexp" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
"sync/atomic" |
| 9 |
"unicode" |
| 10 |
"unicode/utf8" |
| 11 |
|
| 12 |
"github.com/gohugoio/hugo/common/paths" |
| 13 |
"github.com/gohugoio/hugo/tpl" |
| 14 |
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate" |
| 15 |
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" |
| 16 |
) |
| 17 |
|
| 18 |
func (t *templateNamespace) readTemplateInto(templ *TemplInfo) error { |
| 19 |
b, err := templ.Fi.Meta().ReadAll() |
| 20 |
if err != nil { |
| 21 |
return err |
| 22 |
} |
| 23 |
templ.content = string(b) |
| 24 |
if !templ.noBaseOf { |
| 25 |
templ.noBaseOf = !needsBaseTemplate(templ.content) |
| 26 |
} |
| 27 |
return nil |
| 28 |
} |
| 29 |
|
| 30 |
// The tweet and twitter shortcodes were deprecated in favor of the x shortcode |
| 31 |
// in v0.141.0. We can remove these aliases in v0.155.0 or later. |
| 32 |
var embeddedTemplatesAliases = map[string][]string{ |
| 33 |
"_shortcodes/twitter.html": {"_shortcodes/tweet.html"}, |
| 34 |
} |
| 35 |
|
| 36 |
func (s *TemplateStore) parseTemplate(ti *TemplInfo) error { |
| 37 |
err := s.tns.doParseTemplate(ti) |
| 38 |
if err != nil { |
| 39 |
return s.addFileContext(ti, "parse of template failed", err) |
| 40 |
} |
| 41 |
return err |
| 42 |
} |
| 43 |
|
| 44 |
func (t *templateNamespace) newBlankTemplate(ti *TemplInfo) tpl.Template { |
| 45 |
if ti.D.IsPlainText { |
| 46 |
tt, err := t.parseText.New(ti.Name()).Parse("") |
| 47 |
if err != nil { |
| 48 |
panic(err) |
| 49 |
} |
| 50 |
return tt |
| 51 |
|
| 52 |
} |
| 53 |
tt, err := t.parseHTML.New(ti.Name()).Parse("") |
| 54 |
if err != nil { |
| 55 |
panic(err) |
| 56 |
} |
| 57 |
return tt |
| 58 |
} |
| 59 |
|
| 60 |
func (t *templateNamespace) doParseTemplate(ti *TemplInfo) error { |
| 61 |
if !ti.noBaseOf || ti.category == CategoryBaseof { |
| 62 |
// Delay parsing until we have the base template. |
| 63 |
return nil |
| 64 |
} |
| 65 |
pi := ti.PathInfo |
| 66 |
name := pi.PathNoLeadingSlash() |
| 67 |
|
| 68 |
var ( |
| 69 |
templ tpl.Template |
| 70 |
err error |
| 71 |
) |
| 72 |
|
| 73 |
if ti.D.IsPlainText { |
| 74 |
prototype := t.parseText |
| 75 |
if prototype.Lookup(name) != nil { |
| 76 |
name += "-" + strconv.FormatUint(t.nameCounter.Add(1), 10) |
| 77 |
} |
| 78 |
templ, err = prototype.New(name).Parse(ti.content) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
} else { |
| 83 |
prototype := t.parseHTML |
| 84 |
if prototype.Lookup(name) != nil { |
| 85 |
name += "-" + strconv.FormatUint(t.nameCounter.Add(1), 10) |
| 86 |
} |
| 87 |
templ, err = prototype.New(name).Parse(ti.content) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
|
| 92 |
if ti.subCategory == SubCategoryEmbedded { |
| 93 |
// In Hugo 0.146.0 we moved the internal templates around. |
| 94 |
// For the "_internal/twitter_cards.html" style templates, they |
| 95 |
// were moved to the _partials directory. |
| 96 |
// But we need to make them accessible from the old path for a while. |
| 97 |
if pi.Type() == paths.TypePartial { |
| 98 |
aliasName := strings.TrimPrefix(name, "_partials/") |
| 99 |
aliasName = "_internal/" + aliasName |
| 100 |
_, err = prototype.AddParseTree(aliasName, templ.(*htmltemplate.Template).Tree) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// This was also possible before Hugo 0.146.0, but this should be deprecated. |
| 107 |
if pi.Type() == paths.TypeShortcode { |
| 108 |
aliasName := strings.TrimPrefix(name, "_shortcodes/") |
| 109 |
aliasName = "_internal/shortcodes/" + aliasName |
| 110 |
_, err = prototype.AddParseTree(aliasName, templ.(*htmltemplate.Template).Tree) |
| 111 |
if err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Issue #13599. |
| 118 |
if ti.category == CategoryPartial && ti.Fi != nil && ti.Fi.Meta().PathInfo.Section() == "partials" { |
| 119 |
aliasName := strings.TrimPrefix(name, "_") |
| 120 |
if _, err := prototype.AddParseTree(aliasName, templ.(*htmltemplate.Template).Tree); err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
ti.Template = templ |
| 127 |
|
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
func (t *templateNamespace) applyBaseTemplate(overlay *TemplInfo, base keyTemplateInfo) error { |
| 132 |
tb := &TemplWithBaseApplied{ |
| 133 |
Overlay: overlay, |
| 134 |
Base: base.Info, |
| 135 |
} |
| 136 |
|
| 137 |
base.Info.overlays = append(base.Info.overlays, overlay) |
| 138 |
|
| 139 |
var templ tpl.Template |
| 140 |
if overlay.D.IsPlainText { |
| 141 |
tt := texttemplate.Must(t.parseText.Clone()).New(overlay.PathInfo.PathNoLeadingSlash()) |
| 142 |
var err error |
| 143 |
tt, err = tt.Parse(base.Info.content) |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
tt, err = tt.Parse(overlay.content) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
templ = tt |
| 152 |
t.baseofTextClones = append(t.baseofTextClones, tt) |
| 153 |
} else { |
| 154 |
tt := htmltemplate.Must(t.parseHTML.CloneShallow()).New(overlay.PathInfo.PathNoLeadingSlash()) |
| 155 |
var err error |
| 156 |
tt, err = tt.Parse(base.Info.content) |
| 157 |
if err != nil { |
| 158 |
return err |
| 159 |
} |
| 160 |
tt, err = tt.Parse(overlay.content) |
| 161 |
if err != nil { |
| 162 |
return err |
| 163 |
} |
| 164 |
templ = tt |
| 165 |
|
| 166 |
t.baseofHtmlClones = append(t.baseofHtmlClones, tt) |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
tb.Template = &TemplInfo{ |
| 171 |
Template: templ, |
| 172 |
base: base.Info, |
| 173 |
PathInfo: overlay.PathInfo, |
| 174 |
Fi: overlay.Fi, |
| 175 |
D: overlay.D, |
| 176 |
matrix: overlay.matrix, |
| 177 |
noBaseOf: true, |
| 178 |
} |
| 179 |
|
| 180 |
variants := overlay.baseVariants.Get(base.Key) |
| 181 |
if variants == nil { |
| 182 |
variants = make(map[TemplateDescriptor]*TemplWithBaseApplied) |
| 183 |
overlay.baseVariants.Insert(base.Key, variants) |
| 184 |
} |
| 185 |
variants[base.Info.D] = tb |
| 186 |
return nil |
| 187 |
} |
| 188 |
|
| 189 |
func (t *templateNamespace) templatesIn(in tpl.Template) iter.Seq[tpl.Template] { |
| 190 |
return func(yield func(t tpl.Template) bool) { |
| 191 |
switch in := in.(type) { |
| 192 |
case *htmltemplate.Template: |
| 193 |
for t := range in.All() { |
| 194 |
if !yield(t) { |
| 195 |
return |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
case *texttemplate.Template: |
| 200 |
for t := range in.All() { |
| 201 |
if !yield(t) { |
| 202 |
return |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
var baseTemplateDefineRe = regexp.MustCompile(`^{{-?\s*define`) |
| 210 |
|
| 211 |
// needsBaseTemplate returns true if the first non-comment template block is a |
| 212 |
// define block. |
| 213 |
func needsBaseTemplate(templ string) bool { |
| 214 |
idx := -1 |
| 215 |
inComment := false |
| 216 |
for i := 0; i < len(templ); { |
| 217 |
if !inComment && strings.HasPrefix(templ[i:], "{{/*") { |
| 218 |
inComment = true |
| 219 |
i += 4 |
| 220 |
} else if !inComment && strings.HasPrefix(templ[i:], "{{- /*") { |
| 221 |
inComment = true |
| 222 |
i += 6 |
| 223 |
} else if inComment && strings.HasPrefix(templ[i:], "*/}}") { |
| 224 |
inComment = false |
| 225 |
i += 4 |
| 226 |
} else if inComment && strings.HasPrefix(templ[i:], "*/ -}}") { |
| 227 |
inComment = false |
| 228 |
i += 6 |
| 229 |
} else { |
| 230 |
r, size := utf8.DecodeRuneInString(templ[i:]) |
| 231 |
if !inComment { |
| 232 |
if strings.HasPrefix(templ[i:], "{{") { |
| 233 |
idx = i |
| 234 |
break |
| 235 |
} else if !unicode.IsSpace(r) { |
| 236 |
break |
| 237 |
} |
| 238 |
} |
| 239 |
i += size |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if idx == -1 { |
| 244 |
return false |
| 245 |
} |
| 246 |
|
| 247 |
return baseTemplateDefineRe.MatchString(templ[idx:]) |
| 248 |
} |
| 249 |
|
| 250 |
type templateNamespace struct { |
| 251 |
parseText *texttemplate.Template |
| 252 |
parseHTML *htmltemplate.Template |
| 253 |
prototypeText *texttemplate.Template |
| 254 |
prototypeHTML *htmltemplate.Template |
| 255 |
|
| 256 |
nameCounter atomic.Uint64 |
| 257 |
|
| 258 |
standaloneText *texttemplate.Template |
| 259 |
|
| 260 |
baseofTextClones []*texttemplate.Template |
| 261 |
baseofHtmlClones []*htmltemplate.Template |
| 262 |
} |
| 263 |
|
| 264 |
func (t *templateNamespace) createPrototypesParse() error { |
| 265 |
if t.prototypeHTML == nil { |
| 266 |
panic("prototypeHTML not set") |
| 267 |
} |
| 268 |
t.parseHTML = htmltemplate.Must(t.prototypeHTML.Clone()) |
| 269 |
t.parseText = texttemplate.Must(t.prototypeText.Clone()) |
| 270 |
return nil |
| 271 |
} |
| 272 |
|
| 273 |
func (t *templateNamespace) createPrototypes(init bool) error { |
| 274 |
if init { |
| 275 |
t.prototypeHTML = htmltemplate.Must(t.parseHTML.Clone()) |
| 276 |
t.prototypeText = texttemplate.Must(t.parseText.Clone()) |
| 277 |
} |
| 278 |
|
| 279 |
return nil |
| 280 |
} |
| 281 |
|
| 282 |
func newTemplateNamespace(funcs map[string]any) *templateNamespace { |
| 283 |
return &templateNamespace{ |
| 284 |
parseHTML: htmltemplate.New("").Funcs(funcs), |
| 285 |
parseText: texttemplate.New("").Funcs(funcs), |
| 286 |
standaloneText: texttemplate.New("").Funcs(funcs), |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func isText(t tpl.Template) bool { |
| 291 |
switch t.(type) { |
| 292 |
case *texttemplate.Template: |
| 293 |
return true |
| 294 |
case *htmltemplate.Template: |
| 295 |
return false |
| 296 |
default: |
| 297 |
panic("unknown template type") |
| 298 |
} |
| 299 |
} |