Skip to content

Commit b7bb557

Browse files
committed
Add reflect.Is{Page,Site,Resource,ImageResource}
Fixes #14307
1 parent 7c19c19 commit b7bb557

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

‎resources/transform.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,16 @@ func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
400400
return img
401401
}
402402

403+
// IsImage reports whether the given resource is an image that can be processed.
404+
func IsImage(v any) bool {
405+
r, ok := v.(*resourceAdapter)
406+
if ok {
407+
_, ok := r.target.(images.ImageResourceOps)
408+
return ok
409+
}
410+
return false
411+
}
412+
403413
func (r *resourceAdapter) publish() {
404414
if r.publishOnce == nil {
405415
return

‎tpl/reflect/reflect.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ package reflect
1515

1616
import (
1717
"github.com/gohugoio/hugo/common/hreflect"
18+
"github.com/gohugoio/hugo/resources"
19+
"github.com/gohugoio/hugo/resources/page"
20+
"github.com/gohugoio/hugo/resources/resource"
1821
)
1922

2023
// New returns a new instance of the reflect-namespaced template functions.
@@ -34,3 +37,27 @@ func (ns *Namespace) IsMap(v any) bool {
3437
func (ns *Namespace) IsSlice(v any) bool {
3538
return hreflect.IsSlice(v)
3639
}
40+
41+
// IsPage reports whether v is a Hugo Page.
42+
func (ns *Namespace) IsPage(v any) bool {
43+
_, ok := v.(page.Page)
44+
return ok
45+
}
46+
47+
// IsResource reports whether v is a Hugo Resource.
48+
func (ns *Namespace) IsResource(v any) bool {
49+
_, ok := v.(resource.Resource)
50+
return ok
51+
}
52+
53+
// IsSite reports whether v is a Hugo Site.
54+
func (ns *Namespace) IsSite(v any) bool {
55+
_, ok := v.(page.Site)
56+
return ok
57+
}
58+
59+
// IsImageResource reports whether v is an Hugo Image Resource.
60+
// If this returns true, you may process it and get information about its width, height, etc.
61+
func (ns *Namespace) IsImageResource(v any) bool {
62+
return resources.IsImage(v)
63+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package reflect_test
15+
16+
import (
17+
"testing"
18+
19+
"github.com/gohugoio/hugo/hugolib"
20+
)
21+
22+
func TestIs(t *testing.T) {
23+
t.Parallel()
24+
25+
files := `
26+
-- hugo.toml --
27+
-- assets/a.png --
28+
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
29+
-- assets/b.svg --
30+
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
31+
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
32+
</svg>
33+
-- assets/c.txt --
34+
This is a text file.
35+
-- assets/d.avif --
36+
AAAAHGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZgAAAOptZXRhAAAAAAAAACFoZGxyAAAAAAAAAABwaWN0AAAAAAAAAAAAAAAAAAAAAA5waXRtAAAAAAABAAAAImlsb2MAAAAAREAAAQABAAAAAAEOAAEAAAAAAAAAEgAAACNpaW5mAAAAAAABAAAAFWluZmUCAAAAAAEAAGF2MDEAAAAAamlwcnAAAABLaXBjbwAAABNjb2xybmNseAABAA0ABoAAAAAMYXYxQ4EgAgAAAAAUaXNwZQAAAAAAAAABAAAAAQAAABBwaXhpAAAAAAMICAgAAAAXaXBtYQAAAAAAAAABAAEEAYIDBAAAABptZGF0EgAKBzgABhAQ0GkyBRAAAAtA
37+
-- layouts/home.html --
38+
{{ $a := resources.Get "a.png" }}
39+
{{ $a10 := $a.Fit "10x10" }}
40+
{{ $b := resources.Get "b.svg" }}
41+
{{ $c := resources.Get "c.txt" }}
42+
{{ $d := resources.Get "d.avif" }}
43+
PNG.ResourceType: {{ $a.ResourceType }}
44+
SVG.ResourceType: {{ $b.ResourceType }}
45+
Text.ResourceType: {{ $c.ResourceType }}
46+
AVIF.ResourceType: {{ $d.ResourceType }}
47+
IsSite: false: {{ reflect.IsSite . }}|true: {{ reflect.IsSite .Site }}|true: {{ reflect.IsSite site }}
48+
IsPage: true: {{ reflect.IsPage . }}|false: {{ reflect.IsPage .Site }}|false: {{ reflect.IsPage site }}
49+
IsResource: true: {{ reflect.IsResource . }}|true: {{ reflect.IsResource $a }}|true: {{ reflect.IsResource $b }}|true: {{ reflect.IsResource $c }}
50+
IsImageResource: false: {{ reflect.IsImageResource . }}|true: {{ reflect.IsImageResource $a }}|true: {{ reflect.IsImageResource $a10 }}|false: {{ reflect.IsImageResource $b }}|false: {{ reflect.IsImageResource $c }}|false: {{ reflect.IsImageResource $d }}
51+
52+
53+
54+
`
55+
56+
b := hugolib.Test(t, files)
57+
58+
b.AssertFileContent("public/index.html", `
59+
PNG.ResourceType: image
60+
SVG.ResourceType: image
61+
Text.ResourceType: text
62+
AVIF.ResourceType: image
63+
IsSite: false: false|true: true|true: true
64+
IsPage: true: true|false: false|false: false
65+
IsResource: true: true|true: true|true: true|true: true
66+
IsImageResource: false: false|true: true|true: true|false: false|false: false|false: false
67+
`)
68+
}

0 commit comments

Comments
 (0)