Debian Codegraph @

tpl/reflect/reflect.go

raw
1 // Copyright 2018 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
15
16 import (
17 "github.com/gohugoio/hugo/common/hreflect"
18 "github.com/gohugoio/hugo/resources"
19 "github.com/gohugoio/hugo/resources/images"
20 "github.com/gohugoio/hugo/resources/page"
21 "github.com/gohugoio/hugo/resources/resource"
22 )
23
24 // New returns a new instance of the reflect-namespaced template functions.
25 func New() *Namespace {
26 return &Namespace{}
27 }
28
29 // Namespace provides template functions for the "reflect" namespace.
30 type Namespace struct{}
31
32 // IsMap reports whether v is a map.
33 func (ns *Namespace) IsMap(v any) bool {
34 return hreflect.IsMap(v)
35 }
36
37 // IsSlice reports whether v is a slice.
38 func (ns *Namespace) IsSlice(v any) bool {
39 return hreflect.IsSlice(v)
40 }
41
42 // IsPage reports whether v is a Hugo Page.
43 func (ns *Namespace) IsPage(v any) bool {
44 _, ok := v.(page.Page)
45 return ok
46 }
47
48 // IsResource reports whether v is a Hugo Resource.
49 func (ns *Namespace) IsResource(v any) bool {
50 _, ok := v.(resource.Resource)
51 return ok
52 }
53
54 // IsSite reports whether v is a Hugo Site.
55 func (ns *Namespace) IsSite(v any) bool {
56 _, ok := v.(page.Site)
57 return ok
58 }
59
60 // IsImageResource reports whether v is a Image Resource.
61 func (ns *Namespace) IsImageResource(v any) bool {
62 return resources.ResolveImageOpsSupport(v) > images.ImageResourceTypeNone
63 }
64
65 // IsImageResourceWithMeta reports whether v is a Image Resource that supports at least the image metadata operations Width and Height and Meta (for e.g. Exif).
66 // This will return true for AVIF, HEIF and HEIC image resources, even if we don't yet support image operations like Resize, Crop, etc. on these formats.
67 func (ns *Namespace) IsImageResourceWithMeta(v any) bool {
68 return resources.ResolveImageOpsSupport(v) >= images.ImageResourceTypeMetaOnly
69 }
70
71 // IsImageResourceProcessable reports whether v is a Image Resource that supports all image processing operations like Resize, Crop, etc. in addition to the metadata operations.
72 func (ns *Namespace) IsImageResourceProcessable(v any) bool {
73 return resources.ResolveImageOpsSupport(v) >= images.ImageResourceTypeProcessable
74 }