Skip to content

Commit 00ee9ad

Browse files
committed
Reimplement and simplify Hugo's template system
Fixes #13515 Closes #7964 Closes #13365 Updates #12988 Closes #4891
1 parent c15ebce commit 00ee9ad

File tree

133 files changed

+5179
-4363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+5179
-4363
lines changed

commands/server.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"errors"
2424
"fmt"
2525
"io"
26+
"maps"
2627
"net"
2728
"net/http"
2829
_ "net/http/pprof"
@@ -48,6 +49,7 @@ import (
4849
"github.com/fsnotify/fsnotify"
4950
"github.com/gohugoio/hugo/common/herrors"
5051
"github.com/gohugoio/hugo/common/hugo"
52+
"github.com/gohugoio/hugo/tpl/tplimpl"
5153

5254
"github.com/gohugoio/hugo/common/types"
5355
"github.com/gohugoio/hugo/common/urls"
@@ -57,15 +59,13 @@ import (
5759
"github.com/gohugoio/hugo/hugolib"
5860
"github.com/gohugoio/hugo/hugolib/filesystems"
5961
"github.com/gohugoio/hugo/livereload"
60-
"github.com/gohugoio/hugo/tpl"
6162
"github.com/gohugoio/hugo/transform"
6263
"github.com/gohugoio/hugo/transform/livereloadinject"
6364
"github.com/spf13/afero"
6465
"github.com/spf13/cobra"
6566
"github.com/spf13/fsync"
6667
"golang.org/x/sync/errgroup"
6768
"golang.org/x/sync/semaphore"
68-
"maps"
6969
)
7070

7171
var (
@@ -897,16 +897,16 @@ func (c *serverCommand) serve() error {
897897
// To allow the en user to change the error template while the server is running, we use
898898
// the freshest template we can provide.
899899
var (
900-
errTempl tpl.Template
901-
templHandler tpl.TemplateHandler
900+
errTempl *tplimpl.TemplInfo
901+
templHandler *tplimpl.TemplateStore
902902
)
903-
getErrorTemplateAndHandler := func(h *hugolib.HugoSites) (tpl.Template, tpl.TemplateHandler) {
903+
getErrorTemplateAndHandler := func(h *hugolib.HugoSites) (*tplimpl.TemplInfo, *tplimpl.TemplateStore) {
904904
if h == nil {
905905
return errTempl, templHandler
906906
}
907-
templHandler := h.Tmpl()
908-
errTempl, found := templHandler.Lookup("_server/error.html")
909-
if !found {
907+
templHandler := h.GetTemplateStore()
908+
errTempl := templHandler.LookupByPath("/_server/error.html")
909+
if errTempl == nil {
910910
panic("template server/error.html not found")
911911
}
912912
return errTempl, templHandler

common/constants/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
WarnFrontMatterParamsOverrides = "warning-frontmatter-params-overrides"
2424
WarnRenderShortcodesInHTML = "warning-rendershortcodes-in-html"
2525
WarnGoldmarkRawHTML = "warning-goldmark-raw-html"
26+
WarnPartialSuperfluousPrefix = "warning-partial-superfluous-prefix"
2627
)
2728

2829
// Field/method names with special meaning.

common/maps/cache.go

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func (c *Cache[K, T]) GetOrCreate(key K, create func() (T, error)) (T, error) {
6969
return v, nil
7070
}
7171

72+
// Contains returns whether the given key exists in the cache.
73+
func (c *Cache[K, T]) Contains(key K) bool {
74+
c.RLock()
75+
_, found := c.m[key]
76+
c.RUnlock()
77+
return found
78+
}
79+
7280
// InitAndGet initializes the cache if not already done and returns the value for the given key.
7381
// The init state will be reset on Reset or Drain.
7482
func (c *Cache[K, T]) InitAndGet(key K, init func(get func(key K) (T, bool), set func(key K, value T)) error) (T, error) {
@@ -108,6 +116,17 @@ func (c *Cache[K, T]) Set(key K, value T) {
108116
c.Unlock()
109117
}
110118

119+
// SetIfAbsent sets the given key to the given value if the key does not already exist in the cache.
120+
func (c *Cache[K, T]) SetIfAbsent(key K, value T) {
121+
c.RLock()
122+
if _, found := c.get(key); !found {
123+
c.RUnlock()
124+
c.Set(key, value)
125+
} else {
126+
c.RUnlock()
127+
}
128+
}
129+
111130
func (c *Cache[K, T]) set(key K, value T) {
112131
c.m[key] = value
113132
}

common/maps/ordered.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
package maps
1515

1616
import (
17-
"github.com/gohugoio/hugo/common/hashing"
1817
"slices"
18+
19+
"github.com/gohugoio/hugo/common/hashing"
1920
)
2021

2122
// Ordered is a map that can be iterated in the order of insertion.
@@ -57,6 +58,15 @@ func (m *Ordered[K, T]) Get(key K) (T, bool) {
5758
return value, found
5859
}
5960

61+
// Has returns whether the given key exists in the map.
62+
func (m *Ordered[K, T]) Has(key K) bool {
63+
if m == nil {
64+
return false
65+
}
66+
_, found := m.values[key]
67+
return found
68+
}
69+
6070
// Delete deletes the value for the given key.
6171
func (m *Ordered[K, T]) Delete(key K) {
6272
if m == nil {

0 commit comments

Comments
 (0)