Reimplement and simplify Hugo's template system

See #13541 for details.

Fixes #13545
Fixes #13515
Closes #7964
Closes #13365
Closes #12988
Closes #4891
This commit is contained in:
Bjørn Erik Pedersen
2025-04-06 19:55:35 +02:00
parent 812ea0b325
commit 83cfdd78ca
138 changed files with 5342 additions and 4396 deletions

View File

@@ -24,7 +24,7 @@ const name = "math"
func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New()
ctx := New(d)
ns := &internal.TemplateFuncsNamespace{
Name: name,

View File

@@ -20,9 +20,9 @@ import (
"math"
"math/rand"
"reflect"
"sync/atomic"
_math "github.com/gohugoio/hugo/common/math"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/cast"
)
@@ -32,12 +32,16 @@ var (
)
// New returns a new instance of the math-namespaced template functions.
func New() *Namespace {
return &Namespace{}
func New(d *deps.Deps) *Namespace {
return &Namespace{
d: d,
}
}
// Namespace provides template functions for the "math" namespace.
type Namespace struct{}
type Namespace struct {
d *deps.Deps
}
// Abs returns the absolute value of n.
func (ns *Namespace) Abs(n any) (float64, error) {
@@ -345,8 +349,6 @@ func (ns *Namespace) doArithmetic(inputs []any, operation rune) (value any, err
return
}
var counter uint64
// Counter increments and returns a global counter.
// This was originally added to be used in tests where now.UnixNano did not
// have the needed precision (especially on Windows).
@@ -354,5 +356,5 @@ var counter uint64
// and the counter will reset on new builds.
// <docsmeta>{"identifiers": ["now.UnixNano"] }</docsmeta>
func (ns *Namespace) Counter() uint64 {
return atomic.AddUint64(&counter, uint64(1))
return ns.d.Counters.MathCounter.Add(1)
}

View File

@@ -24,7 +24,7 @@ func TestBasicNSArithmetic(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
type TestCase struct {
fn func(inputs ...any) (any, error)
@@ -66,7 +66,7 @@ func TestBasicNSArithmetic(t *testing.T) {
func TestAbs(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -93,7 +93,7 @@ func TestAbs(t *testing.T) {
func TestCeil(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -126,7 +126,7 @@ func TestFloor(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -159,7 +159,7 @@ func TestLog(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -200,7 +200,7 @@ func TestSqrt(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -239,7 +239,7 @@ func TestMod(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -279,7 +279,7 @@ func TestModBool(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -325,7 +325,7 @@ func TestRound(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -358,7 +358,7 @@ func TestPow(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -398,7 +398,7 @@ func TestMax(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
type TestCase struct {
values []any
@@ -452,7 +452,7 @@ func TestMin(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
type TestCase struct {
values []any
@@ -507,7 +507,7 @@ func TestSum(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
mustSum := func(values ...any) any {
result, err := ns.Sum(values...)
@@ -530,7 +530,7 @@ func TestProduct(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
mustProduct := func(values ...any) any {
result, err := ns.Product(values...)
@@ -554,7 +554,7 @@ func TestPi(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
expect := 3.1415
result := ns.Pi()
@@ -570,7 +570,7 @@ func TestSin(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -604,7 +604,7 @@ func TestCos(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -638,7 +638,7 @@ func TestTan(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
a any
@@ -680,7 +680,7 @@ func TestTan(t *testing.T) {
func TestAsin(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -715,7 +715,7 @@ func TestAsin(t *testing.T) {
func TestAcos(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -751,7 +751,7 @@ func TestAcos(t *testing.T) {
func TestAtan(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -782,7 +782,7 @@ func TestAtan(t *testing.T) {
func TestAtan2(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -821,7 +821,7 @@ func TestAtan2(t *testing.T) {
func TestToDegrees(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any
@@ -852,7 +852,7 @@ func TestToDegrees(t *testing.T) {
func TestToRadians(t *testing.T) {
t.Parallel()
c := qt.New(t)
ns := New()
ns := New(nil)
for _, test := range []struct {
x any