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

@@ -5,6 +5,7 @@ type BuiltinTypes struct {
CSSType Type
SCSSType Type
SASSType Type
GotmplType Type
CSVType Type
HTMLType Type
JavascriptType Type
@@ -60,6 +61,7 @@ var Builtin = BuiltinTypes{
CSSType: Type{Type: "text/css"},
SCSSType: Type{Type: "text/x-scss"},
SASSType: Type{Type: "text/x-sass"},
GotmplType: Type{Type: "text/x-gotmpl"},
CSVType: Type{Type: "text/csv"},
HTMLType: Type{Type: "text/html"},
JavascriptType: Type{Type: "text/javascript"},
@@ -121,6 +123,7 @@ var defaultMediaTypesConfig = map[string]any{
"text/typescript": map[string]any{"suffixes": []string{"ts"}},
"text/tsx": map[string]any{"suffixes": []string{"tsx"}},
"text/jsx": map[string]any{"suffixes": []string{"jsx"}},
"text/x-gotmpl": map[string]any{"suffixes": []string{"gotmpl"}},
"application/json": map[string]any{"suffixes": []string{"json"}},
"application/manifest+json": map[string]any{"suffixes": []string{"webmanifest"}},

View File

@@ -17,6 +17,7 @@ import (
"fmt"
"path/filepath"
"reflect"
"slices"
"sort"
"strings"
@@ -26,7 +27,6 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
"slices"
)
// DefaultTypes is the default media types supported by Hugo.
@@ -271,4 +271,7 @@ var DefaultPathParser = &paths.PathParser{
IsContentExt: func(ext string) bool {
panic("not supported")
},
IsOutputFormat: func(name, ext string) bool {
panic("DefaultPathParser: not supported")
},
}

View File

@@ -151,5 +151,5 @@ func TestDefaultTypes(t *testing.T) {
}
c.Assert(len(DefaultTypes), qt.Equals, 40)
c.Assert(len(DefaultTypes), qt.Equals, 41)
}

View File

@@ -282,7 +282,7 @@ func (t Types) BySuffix(suffix string) []Type {
suffix = t.normalizeSuffix(suffix)
var types []Type
for _, tt := range t {
if tt.hasSuffix(suffix) {
if tt.HasSuffix(suffix) {
types = append(types, tt)
}
}
@@ -293,7 +293,7 @@ func (t Types) BySuffix(suffix string) []Type {
func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool) {
suffix = t.normalizeSuffix(suffix)
for _, tt := range t {
if tt.hasSuffix(suffix) {
if tt.HasSuffix(suffix) {
return tt, SuffixInfo{
FullSuffix: tt.Delimiter + suffix,
Suffix: suffix,
@@ -310,7 +310,7 @@ func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool) {
func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool) {
suffix = t.normalizeSuffix(suffix)
for _, tt := range t {
if tt.hasSuffix(suffix) {
if tt.HasSuffix(suffix) {
if found {
// ambiguous
found = false
@@ -330,14 +330,14 @@ func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool) {
func (t Types) IsTextSuffix(suffix string) bool {
suffix = t.normalizeSuffix(suffix)
for _, tt := range t {
if tt.hasSuffix(suffix) {
if tt.HasSuffix(suffix) {
return tt.IsText()
}
}
return false
}
func (m Type) hasSuffix(suffix string) bool {
func (m Type) HasSuffix(suffix string) bool {
return strings.Contains(","+m.SuffixesCSV+",", ","+suffix+",")
}