Add reflect.Is{Page,Site,Resource,ImageResource}

Fixes #14307
This commit is contained in:
Bjørn Erik Pedersen
2025-12-27 11:20:33 +01:00
parent 7c19c196c3
commit b7bb557c6a
3 changed files with 105 additions and 0 deletions
+10
View File
@@ -400,6 +400,16 @@ func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
return img
}
// IsImage reports whether the given resource is an image that can be processed.
func IsImage(v any) bool {
r, ok := v.(*resourceAdapter)
if ok {
_, ok := r.target.(images.ImageResourceOps)
return ok
}
return false
}
func (r *resourceAdapter) publish() {
if r.publishOnce == nil {
return
+27
View File
@@ -15,6 +15,9 @@ package reflect
import (
"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
)
// New returns a new instance of the reflect-namespaced template functions.
@@ -34,3 +37,27 @@ func (ns *Namespace) IsMap(v any) bool {
func (ns *Namespace) IsSlice(v any) bool {
return hreflect.IsSlice(v)
}
// IsPage reports whether v is a Hugo Page.
func (ns *Namespace) IsPage(v any) bool {
_, ok := v.(page.Page)
return ok
}
// IsResource reports whether v is a Hugo Resource.
func (ns *Namespace) IsResource(v any) bool {
_, ok := v.(resource.Resource)
return ok
}
// IsSite reports whether v is a Hugo Site.
func (ns *Namespace) IsSite(v any) bool {
_, ok := v.(page.Site)
return ok
}
// IsImageResource reports whether v is an Hugo Image Resource.
// If this returns true, you may process it and get information about its width, height, etc.
func (ns *Namespace) IsImageResource(v any) bool {
return resources.IsImage(v)
}
+68
View File
@@ -0,0 +1,68 @@
// Copyright 2025 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package reflect_test
import (
"testing"
"github.com/gohugoio/hugo/hugolib"
)
func TestIs(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- assets/a.png --
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
-- assets/b.svg --
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
-- assets/c.txt --
This is a text file.
-- assets/d.avif --
AAAAHGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZgAAAOptZXRhAAAAAAAAACFoZGxyAAAAAAAAAABwaWN0AAAAAAAAAAAAAAAAAAAAAA5waXRtAAAAAAABAAAAImlsb2MAAAAAREAAAQABAAAAAAEOAAEAAAAAAAAAEgAAACNpaW5mAAAAAAABAAAAFWluZmUCAAAAAAEAAGF2MDEAAAAAamlwcnAAAABLaXBjbwAAABNjb2xybmNseAABAA0ABoAAAAAMYXYxQ4EgAgAAAAAUaXNwZQAAAAAAAAABAAAAAQAAABBwaXhpAAAAAAMICAgAAAAXaXBtYQAAAAAAAAABAAEEAYIDBAAAABptZGF0EgAKBzgABhAQ0GkyBRAAAAtA
-- layouts/home.html --
{{ $a := resources.Get "a.png" }}
{{ $a10 := $a.Fit "10x10" }}
{{ $b := resources.Get "b.svg" }}
{{ $c := resources.Get "c.txt" }}
{{ $d := resources.Get "d.avif" }}
PNG.ResourceType: {{ $a.ResourceType }}
SVG.ResourceType: {{ $b.ResourceType }}
Text.ResourceType: {{ $c.ResourceType }}
AVIF.ResourceType: {{ $d.ResourceType }}
IsSite: false: {{ reflect.IsSite . }}|true: {{ reflect.IsSite .Site }}|true: {{ reflect.IsSite site }}
IsPage: true: {{ reflect.IsPage . }}|false: {{ reflect.IsPage .Site }}|false: {{ reflect.IsPage site }}
IsResource: true: {{ reflect.IsResource . }}|true: {{ reflect.IsResource $a }}|true: {{ reflect.IsResource $b }}|true: {{ reflect.IsResource $c }}
IsImageResource: false: {{ reflect.IsImageResource . }}|true: {{ reflect.IsImageResource $a }}|true: {{ reflect.IsImageResource $a10 }}|false: {{ reflect.IsImageResource $b }}|false: {{ reflect.IsImageResource $c }}|false: {{ reflect.IsImageResource $d }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", `
PNG.ResourceType: image
SVG.ResourceType: image
Text.ResourceType: text
AVIF.ResourceType: image
IsSite: false: false|true: true|true: true
IsPage: true: true|false: false|false: false
IsResource: true: true|true: true|true: true|true: true
IsImageResource: false: false|true: true|true: true|false: false|false: false|false: false
`)
}