| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package tpl contains template functions and related types. |
| 15 |
package tpl |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"slices" |
| 20 |
"strings" |
| 21 |
"sync" |
| 22 |
"unicode" |
| 23 |
|
| 24 |
"github.com/bep/helpers/contexthelpers" |
| 25 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 26 |
"github.com/gohugoio/hugo/common/collections" |
| 27 |
|
| 28 |
"github.com/gohugoio/hugo/identity" |
| 29 |
"github.com/gohugoio/hugo/langs" |
| 30 |
|
| 31 |
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate" |
| 32 |
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" |
| 33 |
) |
| 34 |
|
| 35 |
// Template is the common interface between text/template and html/template. |
| 36 |
type Template interface { |
| 37 |
Name() string |
| 38 |
Prepare() (*texttemplate.Template, error) |
| 39 |
} |
| 40 |
|
| 41 |
// RenderingContext represents the currently rendered site/language. |
| 42 |
type RenderingContext struct { |
| 43 |
Site site |
| 44 |
SiteOutIdx int |
| 45 |
} |
| 46 |
|
| 47 |
type ( |
| 48 |
contextKey uint8 |
| 49 |
) |
| 50 |
|
| 51 |
const ( |
| 52 |
contextKeyDependencyManagerScopedProvider contextKey = iota |
| 53 |
contextKeyDependencyScope |
| 54 |
contextKeyPage |
| 55 |
contextKeyIsInGoldmark |
| 56 |
cntextKeyCurrentTemplateInfo |
| 57 |
contextKeyPartialDecoratorIDStack |
| 58 |
contextKeyIsInPartialCached |
| 59 |
) |
| 60 |
|
| 61 |
// Context manages values passed in the context to templates. |
| 62 |
var Context = struct { |
| 63 |
DependencyManagerScopedProvider contexthelpers.ContextDispatcher[identity.DependencyManagerScopedProvider] |
| 64 |
GetDependencyManagerInCurrentScope func(context.Context) identity.Manager |
| 65 |
DependencyScope contexthelpers.ContextDispatcher[int] |
| 66 |
Page contexthelpers.ContextDispatcher[page] |
| 67 |
IsInGoldmark contexthelpers.ContextDispatcher[bool] |
| 68 |
CurrentTemplate contexthelpers.ContextDispatcher[*CurrentTemplateInfo] |
| 69 |
PartialDecoratorIDStack contexthelpers.ContextDispatcher[*collections.Stack[*StringBool]] |
| 70 |
IsInPartialCached contexthelpers.ContextDispatcher[bool] |
| 71 |
}{ |
| 72 |
DependencyManagerScopedProvider: contexthelpers.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKeyDependencyManagerScopedProvider), |
| 73 |
DependencyScope: contexthelpers.NewContextDispatcher[int](contextKeyDependencyScope), |
| 74 |
Page: contexthelpers.NewContextDispatcher[page](contextKeyPage), |
| 75 |
IsInGoldmark: contexthelpers.NewContextDispatcher[bool](contextKeyIsInGoldmark), |
| 76 |
CurrentTemplate: contexthelpers.NewContextDispatcher[*CurrentTemplateInfo](cntextKeyCurrentTemplateInfo), |
| 77 |
PartialDecoratorIDStack: contexthelpers.NewContextDispatcher[*collections.Stack[*StringBool]](contextKeyPartialDecoratorIDStack), |
| 78 |
IsInPartialCached: contexthelpers.NewContextDispatcher[bool](contextKeyIsInPartialCached), |
| 79 |
} |
| 80 |
|
| 81 |
func init() { |
| 82 |
Context.GetDependencyManagerInCurrentScope = func(ctx context.Context) identity.Manager { |
| 83 |
idmsp := Context.DependencyManagerScopedProvider.Get(ctx) |
| 84 |
if idmsp != nil { |
| 85 |
return idmsp.GetDependencyManagerForScope(Context.DependencyScope.Get(ctx)) |
| 86 |
} |
| 87 |
return nil |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// StringBool is a helper struct to hold a string and a bool value. |
| 92 |
type StringBool struct { |
| 93 |
Str string |
| 94 |
Bool bool |
| 95 |
} |
| 96 |
|
| 97 |
type page interface { |
| 98 |
IsBranch() bool |
| 99 |
} |
| 100 |
|
| 101 |
type site interface { |
| 102 |
Language() *langs.Language |
| 103 |
} |
| 104 |
|
| 105 |
const ( |
| 106 |
// HugoDeferredTemplatePrefix is the prefix for placeholders for deferred templates. |
| 107 |
HugoDeferredTemplatePrefix = "__hdeferred/" |
| 108 |
// HugoDeferredTemplateSuffix is the suffix for placeholders for deferred templates. |
| 109 |
HugoDeferredTemplateSuffix = "__d=" |
| 110 |
) |
| 111 |
|
| 112 |
const hugoNewLinePlaceholder = "___hugonl_" |
| 113 |
|
| 114 |
var stripHTMLReplacerPre = strings.NewReplacer("\n", " ", "</p>", hugoNewLinePlaceholder, "<br>", hugoNewLinePlaceholder, "<br />", hugoNewLinePlaceholder) |
| 115 |
|
| 116 |
// StripHTML strips out all HTML tags in s. |
| 117 |
func StripHTML(s string) string { |
| 118 |
// Shortcut strings with no tags in them |
| 119 |
if !strings.ContainsAny(s, "<>") { |
| 120 |
return s |
| 121 |
} |
| 122 |
|
| 123 |
pre := stripHTMLReplacerPre.Replace(s) |
| 124 |
preReplaced := pre != s |
| 125 |
|
| 126 |
s = htmltemplate.StripTags(pre) |
| 127 |
|
| 128 |
if preReplaced { |
| 129 |
s = strings.ReplaceAll(s, hugoNewLinePlaceholder, "\n") |
| 130 |
} |
| 131 |
|
| 132 |
var wasSpace bool |
| 133 |
b := bp.GetBuffer() |
| 134 |
defer bp.PutBuffer(b) |
| 135 |
for _, r := range s { |
| 136 |
isSpace := unicode.IsSpace(r) |
| 137 |
if !(isSpace && wasSpace) { |
| 138 |
b.WriteRune(r) |
| 139 |
} |
| 140 |
wasSpace = isSpace |
| 141 |
} |
| 142 |
|
| 143 |
if b.Len() > 0 { |
| 144 |
s = b.String() |
| 145 |
} |
| 146 |
|
| 147 |
return s |
| 148 |
} |
| 149 |
|
| 150 |
// DeferredExecution holds the template and data for a deferred execution. |
| 151 |
type DeferredExecution struct { |
| 152 |
Mu sync.Mutex |
| 153 |
Ctx context.Context |
| 154 |
TemplatePath string |
| 155 |
Data any |
| 156 |
|
| 157 |
Executed bool |
| 158 |
Result string |
| 159 |
} |
| 160 |
|
| 161 |
type CurrentTemplateInfoOps interface { |
| 162 |
CurrentTemplateInfoCommonOps |
| 163 |
Base() CurrentTemplateInfoCommonOps |
| 164 |
} |
| 165 |
|
| 166 |
type CurrentTemplateInfoCommonOps interface { |
| 167 |
// Template name. |
| 168 |
Name() string |
| 169 |
// Template source filename. |
| 170 |
// Will be empty for internal templates. |
| 171 |
Filename() string |
| 172 |
} |
| 173 |
|
| 174 |
// CurrentTemplateInfo as returned in templates.Current. |
| 175 |
type CurrentTemplateInfo struct { |
| 176 |
Parent *CurrentTemplateInfo |
| 177 |
Level int |
| 178 |
Key string |
| 179 |
CurrentTemplateInfoOps |
| 180 |
} |
| 181 |
|
| 182 |
// CurrentTemplateInfos is a slice of CurrentTemplateInfo. |
| 183 |
type CurrentTemplateInfos []*CurrentTemplateInfo |
| 184 |
|
| 185 |
// Reverse creates a copy of the slice and reverses it. |
| 186 |
func (c CurrentTemplateInfos) Reverse() CurrentTemplateInfos { |
| 187 |
if len(c) == 0 { |
| 188 |
return c |
| 189 |
} |
| 190 |
r := make(CurrentTemplateInfos, len(c)) |
| 191 |
copy(r, c) |
| 192 |
slices.Reverse(r) |
| 193 |
return r |
| 194 |
} |
| 195 |
|
| 196 |
// Ancestors returns the ancestors of the current template. |
| 197 |
func (ti *CurrentTemplateInfo) Ancestors() CurrentTemplateInfos { |
| 198 |
var ancestors []*CurrentTemplateInfo |
| 199 |
for ti.Parent != nil { |
| 200 |
ti = ti.Parent |
| 201 |
ancestors = append(ancestors, ti) |
| 202 |
} |
| 203 |
return ancestors |
| 204 |
} |