From c8fff9501d424882a42f750800d9982ec47df640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 8 Mar 2017 13:45:33 +0100 Subject: Implement the first generic JSON output testcase --- output/outputType.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ output/outputType_test.go | 9 ++++++++ 2 files changed, 64 insertions(+) (limited to 'output') diff --git a/output/outputType.go b/output/outputType.go index cf5fff76e..e3df96f0b 100644 --- a/output/outputType.go +++ b/output/outputType.go @@ -14,16 +14,40 @@ package output import ( + "fmt" + "strings" + "github.com/spf13/hugo/media" ) var ( + // An ordered list of built-in output formats + // See https://www.ampproject.org/learn/overview/ + AMPType = Type{ + Name: "AMP", + MediaType: media.HTMLType, + BaseName: "index", + } + + CSSType = Type{ + Name: "CSS", + MediaType: media.CSSType, + BaseName: "styles", + } + HTMLType = Type{ Name: "HTML", MediaType: media.HTMLType, BaseName: "index", } + JSONType = Type{ + Name: "JSON", + MediaType: media.HTMLType, + BaseName: "index", + IsPlainText: true, + } + RSSType = Type{ Name: "RSS", MediaType: media.RSSType, @@ -31,6 +55,14 @@ var ( } ) +var builtInTypes = map[string]Type{ + strings.ToLower(AMPType.Name): AMPType, + strings.ToLower(CSSType.Name): CSSType, + strings.ToLower(HTMLType.Name): HTMLType, + strings.ToLower(JSONType.Name): JSONType, + strings.ToLower(RSSType.Name): RSSType, +} + type Types []Type // Type represents an output represenation, usually to a file on disk. @@ -57,3 +89,26 @@ type Type struct { // Enable to ignore the global uglyURLs setting. NoUgly bool } + +func GetType(key string) (Type, bool) { + found, ok := builtInTypes[key] + if !ok { + found, ok = builtInTypes[strings.ToLower(key)] + } + return found, ok +} + +// TODO(bep) outputs rewamp on global config? +func GetTypes(keys ...string) (Types, error) { + var types []Type + + for _, key := range keys { + tpe, ok := GetType(key) + if !ok { + return types, fmt.Errorf("OutputType with key %q not found", key) + } + types = append(types, tpe) + } + + return types, nil +} diff --git a/output/outputType_test.go b/output/outputType_test.go index 6f84c93d3..a55b9a81a 100644 --- a/output/outputType_test.go +++ b/output/outputType_test.go @@ -31,3 +31,12 @@ func TestDefaultTypes(t *testing.T) { require.Empty(t, RSSType.Path) require.False(t, RSSType.IsPlainText) } + +func TestGetType(t *testing.T) { + tp, _ := GetType("html") + require.Equal(t, HTMLType, tp) + tp, _ = GetType("HTML") + require.Equal(t, HTMLType, tp) + _, found := GetType("FOO") + require.False(t, found) +} -- cgit v1.2.3