From: Bjørn Erik Pedersen Date: Sat, 27 Dec 2025 10:20:33 +0000 (+0100) Subject: Add reflect.Is{Page,Site,Resource,ImageResource} X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=b7bb557c6a0862f8f5a62d068e668a5ec530a8c9;p=brevno-suite%2Fhugo Add reflect.Is{Page,Site,Resource,ImageResource} Fixes #14307 --- diff --git a/resources/transform.go b/resources/transform.go index 9d03ec1ff..e404ac0d4 100644 --- a/resources/transform.go +++ b/resources/transform.go @@ -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 diff --git a/tpl/reflect/reflect.go b/tpl/reflect/reflect.go index c19c8c178..73f0b18b2 100644 --- a/tpl/reflect/reflect.go +++ b/tpl/reflect/reflect.go @@ -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) +} diff --git a/tpl/reflect/reflect_integration_test.go b/tpl/reflect/reflect_integration_test.go new file mode 100644 index 000000000..cd4e23564 --- /dev/null +++ b/tpl/reflect/reflect_integration_test.go @@ -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 -- + + + +-- 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 +`) +}