]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Add reflect.Is{Page,Site,Resource,ImageResource}
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 27 Dec 2025 10:20:33 +0000 (11:20 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 30 Dec 2025 15:00:19 +0000 (16:00 +0100)
Fixes #14307

resources/transform.go
tpl/reflect/reflect.go
tpl/reflect/reflect_integration_test.go [new file with mode: 0644]

index 9d03ec1ffa0bd01bb2b2b909b74a73cad61e1c5e..e404ac0d4a44ec30787d79bfb0a7698d030ff222 100644 (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
index c19c8c178a8fe5c9107ba3f4cade488edfe6377b..73f0b18b2462a82870952df93e64d2f52682acda 100644 (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)
+}
diff --git a/tpl/reflect/reflect_integration_test.go b/tpl/reflect/reflect_integration_test.go
new file mode 100644 (file)
index 0000000..cd4e235
--- /dev/null
@@ -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
+`)
+}