Skip to content

Commit

Permalink
Some godoc adjustments and image struct renames
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 27, 2022
1 parent 1104753 commit fa80fe3
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 90 deletions.
2 changes: 1 addition & 1 deletion hugolib/collections.go
Expand Up @@ -27,7 +27,7 @@ var (
// here as it makes it easier to get an idea of "type coverage". These
// implementations have no value on their own.

// Slice is not meant to be used externally. It's a bridge function
// Slice is for internal use.
func (p *pageState) Slice(items any) (any, error) {
return page.ToPages(items)
}
Expand Down
11 changes: 11 additions & 0 deletions langs/language.go
Expand Up @@ -55,21 +55,26 @@ type Language struct {
Title string
Weight int

// For internal use.
Disabled bool

// If set per language, this tells Hugo that all content files without any
// language indicator (e.g. my-page.en.md) is in this language.
// This is usually a path relative to the working dir, but it can be an
// absolute directory reference. It is what we get.
// For internal use.
ContentDir string

// Global config.
// For internal use.
Cfg config.Provider

// Language specific config.
// For internal use.
LocalCfg config.Provider

// Composite config.
// For internal use.
config.Provider

// These are params declared in the [params] section of the language merged with the
Expand All @@ -91,6 +96,7 @@ type Language struct {
initErr error
}

// For internal use.
func (l *Language) String() string {
return l.Lang
}
Expand Down Expand Up @@ -233,6 +239,7 @@ func (l Languages) IsMultihost() bool {

// SetParam sets a param with the given key and value.
// SetParam is case-insensitive.
// For internal use.
func (l *Language) SetParam(k string, v any) {
l.paramsMu.Lock()
defer l.paramsMu.Unlock()
Expand All @@ -245,6 +252,7 @@ func (l *Language) SetParam(k string, v any) {
// GetLocal gets a configuration value set on language level. It will
// not fall back to any global value.
// It will return nil if a value with the given key cannot be found.
// For internal use.
func (l *Language) GetLocal(key string) any {
if l == nil {
panic("language not set")
Expand All @@ -256,6 +264,7 @@ func (l *Language) GetLocal(key string) any {
return nil
}

// For internal use.
func (l *Language) Set(k string, v any) {
k = strings.ToLower(k)
if globalOnlySettings[k] {
Expand All @@ -265,11 +274,13 @@ func (l *Language) Set(k string, v any) {
}

// Merge is currently not supported for Language.
// For internal use.
func (l *Language) Merge(key string, value any) {
panic("Not supported")
}

// IsSet checks whether the key is set in the language or the related config store.
// For internal use.
func (l *Language) IsSet(key string) bool {
key = strings.ToLower(key)
if !globalOnlySettings[key] {
Expand Down
3 changes: 3 additions & 0 deletions media/mediaType.go
Expand Up @@ -163,6 +163,7 @@ func (m Type) Type() string {
return m.MainType + "/" + m.SubType
}

// For internal use.
func (m Type) String() string {
return m.Type()
}
Expand Down Expand Up @@ -510,11 +511,13 @@ func DecodeTypes(mms ...map[string]any) (Types, error) {
}

// IsZero reports whether this Type represents a zero value.
// For internal use.
func (m Type) IsZero() bool {
return m.SubType == ""
}

// MarshalJSON returns the JSON encoding of m.
// For internal use.
func (m Type) MarshalJSON() ([]byte, error) {
type Alias Type
return json.Marshal(&struct {
Expand Down
1 change: 1 addition & 0 deletions navigation/menu.go
Expand Up @@ -156,6 +156,7 @@ func (m *MenuEntry) isSamePage(p Page) bool {
return false
}

// For internal use.
func (m *MenuEntry) MarshallMap(ime map[string]any) error {
var err error
for k, v := range ime {
Expand Down
15 changes: 8 additions & 7 deletions resources/errorResource.go
Expand Up @@ -19,14 +19,15 @@ import (
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/images"
"github.com/gohugoio/hugo/resources/images/exif"
"github.com/gohugoio/hugo/resources/resource"
)

var (
_ error = (*errorResource)(nil)
// Imnage covers all current Resource implementations.
_ resource.Image = (*errorResource)(nil)
_ images.ImageResource = (*errorResource)(nil)
// The list of user facing and exported interfaces in resource.go
// Note that if we're missing some interface here, the user will still
// get an error, but not as pretty.
Expand Down Expand Up @@ -98,27 +99,27 @@ func (e *errorResource) Width() int {
panic(e.ResourceError)
}

func (e *errorResource) Crop(spec string) (resource.Image, error) {
func (e *errorResource) Crop(spec string) (images.ImageResource, error) {
panic(e.ResourceError)
}

func (e *errorResource) Fill(spec string) (resource.Image, error) {
func (e *errorResource) Fill(spec string) (images.ImageResource, error) {
panic(e.ResourceError)
}

func (e *errorResource) Fit(spec string) (resource.Image, error) {
func (e *errorResource) Fit(spec string) (images.ImageResource, error) {
panic(e.ResourceError)
}

func (e *errorResource) Resize(spec string) (resource.Image, error) {
func (e *errorResource) Resize(spec string) (images.ImageResource, error) {
panic(e.ResourceError)
}

func (e *errorResource) Filter(filters ...any) (resource.Image, error) {
func (e *errorResource) Filter(filters ...any) (images.ImageResource, error) {
panic(e.ResourceError)
}

func (e *errorResource) Exif() *exif.Exif {
func (e *errorResource) Exif() *exif.ExifInfo {
panic(e.ResourceError)
}

Expand Down
31 changes: 16 additions & 15 deletions resources/image.go
Expand Up @@ -49,12 +49,12 @@ import (
)

var (
_ resource.Image = (*imageResource)(nil)
_ resource.Source = (*imageResource)(nil)
_ resource.Cloner = (*imageResource)(nil)
_ images.ImageResource = (*imageResource)(nil)
_ resource.Source = (*imageResource)(nil)
_ resource.Cloner = (*imageResource)(nil)
)

// ImageResource represents an image resource.
// imageResource represents an image resource.
type imageResource struct {
*images.Image

Expand All @@ -70,14 +70,14 @@ type imageResource struct {
}

type imageMeta struct {
Exif *exif.Exif
Exif *exif.ExifInfo
}

func (i *imageResource) Exif() *exif.Exif {
func (i *imageResource) Exif() *exif.ExifInfo {
return i.root.getExif()
}

func (i *imageResource) getExif() *exif.Exif {
func (i *imageResource) getExif() *exif.ExifInfo {
i.metaInit.Do(func() {
supportsExif := i.Format == images.JPEG || i.Format == images.TIFF
if !supportsExif {
Expand Down Expand Up @@ -137,6 +137,7 @@ func (i *imageResource) getExif() *exif.Exif {
return i.meta.Exif
}

// Cloneis for internal use.
func (i *imageResource) Clone() resource.Resource {
gr := i.baseResource.Clone().(baseResource)
return &imageResource{
Expand Down Expand Up @@ -170,7 +171,7 @@ func (i *imageResource) cloneWithUpdates(u *transformationUpdate) (baseResource,
// Resize resizes the image to the specified width and height using the specified resampling
// filter and returns the transformed image. If one of width or height is 0, the image aspect
// ratio is preserved.
func (i *imageResource) Resize(spec string) (resource.Image, error) {
func (i *imageResource) Resize(spec string) (images.ImageResource, error) {
conf, err := i.decodeImageConfig("resize", spec)
if err != nil {
return nil, err
Expand All @@ -182,8 +183,8 @@ func (i *imageResource) Resize(spec string) (resource.Image, error) {
}

// Crop the image to the specified dimensions without resizing using the given anchor point.
// Space delimited config: 200x300 TopLeft
func (i *imageResource) Crop(spec string) (resource.Image, error) {
// Space delimited config, e.g. `200x300 TopLeft`.
func (i *imageResource) Crop(spec string) (images.ImageResource, error) {
conf, err := i.decodeImageConfig("crop", spec)
if err != nil {
return nil, err
Expand All @@ -196,7 +197,7 @@ func (i *imageResource) Crop(spec string) (resource.Image, error) {

// Fit scales down the image using the specified resample filter to fit the specified
// maximum width and height.
func (i *imageResource) Fit(spec string) (resource.Image, error) {
func (i *imageResource) Fit(spec string) (images.ImageResource, error) {
conf, err := i.decodeImageConfig("fit", spec)
if err != nil {
return nil, err
Expand All @@ -209,8 +210,8 @@ func (i *imageResource) Fit(spec string) (resource.Image, error) {

// Fill scales the image to the smallest possible size that will cover the specified dimensions,
// crops the resized image to the specified dimensions using the given anchor point.
// Space delimited config: 200x300 TopLeft
func (i *imageResource) Fill(spec string) (resource.Image, error) {
// Space delimited config, e.g. `200x300 TopLeft`.
func (i *imageResource) Fill(spec string) (images.ImageResource, error) {
conf, err := i.decodeImageConfig("fill", spec)
if err != nil {
return nil, err
Expand Down Expand Up @@ -238,7 +239,7 @@ func (i *imageResource) Fill(spec string) (resource.Image, error) {
return img, err
}

func (i *imageResource) Filter(filters ...any) (resource.Image, error) {
func (i *imageResource) Filter(filters ...any) (images.ImageResource, error) {
conf := images.GetDefaultImageConfig("filter", i.Proc.Cfg)

var gfilters []gift.Filter
Expand All @@ -264,7 +265,7 @@ const imageProcWorkers = 1

var imageProcSem = make(chan bool, imageProcWorkers)

func (i *imageResource) doWithImageConfig(conf images.ImageConfig, f func(src image.Image) (image.Image, error)) (resource.Image, error) {
func (i *imageResource) doWithImageConfig(conf images.ImageConfig, f func(src image.Image) (image.Image, error)) (images.ImageResource, error) {
img, err := i.getSpec().imageCache.getOrCreate(i, conf, func() (*imageResource, image.Image, error) {
imageProcSem <- true
defer func() {
Expand Down
27 changes: 13 additions & 14 deletions resources/image_test.go
Expand Up @@ -40,7 +40,6 @@ import (

"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/images"
"github.com/gohugoio/hugo/resources/resource"
"github.com/google/go-cmp/cmp"

"github.com/gohugoio/hugo/htesting/hqt"
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestImageTransformBasic(t *testing.T) {

fileCache := image.(specProvider).getSpec().FileCaches.ImageCache().Fs

assertWidthHeight := func(img resource.Image, w, h int) {
assertWidthHeight := func(img images.ImageResource, w, h int) {
c.Helper()
c.Assert(img, qt.Not(qt.IsNil))
c.Assert(img.Width(), qt.Equals, w)
Expand Down Expand Up @@ -162,7 +161,7 @@ func TestImageTransformFormat(t *testing.T) {

fileCache := image.(specProvider).getSpec().FileCaches.ImageCache().Fs

assertExtWidthHeight := func(img resource.Image, ext string, w, h int) {
assertExtWidthHeight := func(img images.ImageResource, ext string, w, h int) {
c.Helper()
c.Assert(img, qt.Not(qt.IsNil))
c.Assert(paths.Ext(img.RelPermalink()), qt.Equals, ext)
Expand Down Expand Up @@ -210,13 +209,13 @@ func TestImagePermalinkPublishOrder(t *testing.T) {
os.Remove(workDir)
}()

check1 := func(img resource.Image) {
check1 := func(img images.ImageResource) {
resizedLink := "/a/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_100x50_resize_q75_box.jpg"
c.Assert(img.RelPermalink(), qt.Equals, resizedLink)
assertImageFile(c, spec.PublishFs, resizedLink, 100, 50)
}

check2 := func(img resource.Image) {
check2 := func(img images.ImageResource) {
c.Assert(img.RelPermalink(), qt.Equals, "/a/sunset.jpg")
assertImageFile(c, spec.PublishFs, "a/sunset.jpg", 900, 562)
}
Expand All @@ -231,7 +230,7 @@ func TestImagePermalinkPublishOrder(t *testing.T) {
resized, err := orignal.Resize("100x50")
c.Assert(err, qt.IsNil)

check1(resized.(resource.Image))
check1(resized.(images.ImageResource))

if !checkOriginalFirst {
check2(orignal)
Expand Down Expand Up @@ -441,9 +440,9 @@ func TestImageExif(t *testing.T) {
c := qt.New(t)
fs := afero.NewMemMapFs()
spec := newTestResourceSpec(specDescriptor{fs: fs, c: c})
image := fetchResourceForSpec(spec, c, "sunset.jpg").(resource.Image)
image := fetchResourceForSpec(spec, c, "sunset.jpg").(images.ImageResource)

getAndCheckExif := func(c *qt.C, image resource.Image) {
getAndCheckExif := func(c *qt.C, image images.ImageResource) {
x := image.Exif()
c.Assert(x, qt.Not(qt.IsNil))

Expand All @@ -464,22 +463,22 @@ func TestImageExif(t *testing.T) {
}

getAndCheckExif(c, image)
image = fetchResourceForSpec(spec, c, "sunset.jpg").(resource.Image)
image = fetchResourceForSpec(spec, c, "sunset.jpg").(images.ImageResource)
// This will read from file cache.
getAndCheckExif(c, image)
}

func BenchmarkImageExif(b *testing.B) {
getImages := func(c *qt.C, b *testing.B, fs afero.Fs) []resource.Image {
getImages := func(c *qt.C, b *testing.B, fs afero.Fs) []images.ImageResource {
spec := newTestResourceSpec(specDescriptor{fs: fs, c: c})
images := make([]resource.Image, b.N)
imgs := make([]images.ImageResource, b.N)
for i := 0; i < b.N; i++ {
images[i] = fetchResourceForSpec(spec, c, "sunset.jpg", strconv.Itoa(i)).(resource.Image)
imgs[i] = fetchResourceForSpec(spec, c, "sunset.jpg", strconv.Itoa(i)).(images.ImageResource)
}
return images
return imgs
}

getAndCheckExif := func(c *qt.C, image resource.Image) {
getAndCheckExif := func(c *qt.C, image images.ImageResource) {
x := image.Exif()
c.Assert(x, qt.Not(qt.IsNil))
c.Assert(x.Long, qt.Equals, float64(-4.50846))
Expand Down
16 changes: 12 additions & 4 deletions resources/images/exif/exif.go
Expand Up @@ -32,10 +32,18 @@ import (

const exifTimeLayout = "2006:01:02 15:04:05"

type Exif struct {
Lat float64
// ExifInfo holds the decoded Exif data for an Image.
type ExifInfo struct {
// GPS latitude in degrees.
Lat float64

// GPS longitude in degrees.
Long float64

// Image creation date/time.
Date time.Time

// A collection of the available Exif tags for this Image.
Tags Tags
}

Expand Down Expand Up @@ -106,7 +114,7 @@ func NewDecoder(options ...func(*Decoder) error) (*Decoder, error) {
return d, nil
}

func (d *Decoder) Decode(r io.Reader) (ex *Exif, err error) {
func (d *Decoder) Decode(r io.Reader) (ex *ExifInfo, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Exif failed: %v", r)
Expand Down Expand Up @@ -139,7 +147,7 @@ func (d *Decoder) Decode(r io.Reader) (ex *Exif, err error) {
return
}

ex = &Exif{Lat: lat, Long: long, Date: tm, Tags: walker.vals}
ex = &ExifInfo{Lat: lat, Long: long, Date: tm, Tags: walker.vals}

return
}
Expand Down

0 comments on commit fa80fe3

Please sign in to comment.