diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /tpl/transform | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) |
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code.
Also,
* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.
Closes #10896
Closes #10620
Diffstat (limited to 'tpl/transform')
-rw-r--r-- | tpl/transform/transform_test.go | 25 | ||||
-rw-r--r-- | tpl/transform/unmarshal.go | 2 | ||||
-rw-r--r-- | tpl/transform/unmarshal_test.go | 20 |
3 files changed, 11 insertions, 36 deletions
diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go index 86ddb1259..d645ca8e2 100644 --- a/tpl/transform/transform_test.go +++ b/tpl/transform/transform_test.go @@ -19,17 +19,10 @@ import ( "strings" "testing" - "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/tpl/transform" - "github.com/spf13/afero" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/hugofs" - "github.com/gohugoio/hugo/langs" ) type tstNoStringer struct{} @@ -254,21 +247,3 @@ func TestPlainify(t *testing.T) { b.Assert(result, qt.Equals, test.expect) } } - -func newDeps(cfg config.Provider) *deps.Deps { - cfg.Set("contentDir", "content") - cfg.Set("i18nDir", "i18n") - - l := langs.NewLanguage("en", cfg) - - cs, err := helpers.NewContentSpec(l, loggers.NewErrorLogger(), afero.NewMemMapFs(), nil) - if err != nil { - panic(err) - } - - return &deps.Deps{ - Cfg: cfg, - Fs: hugofs.NewMem(l), - ContentSpec: cs, - } -} diff --git a/tpl/transform/unmarshal.go b/tpl/transform/unmarshal.go index f5ff63585..3936126ca 100644 --- a/tpl/transform/unmarshal.go +++ b/tpl/transform/unmarshal.go @@ -72,7 +72,7 @@ func (ns *Namespace) Unmarshal(args ...any) (any, error) { } return ns.cache.GetOrCreate(key, func() (any, error) { - f := metadecoders.FormatFromMediaType(r.MediaType()) + f := metadecoders.FormatFromStrings(r.MediaType().Suffixes()...) if f == "" { return nil, fmt.Errorf("MIME %q not supported", r.MediaType()) } diff --git a/tpl/transform/unmarshal_test.go b/tpl/transform/unmarshal_test.go index e63f96de2..12774298a 100644 --- a/tpl/transform/unmarshal_test.go +++ b/tpl/transform/unmarshal_test.go @@ -105,26 +105,26 @@ func TestUnmarshal(t *testing.T) { {`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]any) { + {testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.Builtin.YAMLType}, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]any) { + {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.Builtin.JSONType}, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]any) { + {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.Builtin.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]any) { + {testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.Builtin.XMLType}, nil, func(m map[string]any) { assertSlogan(m) }}, {testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00 -1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.CSVType}, nil, func(r [][]string) { +1999,Chevy,"Venture ""Extended Edition""","",4900.00`, mime: media.Builtin.CSVType}, nil, func(r [][]string) { b.Assert(len(r), qt.Equals, 2) first := r[0] 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]any{"delimiter": ";"}, func(r [][]string) { + {testContentResource{key: "r1", content: `a;b;c`, mime: media.Builtin.CSVType}, map[string]any{"delimiter": ";"}, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, {"a,b,c", nil, func(r [][]string) { @@ -135,13 +135,13 @@ func TestUnmarshal(t *testing.T) { }}, {testContentResource{key: "r1", content: ` % This is a comment -a;b;c`, mime: media.CSVType}, map[string]any{"DElimiter": ";", "Comment": "%"}, func(r [][]string) { +a;b;c`, mime: media.Builtin.CSVType}, map[string]any{"DElimiter": ";", "Comment": "%"}, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, // errors {"thisisnotavaliddataformat", nil, false}, - {testContentResource{key: "r1", content: `invalid&toml"`, mime: media.TOMLType}, nil, false}, - {testContentResource{key: "r1", content: `unsupported: MIME"`, mime: media.CalendarType}, nil, false}, + {testContentResource{key: "r1", content: `invalid&toml"`, mime: media.Builtin.TOMLType}, nil, false}, + {testContentResource{key: "r1", content: `unsupported: MIME"`, mime: media.Builtin.CalendarType}, nil, false}, {"thisisnotavaliddataformat", nil, false}, {`{ notjson }`, nil, false}, {tstNoStringer{}, nil, false}, @@ -217,7 +217,7 @@ func BenchmarkUnmarshalResource(b *testing.B) { var jsons [numJsons]testContentResource for i := 0; i < numJsons; i++ { key := fmt.Sprintf("root%d", i) - jsons[i] = testContentResource{key: key, content: strings.Replace(testJSON, "ROOT_KEY", key, 1), mime: media.JSONType} + jsons[i] = testContentResource{key: key, content: strings.Replace(testJSON, "ROOT_KEY", key, 1), mime: media.Builtin.JSONType} } b.ResetTimer() |