mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
`)
|
||||
}
|
||||
Reference in New Issue
Block a user