| 1 |
// Copyright 2019 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 resource |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/hmaps" |
| 20 |
"github.com/gohugoio/hugo/common/types" |
| 21 |
"github.com/gohugoio/hugo/langs" |
| 22 |
"github.com/gohugoio/hugo/media" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/hugio" |
| 25 |
) |
| 26 |
|
| 27 |
var ( |
| 28 |
_ ResourceDataProvider = (*resourceError)(nil) |
| 29 |
_ ResourceError = (*resourceError)(nil) |
| 30 |
) |
| 31 |
|
| 32 |
// Cloner is for internal use. |
| 33 |
type Cloner interface { |
| 34 |
Clone() Resource |
| 35 |
} |
| 36 |
|
| 37 |
// OriginProvider provides the original Resource if this is wrapped. |
| 38 |
// This is an internal Hugo interface and not meant for use in the templates. |
| 39 |
type OriginProvider interface { |
| 40 |
Origin() Resource |
| 41 |
GetFieldString(pattern string) (string, bool) |
| 42 |
} |
| 43 |
|
| 44 |
// NewResourceError creates a new ResourceError. |
| 45 |
func NewResourceError(err error, data any) ResourceError { |
| 46 |
if data == nil { |
| 47 |
data = map[string]any{} |
| 48 |
} |
| 49 |
return &resourceError{ |
| 50 |
error: err, |
| 51 |
data: data, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
type resourceError struct { |
| 56 |
error |
| 57 |
data any |
| 58 |
} |
| 59 |
|
| 60 |
// The data associated with this error. |
| 61 |
func (e *resourceError) Data() any { |
| 62 |
return e.data |
| 63 |
} |
| 64 |
|
| 65 |
// ResourceError is the error return from .Err in Resource in error situations. |
| 66 |
type ResourceError interface { |
| 67 |
error |
| 68 |
ResourceDataProvider |
| 69 |
} |
| 70 |
|
| 71 |
// Resource represents a linkable resource, i.e. a content page, image etc. |
| 72 |
type Resource interface { |
| 73 |
ResourceWithoutMeta |
| 74 |
ResourceMetaProvider |
| 75 |
} |
| 76 |
|
| 77 |
type ResourceWithoutMeta interface { |
| 78 |
ResourceTypeProvider |
| 79 |
MediaTypeProvider |
| 80 |
ResourceLinksProvider |
| 81 |
ResourceDataProvider |
| 82 |
} |
| 83 |
|
| 84 |
type ResourceTypeProvider interface { |
| 85 |
// ResourceType is the resource type. For most file types, this is the main |
| 86 |
// part of the MIME type, e.g. "image", "application", "text" etc. |
| 87 |
// For content pages, this value is "page". |
| 88 |
ResourceType() string |
| 89 |
} |
| 90 |
|
| 91 |
type ResourceTypesProvider interface { |
| 92 |
ResourceTypeProvider |
| 93 |
MediaTypeProvider |
| 94 |
} |
| 95 |
|
| 96 |
type MediaTypeProvider interface { |
| 97 |
// MediaType is this resource's MIME type. |
| 98 |
MediaType() media.Type |
| 99 |
} |
| 100 |
|
| 101 |
type ResourceLinksProvider interface { |
| 102 |
// Permalink represents the absolute link to this resource. |
| 103 |
Permalink() string |
| 104 |
|
| 105 |
// RelPermalink represents the host relative link to this resource. |
| 106 |
RelPermalink() string |
| 107 |
} |
| 108 |
|
| 109 |
// ResourceMetaProvider provides metadata about a resource. |
| 110 |
type ResourceMetaProvider interface { |
| 111 |
ResourceNameTitleProvider |
| 112 |
ResourceParamsProvider |
| 113 |
} |
| 114 |
|
| 115 |
type WithResourceMetaProvider interface { |
| 116 |
// WithResourceMeta creates a new Resource with the given metadata. |
| 117 |
// For internal use. |
| 118 |
WithResourceMeta(ResourceMetaProvider) Resource |
| 119 |
} |
| 120 |
|
| 121 |
type ResourceNameTitleProvider interface { |
| 122 |
// Name is the logical name of this resource. This can be set in the front matter |
| 123 |
// metadata for this resource. If not set, Hugo will assign a value. |
| 124 |
// This will in most cases be the base filename. |
| 125 |
// So, for the image "/some/path/sunset.jpg" this will be "sunset.jpg". |
| 126 |
// The value returned by this method will be used in the GetByPrefix and ByPrefix methods |
| 127 |
// on Resources. |
| 128 |
Name() string |
| 129 |
|
| 130 |
// Title returns the title if set in front matter. For content pages, this will be the expected value. |
| 131 |
Title() string |
| 132 |
} |
| 133 |
|
| 134 |
type NameNormalizedProvider interface { |
| 135 |
// NameNormalized is the normalized name of this resource. |
| 136 |
// For internal use (for now). |
| 137 |
NameNormalized() string |
| 138 |
} |
| 139 |
|
| 140 |
type ResourceParamsProvider interface { |
| 141 |
// Params set in front matter for this resource. |
| 142 |
Params() hmaps.Params |
| 143 |
} |
| 144 |
|
| 145 |
type ResourceDataProvider interface { |
| 146 |
// Resource specific data set by Hugo. |
| 147 |
// One example would be .Data.Integrity for fingerprinted resources. |
| 148 |
Data() any |
| 149 |
} |
| 150 |
|
| 151 |
// ResourcesLanguageMerger describes an interface for merging resources from a |
| 152 |
// different language. |
| 153 |
type ResourcesLanguageMerger interface { |
| 154 |
MergeByLanguage(other Resources) Resources |
| 155 |
|
| 156 |
// Needed for integration with the tpl package. |
| 157 |
// For internal use. |
| 158 |
MergeByLanguageInterface(other any) (any, error) |
| 159 |
} |
| 160 |
|
| 161 |
// Identifier identifies a resource. |
| 162 |
type Identifier interface { |
| 163 |
// Key is mostly for internal use and should be considered opaque. |
| 164 |
// This value may change between Hugo versions. |
| 165 |
Key() string |
| 166 |
} |
| 167 |
|
| 168 |
// TransientIdentifier identifies a transient resource. |
| 169 |
type TransientIdentifier interface { |
| 170 |
// TransientKey is mostly for internal use and should be considered opaque. |
| 171 |
// This value is implemented by transient resources where pointers may be short lived and |
| 172 |
// not suitable for use as a map keys. |
| 173 |
TransientKey() string |
| 174 |
} |
| 175 |
|
| 176 |
// ContentResource represents a Resource that provides a way to get to its content. |
| 177 |
// Most Resource types in Hugo implements this interface, including Page. |
| 178 |
type ContentResource interface { |
| 179 |
MediaType() media.Type |
| 180 |
ContentProvider |
| 181 |
} |
| 182 |
|
| 183 |
// ContentProvider provides Content. |
| 184 |
// This should be used with care, as it will read the file content into memory, but it |
| 185 |
// should be cached as effectively as possible by the implementation. |
| 186 |
type ContentProvider interface { |
| 187 |
// Content returns this resource's content. It will be equivalent to reading the content |
| 188 |
// that RelPermalink points to in the published folder. |
| 189 |
// The return type will be contextual, and should be what you would expect: |
| 190 |
// * Page: template.HTML |
| 191 |
// * JSON: String |
| 192 |
// * Etc. |
| 193 |
Content(context.Context) (any, error) |
| 194 |
} |
| 195 |
|
| 196 |
// ReadSeekCloserResource is a Resource that supports loading its content. |
| 197 |
type ReadSeekCloserResource interface { |
| 198 |
MediaType() media.Type |
| 199 |
hugio.ReadSeekCloserProvider |
| 200 |
} |
| 201 |
|
| 202 |
// LengthProvider is a Resource that provides a length |
| 203 |
// (typically the length of the content). |
| 204 |
type LengthProvider interface { |
| 205 |
Len(context.Context) int |
| 206 |
} |
| 207 |
|
| 208 |
// LanguageProvider is a Resource in a language. |
| 209 |
type LanguageProvider interface { |
| 210 |
Language() *langs.Language |
| 211 |
Lang() string |
| 212 |
} |
| 213 |
|
| 214 |
// TranslationKeyProvider connects translations of the same Resource. |
| 215 |
type TranslationKeyProvider interface { |
| 216 |
TranslationKey() string |
| 217 |
} |
| 218 |
|
| 219 |
// Staler controls stale state of a Resource. A stale resource should be discarded. |
| 220 |
type Staler interface { |
| 221 |
StaleMarker |
| 222 |
StaleInfo |
| 223 |
} |
| 224 |
|
| 225 |
// StaleMarker marks a Resource as stale. |
| 226 |
type StaleMarker interface { |
| 227 |
MarkStale() |
| 228 |
} |
| 229 |
|
| 230 |
// StaleInfo tells if a resource is marked as stale. |
| 231 |
type StaleInfo interface { |
| 232 |
StaleVersion() uint32 |
| 233 |
} |
| 234 |
|
| 235 |
// StaleVersion returns the StaleVersion for the given os, |
| 236 |
// or 0 if not set. |
| 237 |
func StaleVersion(os any) uint32 { |
| 238 |
if s, ok := os.(StaleInfo); ok { |
| 239 |
return s.StaleVersion() |
| 240 |
} |
| 241 |
return 0 |
| 242 |
} |
| 243 |
|
| 244 |
// StaleVersionSum calculates the sum of the StaleVersionSum for the given oss. |
| 245 |
func StaleVersionSum(oss ...any) uint32 { |
| 246 |
var version uint32 |
| 247 |
for _, o := range oss { |
| 248 |
if s, ok := o.(StaleInfo); ok && s.StaleVersion() > 0 { |
| 249 |
version += s.StaleVersion() |
| 250 |
} |
| 251 |
} |
| 252 |
return version |
| 253 |
} |
| 254 |
|
| 255 |
// MarkStale will mark any of the oses as stale, if possible. |
| 256 |
func MarkStale(os ...any) { |
| 257 |
for _, o := range os { |
| 258 |
if types.IsNil(o) { |
| 259 |
continue |
| 260 |
} |
| 261 |
if s, ok := o.(StaleMarker); ok { |
| 262 |
s.MarkStale() |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// UnmarshableResource represents a Resource that can be unmarshaled to some other format. |
| 268 |
type UnmarshableResource interface { |
| 269 |
ReadSeekCloserResource |
| 270 |
Identifier |
| 271 |
} |
| 272 |
|
| 273 |
type resourceTypesHolder struct { |
| 274 |
mediaType media.Type |
| 275 |
resourceType string |
| 276 |
} |
| 277 |
|
| 278 |
func (r resourceTypesHolder) MediaType() media.Type { |
| 279 |
return r.mediaType |
| 280 |
} |
| 281 |
|
| 282 |
func (r resourceTypesHolder) ResourceType() string { |
| 283 |
return r.resourceType |
| 284 |
} |
| 285 |
|
| 286 |
func NewResourceTypesProvider(mediaType media.Type, resourceType string) ResourceTypesProvider { |
| 287 |
return resourceTypesHolder{mediaType: mediaType, resourceType: resourceType} |
| 288 |
} |
| 289 |
|
| 290 |
// NameNormalizedOrName returns the normalized name if available, otherwise the name. |
| 291 |
func NameNormalizedOrName(r Resource) string { |
| 292 |
if nn, ok := r.(NameNormalizedProvider); ok { |
| 293 |
return nn.NameNormalized() |
| 294 |
} |
| 295 |
return r.Name() |
| 296 |
} |