diff --git a/resource/image.go b/resource/image.go index 8b1ba8209..e8b87cdb4 100644 --- a/resource/image.go +++ b/resource/image.go @@ -16,28 +16,26 @@ package resource import ( "errors" "fmt" + "image" "image/color" + "image/draw" + "image/jpeg" "io" "os" "strconv" "strings" - - "github.com/mitchellh/mapstructure" - - "github.com/gohugoio/hugo/common/hugio" - "github.com/gohugoio/hugo/helpers" - - // Importing image codecs for image.DecodeConfig - "image" - "image/draw" - _ "image/gif" - "image/jpeg" - _ "image/png" - - "github.com/disintegration/imaging" - // Import webp codec "sync" + "github.com/disintegration/imaging" + "github.com/gohugoio/hugo/common/hugio" + "github.com/gohugoio/hugo/helpers" + "github.com/mitchellh/mapstructure" + + // Blind import for image.Decode + _ "image/gif" + _ "image/png" + + // Blind import for image.Decode _ "golang.org/x/image/webp" ) @@ -117,6 +115,7 @@ var imageFilters = map[string]imaging.ResampleFilter{ strings.ToLower("Cosine"): imaging.Cosine, } +// Image represents an image resource. type Image struct { config image.Config configInit sync.Once @@ -134,17 +133,19 @@ type Image struct { *genericResource } +// Width returns i's width. func (i *Image) Width() int { i.initConfig() return i.config.Width } +// Height returns i's height. func (i *Image) Height() int { i.initConfig() return i.config.Height } -// Implement the Cloner interface. +// WithNewBase implements the Cloner interface. func (i *Image) WithNewBase(base string) Resource { return &Image{ imaging: i.imaging, @@ -535,9 +536,8 @@ func (i *Image) encodeToDestinations(img image.Image, conf imageConfig, resource } if rgba != nil { return jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality}) - } else { - return jpeg.Encode(w, img, &jpeg.Options{Quality: quality}) } + return jpeg.Encode(w, img, &jpeg.Options{Quality: quality}) default: return imaging.Encode(w, img, i.format) } diff --git a/resource/resource.go b/resource/resource.go index dbbbca3ad..9a974e912 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -51,8 +51,6 @@ var ( _ permalinker = (*genericResource)(nil) ) -const DefaultResourceType = "unknown" - var noData = make(map[string]interface{}) // Source is an internal template and not meant for use in the templates. It @@ -110,6 +108,8 @@ type Resource interface { Params() map[string]interface{} } +// ResourcesLanguageMerger describes an interface for merging resources from a +// different language. type ResourcesLanguageMerger interface { MergeByLanguage(other Resources) Resources // Needed for integration with the tpl package. @@ -136,7 +136,7 @@ type ContentResource interface { Content() (interface{}, error) } -// OpenReadSeekeCloser allows setting some other way (than reading from a filesystem) +// OpenReadSeekCloser allows setting some other way (than reading from a filesystem) // to open or create a ReadSeekCloser. type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error) @@ -150,6 +150,7 @@ type ReadSeekCloserResource interface { // I.e. both pages and images etc. type Resources []Resource +// ByType returns resources of a given resource type (ie. "image"). func (r Resources) ByType(tp string) Resources { var filtered Resources @@ -230,19 +231,19 @@ func getGlob(pattern string) (glob.Glob, error) { } // MergeByLanguage adds missing translations in r1 from r2. -func (r1 Resources) MergeByLanguage(r2 Resources) Resources { - result := append(Resources(nil), r1...) +func (r Resources) MergeByLanguage(r2 Resources) Resources { + result := append(Resources(nil), r...) m := make(map[string]bool) - for _, r := range r1 { - if translated, ok := r.(translatedResource); ok { + for _, rr := range r { + if translated, ok := rr.(translatedResource); ok { m[translated.TranslationKey()] = true } } - for _, r := range r2 { - if translated, ok := r.(translatedResource); ok { + for _, rr := range r2 { + if translated, ok := rr.(translatedResource); ok { if _, found := m[translated.TranslationKey()]; !found { - result = append(result, r) + result = append(result, rr) } } } @@ -251,12 +252,12 @@ func (r1 Resources) MergeByLanguage(r2 Resources) Resources { // MergeByLanguageInterface is the generic version of MergeByLanguage. It // is here just so it can be called from the tpl package. -func (r1 Resources) MergeByLanguageInterface(in interface{}) (interface{}, error) { +func (r Resources) MergeByLanguageInterface(in interface{}) (interface{}, error) { r2, ok := in.(Resources) if !ok { return nil, fmt.Errorf("%T cannot be merged by language", in) } - return r1.MergeByLanguage(r2), nil + return r.MergeByLanguage(r2), nil } type Spec struct {