hugolib, media: Make the MediaType available to the templates - hugo - [fork] hugo port for 9front
 (HTM) git clone git@git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 4aaed87dd97b085b1505e53b7d4564aa8f7f18ef
 (DIR) parent c7dbee2321af2f0d61bdc976829681f3799582a9
 (HTM) Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
       Date:   Wed, 22 Mar 2017 11:03:42 +0100
       
       hugolib, media: Make the MediaType available to the templates
       
       Diffstat:
         M hugolib/page_output.go              |       8 ++++++++
         M media/mediaType.go                  |      21 +++++++++++----------
         M media/mediaType_test.go             |       8 ++++----
       
       3 files changed, 23 insertions(+), 14 deletions(-)
       ---
 (DIR) diff --git a/hugolib/page_output.go b/hugolib/page_output.go
       @@ -19,6 +19,8 @@ import (
                "strings"
                "sync"
        
       +        "github.com/spf13/hugo/media"
       +
                "github.com/spf13/hugo/output"
        )
        
       @@ -133,6 +135,7 @@ type OutputFormat struct {
        
                // It may be tempting to export this, but let us hold on to that horse for a while.
                f output.Format
       +
                p *Page
        }
        
       @@ -141,6 +144,11 @@ func (o OutputFormat) Name() string {
                return o.f.Name
        }
        
       +// MediaType returns this OutputFormat's MediaType (MIME type).
       +func (o OutputFormat) MediaType() media.Type {
       +        return o.f.MediaType
       +}
       +
        // TODO(bep) outputs consider just save this wrapper on Page.
        // OutputFormats gives the output formats for this Page.
        func (p *Page) OutputFormats() OutputFormats {
 (DIR) diff --git a/media/mediaType.go b/media/mediaType.go
       @@ -26,23 +26,24 @@ type Types []Type
        // If suffix is not provided, the sub type will be used.
        // See // https://en.wikipedia.org/wiki/Media_type
        type Type struct {
       -        Type    string // i.e. text
       -        SubType string // i.e. html
       -        Suffix  string // i.e html
       +        MainType string // i.e. text
       +        SubType  string // i.e. html
       +        Suffix   string // i.e html
        }
        
       -// Key return a key used to identify this media type. Hugo will register a set of
       -// default media types. These can be overridden by the user in the configuration,
       -// by defining a media type with the same Key.
       -func (m Type) Key() string {
       -        return fmt.Sprintf("%s/%s", m.Type, m.SubType)
       +// Type returns a string representing the main- and sub-type of a media type, i.e. "text/css".
       +// Hugo will register a set of default media types.
       +// These can be overridden by the user in the configuration,
       +// by defining a media type with the same Type.
       +func (m Type) Type() string {
       +        return fmt.Sprintf("%s/%s", m.MainType, m.SubType)
        }
        
        func (m Type) String() string {
                if m.Suffix != "" {
       -                return fmt.Sprintf("%s/%s+%s", m.Type, m.SubType, m.Suffix)
       +                return fmt.Sprintf("%s/%s+%s", m.MainType, m.SubType, m.Suffix)
                }
       -        return fmt.Sprintf("%s/%s", m.Type, m.SubType)
       +        return fmt.Sprintf("%s/%s", m.MainType, m.SubType)
        }
        
        var (
 (DIR) diff --git a/media/mediaType_test.go b/media/mediaType_test.go
       @@ -20,18 +20,18 @@ import (
        )
        
        func TestDefaultTypes(t *testing.T) {
       -        require.Equal(t, "text", HTMLType.Type)
       +        require.Equal(t, "text", HTMLType.MainType)
                require.Equal(t, "html", HTMLType.SubType)
                require.Equal(t, "html", HTMLType.Suffix)
        
       -        require.Equal(t, "text/html", HTMLType.Key())
       +        require.Equal(t, "text/html", HTMLType.MainType())
                require.Equal(t, "text/html+html", HTMLType.String())
        
       -        require.Equal(t, "application", RSSType.Type)
       +        require.Equal(t, "application", RSSType.MainType)
                require.Equal(t, "rss", RSSType.SubType)
                require.Equal(t, "xml", RSSType.Suffix)
        
       -        require.Equal(t, "application/rss", RSSType.Key())
       +        require.Equal(t, "application/rss", RSSType.MainType())
                require.Equal(t, "application/rss+xml", RSSType.String())
        
        }