output: Add output formats decoder

And clean up the output package.
This commit is contained in:
Bjørn Erik Pedersen
2017-04-03 17:00:23 +02:00
parent d6e8b86f66
commit c9aee467d3
6 changed files with 281 additions and 91 deletions

View File

@@ -15,10 +15,20 @@ package media
import (
"fmt"
"strings"
)
type Types []Type
func (t Types) GetByType(tp string) (Type, bool) {
for _, tt := range t {
if strings.EqualFold(tt.Type(), tp) {
return tt, true
}
}
return Type{}, false
}
// A media type (also known as MIME type and content type) is a two-part identifier for
// file formats and format contents transmitted on the Internet.
// For Hugo's use case, we use the top-level type name / subtype name + suffix.

View File

@@ -47,3 +47,14 @@ func TestDefaultTypes(t *testing.T) {
}
}
func TestGetByType(t *testing.T) {
types := Types{HTMLType, RSSType}
mt, found := types.GetByType("text/HTML")
require.True(t, found)
require.Equal(t, mt, HTMLType)
_, found = types.GetByType("text/nono")
require.False(t, found)
}