mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Implement the first generic JSON output testcase
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user