| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Portions Copyright The Go Authors. |
| 4 |
|
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package tplimplinit |
| 17 |
|
| 18 |
import ( |
| 19 |
// Init the template funcs namespaces |
| 20 |
"context" |
| 21 |
"html/template" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/deps" |
| 24 |
_ "github.com/gohugoio/hugo/tpl/cast" |
| 25 |
_ "github.com/gohugoio/hugo/tpl/collections" |
| 26 |
_ "github.com/gohugoio/hugo/tpl/compare" |
| 27 |
_ "github.com/gohugoio/hugo/tpl/crypto" |
| 28 |
_ "github.com/gohugoio/hugo/tpl/css" |
| 29 |
_ "github.com/gohugoio/hugo/tpl/debug" |
| 30 |
_ "github.com/gohugoio/hugo/tpl/diagrams" |
| 31 |
_ "github.com/gohugoio/hugo/tpl/encoding" |
| 32 |
_ "github.com/gohugoio/hugo/tpl/fmt" |
| 33 |
_ "github.com/gohugoio/hugo/tpl/hash" |
| 34 |
_ "github.com/gohugoio/hugo/tpl/hugo" |
| 35 |
_ "github.com/gohugoio/hugo/tpl/images" |
| 36 |
_ "github.com/gohugoio/hugo/tpl/inflect" |
| 37 |
"github.com/gohugoio/hugo/tpl/internal" |
| 38 |
_ "github.com/gohugoio/hugo/tpl/js" |
| 39 |
_ "github.com/gohugoio/hugo/tpl/lang" |
| 40 |
_ "github.com/gohugoio/hugo/tpl/math" |
| 41 |
_ "github.com/gohugoio/hugo/tpl/openapi/openapi3" |
| 42 |
_ "github.com/gohugoio/hugo/tpl/os" |
| 43 |
_ "github.com/gohugoio/hugo/tpl/page" |
| 44 |
_ "github.com/gohugoio/hugo/tpl/partials" |
| 45 |
_ "github.com/gohugoio/hugo/tpl/path" |
| 46 |
_ "github.com/gohugoio/hugo/tpl/reflect" |
| 47 |
_ "github.com/gohugoio/hugo/tpl/resources" |
| 48 |
_ "github.com/gohugoio/hugo/tpl/safe" |
| 49 |
_ "github.com/gohugoio/hugo/tpl/site" |
| 50 |
_ "github.com/gohugoio/hugo/tpl/strings" |
| 51 |
_ "github.com/gohugoio/hugo/tpl/templates" |
| 52 |
_ "github.com/gohugoio/hugo/tpl/testinginternal" |
| 53 |
_ "github.com/gohugoio/hugo/tpl/time" |
| 54 |
_ "github.com/gohugoio/hugo/tpl/transform" |
| 55 |
_ "github.com/gohugoio/hugo/tpl/urls" |
| 56 |
) |
| 57 |
|
| 58 |
// CreateFuncMap creates a template.FuncMap with all of Hugo's template funcs, |
| 59 |
// excluding the Go built-ins. |
| 60 |
func CreateFuncMap(d *deps.Deps) map[string]any { |
| 61 |
funcMap := template.FuncMap{} |
| 62 |
nsMap := make(map[string]any) |
| 63 |
var onCreated []func(namespaces map[string]any) |
| 64 |
|
| 65 |
// Merge the namespace funcs |
| 66 |
for _, nsf := range internal.TemplateFuncsNamespaceRegistry { |
| 67 |
ns := nsf(d) |
| 68 |
if _, exists := funcMap[ns.Name]; exists { |
| 69 |
panic(ns.Name + " is a duplicate template func") |
| 70 |
} |
| 71 |
funcMap[ns.Name] = ns.Context |
| 72 |
contextV, err := ns.Context(context.Background()) |
| 73 |
if err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
nsMap[ns.Name] = contextV |
| 77 |
for _, mm := range ns.MethodMappings { |
| 78 |
for _, alias := range mm.Aliases { |
| 79 |
if _, exists := funcMap[alias]; exists { |
| 80 |
panic(alias + " is a duplicate template func") |
| 81 |
} |
| 82 |
funcMap[alias] = mm.Method |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ns.OnCreated != nil { |
| 87 |
onCreated = append(onCreated, ns.OnCreated) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
for _, f := range onCreated { |
| 92 |
f(nsMap) |
| 93 |
} |
| 94 |
|
| 95 |
return funcMap |
| 96 |
} |