mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
@@ -26,7 +26,7 @@ func init() {
|
||||
|
||||
ns := &internal.TemplateFuncsNamespace{
|
||||
Name: name,
|
||||
Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
|
||||
Context: func(args ...any) (any, error) { return ctx, nil },
|
||||
}
|
||||
|
||||
ns.AddMethodMapping(ctx.Emojify,
|
||||
|
@@ -17,8 +17,8 @@ import (
|
||||
// It is not a general purpose YAML to TOML converter etc., and may
|
||||
// change without notice if it serves a purpose in the docs.
|
||||
// Format is one of json, yaml or toml.
|
||||
func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) {
|
||||
var meta map[string]interface{}
|
||||
func (ns *Namespace) Remarshal(format string, data any) (string, error) {
|
||||
var meta map[string]any
|
||||
|
||||
format = strings.TrimSpace(strings.ToLower(format))
|
||||
|
||||
@@ -27,7 +27,7 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if m, ok := data.(map[string]interface{}); ok {
|
||||
if m, ok := data.(map[string]any); ok {
|
||||
meta = m
|
||||
} else {
|
||||
from, err := cast.ToStringE(data)
|
||||
@@ -65,10 +65,10 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error)
|
||||
// The unmarshal/marshal dance is extremely type lossy, and we need
|
||||
// to make sure that integer types prints as "43" and not "43.0" in
|
||||
// all formats, hence this hack.
|
||||
func applyMarshalTypes(m map[string]interface{}) {
|
||||
func applyMarshalTypes(m map[string]any) {
|
||||
for k, v := range m {
|
||||
switch t := v.(type) {
|
||||
case map[string]interface{}:
|
||||
case map[string]any:
|
||||
applyMarshalTypes(t)
|
||||
case float64:
|
||||
i := int64(t)
|
||||
|
@@ -184,7 +184,7 @@ a = "b"
|
||||
})
|
||||
|
||||
c.Run("Map input", func(c *qt.C) {
|
||||
input := map[string]interface{}{
|
||||
input := map[string]any{
|
||||
"hello": "world",
|
||||
}
|
||||
|
||||
|
@@ -51,7 +51,7 @@ type Namespace struct {
|
||||
// Emojify returns a copy of s with all emoji codes replaced with actual emojis.
|
||||
//
|
||||
// See http://www.emoji-cheat-sheet.com/
|
||||
func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) {
|
||||
func (ns *Namespace) Emojify(s any) (template.HTML, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -62,13 +62,13 @@ func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) {
|
||||
|
||||
// Highlight returns a copy of s as an HTML string with syntax
|
||||
// highlighting applied.
|
||||
func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{}) (template.HTML, error) {
|
||||
func (ns *Namespace) Highlight(s any, lang string, opts ...any) (template.HTML, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var optsv interface{}
|
||||
var optsv any
|
||||
if len(opts) > 0 {
|
||||
optsv = opts[0]
|
||||
}
|
||||
@@ -79,8 +79,8 @@ func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{})
|
||||
}
|
||||
|
||||
// HighlightCodeBlock highlights a code block on the form received in the codeblock render hooks.
|
||||
func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...interface{}) (highlight.HightlightResult, error) {
|
||||
var optsv interface{}
|
||||
func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...any) (highlight.HightlightResult, error) {
|
||||
var optsv any
|
||||
if len(opts) > 0 {
|
||||
optsv = opts[0]
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (ns *Namespace) CanHighlight(lang string) bool {
|
||||
}
|
||||
|
||||
// HTMLEscape returns a copy of s with reserved HTML characters escaped.
|
||||
func (ns *Namespace) HTMLEscape(s interface{}) (string, error) {
|
||||
func (ns *Namespace) HTMLEscape(s any) (string, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -107,7 +107,7 @@ func (ns *Namespace) HTMLEscape(s interface{}) (string, error) {
|
||||
|
||||
// HTMLUnescape returns a copy of with HTML escape requences converted to plain
|
||||
// text.
|
||||
func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) {
|
||||
func (ns *Namespace) HTMLUnescape(s any) (string, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -117,7 +117,7 @@ func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) {
|
||||
}
|
||||
|
||||
// Markdownify renders a given input from Markdown to HTML.
|
||||
func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) {
|
||||
func (ns *Namespace) Markdownify(s any) (template.HTML, error) {
|
||||
|
||||
home := ns.deps.Site.Home()
|
||||
if home == nil {
|
||||
@@ -135,7 +135,7 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) {
|
||||
}
|
||||
|
||||
// Plainify returns a copy of s with all HTML tags removed.
|
||||
func (ns *Namespace) Plainify(s interface{}) (string, error) {
|
||||
func (ns *Namespace) Plainify(s any) (string, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@@ -42,8 +42,8 @@ func TestEmojify(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
s any
|
||||
expect any
|
||||
}{
|
||||
{":notamoji:", template.HTML(":notamoji:")},
|
||||
{"I :heart: Hugo", template.HTML("I ❤️ Hugo")},
|
||||
@@ -72,10 +72,10 @@ func TestHighlight(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
s any
|
||||
lang string
|
||||
opts interface{}
|
||||
expect interface{}
|
||||
opts any
|
||||
expect any
|
||||
}{
|
||||
{"func boo() {}", "go", "", "boo"},
|
||||
{"func boo() {}", "go", nil, "boo"},
|
||||
@@ -117,8 +117,8 @@ func TestHTMLEscape(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
s any
|
||||
expect any
|
||||
}{
|
||||
{`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`},
|
||||
{"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"},
|
||||
@@ -147,8 +147,8 @@ func TestHTMLUnescape(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
s any
|
||||
expect any
|
||||
}{
|
||||
{`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`},
|
||||
{"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"},
|
||||
@@ -177,8 +177,8 @@ func TestMarkdownify(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
s any
|
||||
expect any
|
||||
}{
|
||||
{"Hello **World!**", template.HTML("Hello <strong>World!</strong>")},
|
||||
{[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")},
|
||||
@@ -233,8 +233,8 @@ func TestPlainify(t *testing.T) {
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
for _, test := range []struct {
|
||||
s interface{}
|
||||
expect interface{}
|
||||
s any
|
||||
expect any
|
||||
}{
|
||||
{"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"},
|
||||
// errors
|
||||
|
@@ -33,18 +33,18 @@ import (
|
||||
// Unmarshal unmarshals the data given, which can be either a string, json.RawMessage
|
||||
// or a Resource. Supported formats are JSON, TOML, YAML, and CSV.
|
||||
// You can optionally provide an options map as the first argument.
|
||||
func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
|
||||
func (ns *Namespace) Unmarshal(args ...any) (any, error) {
|
||||
if len(args) < 1 || len(args) > 2 {
|
||||
return nil, errors.New("unmarshal takes 1 or 2 arguments")
|
||||
}
|
||||
|
||||
var data interface{}
|
||||
var data any
|
||||
decoder := metadecoders.Default
|
||||
|
||||
if len(args) == 1 {
|
||||
data = args[0]
|
||||
} else {
|
||||
m, ok := args[0].(map[string]interface{})
|
||||
m, ok := args[0].(map[string]any)
|
||||
if !ok {
|
||||
return nil, errors.New("first argument must be a map")
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
|
||||
key += decoder.OptionsKey()
|
||||
}
|
||||
|
||||
return ns.cache.GetOrCreate(key, func() (interface{}, error) {
|
||||
return ns.cache.GetOrCreate(key, func() (any, error) {
|
||||
f := metadecoders.FormatFromMediaType(r.MediaType())
|
||||
if f == "" {
|
||||
return nil, errors.Errorf("MIME %q not supported", r.MediaType())
|
||||
@@ -101,7 +101,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
|
||||
|
||||
key := helpers.MD5String(dataStr)
|
||||
|
||||
return ns.cache.GetOrCreate(key, func() (interface{}, error) {
|
||||
return ns.cache.GetOrCreate(key, func() (any, error) {
|
||||
f := decoder.FormatFromContentString(dataStr)
|
||||
if f == "" {
|
||||
return nil, errors.New("unknown format")
|
||||
@@ -111,7 +111,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
|
||||
})
|
||||
}
|
||||
|
||||
func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) {
|
||||
func decodeDecoder(m map[string]any) (metadecoders.Decoder, error) {
|
||||
opts := metadecoders.Default
|
||||
|
||||
if m == nil {
|
||||
@@ -144,7 +144,7 @@ func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) {
|
||||
return opts, err
|
||||
}
|
||||
|
||||
func stringToRune(v interface{}) (rune, error) {
|
||||
func stringToRune(v any) (rune, error) {
|
||||
s, err := cast.ToStringE(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@@ -87,34 +87,34 @@ func TestUnmarshal(t *testing.T) {
|
||||
|
||||
ns := transform.New(b.H.Deps)
|
||||
|
||||
assertSlogan := func(m map[string]interface{}) {
|
||||
assertSlogan := func(m map[string]any) {
|
||||
b.Assert(m["slogan"], qt.Equals, "Hugo Rocks!")
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
data interface{}
|
||||
options interface{}
|
||||
expect interface{}
|
||||
data any
|
||||
options any
|
||||
expect any
|
||||
}{
|
||||
{`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]interface{}) {
|
||||
{`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{`slogan: "Hugo Rocks!"`, nil, func(m map[string]interface{}) {
|
||||
{`slogan: "Hugo Rocks!"`, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{`slogan = "Hugo Rocks!"`, nil, func(m map[string]interface{}) {
|
||||
{`slogan = "Hugo Rocks!"`, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]interface{}) {
|
||||
{testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]interface{}) {
|
||||
{testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]interface{}) {
|
||||
{testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.XMLType}, nil, func(m map[string]interface{}) {
|
||||
{testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.XMLType}, nil, func(m map[string]any) {
|
||||
assertSlogan(m)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00
|
||||
@@ -124,18 +124,18 @@ func TestUnmarshal(t *testing.T) {
|
||||
b.Assert(len(first), qt.Equals, 5)
|
||||
b.Assert(first[1], qt.Equals, "Ford")
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
||||
{testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]any{"delimiter": ";"}, func(r [][]string) {
|
||||
b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
}},
|
||||
{"a,b,c", nil, func(r [][]string) {
|
||||
b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
}},
|
||||
{"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
|
||||
{"a;b;c", map[string]any{"delimiter": ";"}, func(r [][]string) {
|
||||
b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
}},
|
||||
{testContentResource{key: "r1", content: `
|
||||
% This is a comment
|
||||
a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
|
||||
a;b;c`, mime: media.CSVType}, map[string]any{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
|
||||
b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r)
|
||||
}},
|
||||
// errors
|
||||
@@ -149,21 +149,21 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment"
|
||||
|
||||
ns.Reset()
|
||||
|
||||
var args []interface{}
|
||||
var args []any
|
||||
|
||||
if test.options != nil {
|
||||
args = []interface{}{test.options, test.data}
|
||||
args = []any{test.options, test.data}
|
||||
} else {
|
||||
args = []interface{}{test.data}
|
||||
args = []any{test.data}
|
||||
}
|
||||
|
||||
result, err := ns.Unmarshal(args...)
|
||||
|
||||
if bb, ok := test.expect.(bool); ok && !bb {
|
||||
b.Assert(err, qt.Not(qt.IsNil))
|
||||
} else if fn, ok := test.expect.(func(m map[string]interface{})); ok {
|
||||
} else if fn, ok := test.expect.(func(m map[string]any)); ok {
|
||||
b.Assert(err, qt.IsNil)
|
||||
m, ok := result.(map[string]interface{})
|
||||
m, ok := result.(map[string]any)
|
||||
b.Assert(ok, qt.Equals, true)
|
||||
fn(m)
|
||||
} else if fn, ok := test.expect.(func(r [][]string)); ok {
|
||||
|
Reference in New Issue
Block a user