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