| 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 resources |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"path/filepath" |
| 19 |
"strconv" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/hugofs/hglob" |
| 23 |
"github.com/gohugoio/hugo/media" |
| 24 |
"github.com/gohugoio/hugo/resources/page/pagemeta" |
| 25 |
"github.com/gohugoio/hugo/resources/resource" |
| 26 |
|
| 27 |
"github.com/spf13/cast" |
| 28 |
|
| 29 |
maps0 "maps" |
| 30 |
|
| 31 |
"github.com/gohugoio/hugo/common/hmaps" |
| 32 |
"github.com/gohugoio/hugo/common/paths" |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
_ mediaTypeAssigner = (*genericResource)(nil) |
| 37 |
_ mediaTypeAssigner = (*imageResource)(nil) |
| 38 |
_ resource.Staler = (*genericResource)(nil) |
| 39 |
_ resource.NameNormalizedProvider = (*genericResource)(nil) |
| 40 |
) |
| 41 |
|
| 42 |
// metaAssigner allows updating metadata in resources that supports it. |
| 43 |
type metaAssigner interface { |
| 44 |
setTitle(title string) |
| 45 |
setName(name string) |
| 46 |
updateParams(params map[string]any) |
| 47 |
} |
| 48 |
|
| 49 |
// metaAssigner allows updating the media type in resources that supports it. |
| 50 |
type mediaTypeAssigner interface { |
| 51 |
setMediaType(mediaType media.Type) |
| 52 |
} |
| 53 |
|
| 54 |
const counterPlaceHolder = ":counter" |
| 55 |
|
| 56 |
var _ metaAssigner = (*metaResource)(nil) |
| 57 |
|
| 58 |
// metaResource is a resource with metadata that can be updated. |
| 59 |
type metaResource struct { |
| 60 |
changed bool |
| 61 |
title string |
| 62 |
name string |
| 63 |
params hmaps.Params |
| 64 |
} |
| 65 |
|
| 66 |
func (r *metaResource) Name() string { |
| 67 |
return r.name |
| 68 |
} |
| 69 |
|
| 70 |
func (r *metaResource) Title() string { |
| 71 |
return r.title |
| 72 |
} |
| 73 |
|
| 74 |
func (r *metaResource) Params() hmaps.Params { |
| 75 |
return r.params |
| 76 |
} |
| 77 |
|
| 78 |
func (r *metaResource) setTitle(title string) { |
| 79 |
r.title = title |
| 80 |
r.changed = true |
| 81 |
} |
| 82 |
|
| 83 |
func (r *metaResource) setName(name string) { |
| 84 |
r.name = name |
| 85 |
r.changed = true |
| 86 |
} |
| 87 |
|
| 88 |
func (r *metaResource) updateParams(params map[string]any) { |
| 89 |
if r.params == nil { |
| 90 |
r.params = make(map[string]any) |
| 91 |
} |
| 92 |
maps0.Copy(r.params, params) |
| 93 |
r.changed = true |
| 94 |
} |
| 95 |
|
| 96 |
// cloneWithMetadataFromResourceConfigIfNeeded clones the given resource with the given metadata if the resource supports it. |
| 97 |
func cloneWithMetadataFromResourceConfigIfNeeded(rc *pagemeta.ResourceConfig, r resource.Resource) resource.Resource { |
| 98 |
wmp, ok := r.(resource.WithResourceMetaProvider) |
| 99 |
if !ok { |
| 100 |
return r |
| 101 |
} |
| 102 |
|
| 103 |
if rc.Name == "" && rc.Title == "" && len(rc.Params) == 0 { |
| 104 |
// No metadata. |
| 105 |
return r |
| 106 |
} |
| 107 |
|
| 108 |
if rc.Title == "" { |
| 109 |
rc.Title = rc.Name |
| 110 |
} |
| 111 |
|
| 112 |
wrapped := &metaResource{ |
| 113 |
name: rc.Name, |
| 114 |
title: rc.Title, |
| 115 |
params: rc.Params, |
| 116 |
} |
| 117 |
|
| 118 |
return wmp.WithResourceMeta(wrapped) |
| 119 |
} |
| 120 |
|
| 121 |
// CloneWithMetadataFromMapIfNeeded clones the given resource with the given metadata if the resource supports it. |
| 122 |
// The counters map is shared across all resources in a bundle so that the :counter placeholder increments correctly. |
| 123 |
func CloneWithMetadataFromMapIfNeeded(m []map[string]any, r resource.Resource, counters map[string]int) resource.Resource { |
| 124 |
wmp, ok := r.(resource.WithResourceMetaProvider) |
| 125 |
if !ok { |
| 126 |
return r |
| 127 |
} |
| 128 |
|
| 129 |
wrapped := &metaResource{ |
| 130 |
name: r.Name(), |
| 131 |
title: r.Title(), |
| 132 |
params: r.Params(), |
| 133 |
} |
| 134 |
|
| 135 |
assignMetadata(m, wrapped, counters) |
| 136 |
if !wrapped.changed { |
| 137 |
return r |
| 138 |
} |
| 139 |
|
| 140 |
return wmp.WithResourceMeta(wrapped) |
| 141 |
} |
| 142 |
|
| 143 |
// AssignMetadata assigns the given metadata to those resources that supports updates |
| 144 |
// and matching by wildcard given in `src` using `filepath.Match` with lower cased values. |
| 145 |
// This assignment is additive, but the most specific match needs to be first. |
| 146 |
// The `name` and `title` metadata field support shell-matched collection it got a match in. |
| 147 |
// See https://golang.org/pkg/path/#Match |
| 148 |
func assignMetadata(metadata []map[string]any, ma *metaResource, counters map[string]int) error { |
| 149 |
var ( |
| 150 |
nameSet, titleSet bool |
| 151 |
nameCounter, titleCounter = 0, 0 |
| 152 |
nameCounterFound, titleCounterFound bool |
| 153 |
resourceSrcKey = strings.ToLower(ma.Name()) |
| 154 |
) |
| 155 |
|
| 156 |
for _, meta := range metadata { |
| 157 |
src, found := meta["src"] |
| 158 |
if !found { |
| 159 |
return fmt.Errorf("missing 'src' in metadata for resource") |
| 160 |
} |
| 161 |
|
| 162 |
srcKey := strings.ToLower(cast.ToString(src)) |
| 163 |
|
| 164 |
glob, err := hglob.GetGlob(srcKey) |
| 165 |
if err != nil { |
| 166 |
return fmt.Errorf("failed to match resource with metadata: %w", err) |
| 167 |
} |
| 168 |
|
| 169 |
match := glob.Match(resourceSrcKey) |
| 170 |
|
| 171 |
if match { |
| 172 |
if !nameSet { |
| 173 |
name, found := meta["name"] |
| 174 |
if found { |
| 175 |
name := cast.ToString(name) |
| 176 |
// Bundled resources in sub folders are relative paths with forward slashes. Make sure any renames also matches that format: |
| 177 |
name = paths.TrimLeading(filepath.ToSlash(name)) |
| 178 |
if !nameCounterFound { |
| 179 |
nameCounterFound = strings.Contains(name, counterPlaceHolder) |
| 180 |
} |
| 181 |
if nameCounterFound && nameCounter == 0 { |
| 182 |
counterKey := "name_" + srcKey |
| 183 |
nameCounter = counters[counterKey] + 1 |
| 184 |
counters[counterKey] = nameCounter |
| 185 |
} |
| 186 |
|
| 187 |
ma.setName(replaceResourcePlaceholders(name, nameCounter)) |
| 188 |
nameSet = true |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if !titleSet { |
| 193 |
title, found := meta["title"] |
| 194 |
if found { |
| 195 |
title := cast.ToString(title) |
| 196 |
if !titleCounterFound { |
| 197 |
titleCounterFound = strings.Contains(title, counterPlaceHolder) |
| 198 |
} |
| 199 |
if titleCounterFound && titleCounter == 0 { |
| 200 |
counterKey := "title_" + srcKey |
| 201 |
titleCounter = counters[counterKey] + 1 |
| 202 |
counters[counterKey] = titleCounter |
| 203 |
} |
| 204 |
ma.setTitle((replaceResourcePlaceholders(title, titleCounter))) |
| 205 |
titleSet = true |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
params, found := meta["params"] |
| 210 |
if found { |
| 211 |
m := hmaps.ToStringMap(params) |
| 212 |
// Needed for case insensitive fetching of params values |
| 213 |
hmaps.PrepareParams(m) |
| 214 |
ma.updateParams(m) |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return nil |
| 220 |
} |
| 221 |
|
| 222 |
func replaceResourcePlaceholders(in string, counter int) string { |
| 223 |
return strings.Replace(in, counterPlaceHolder, strconv.Itoa(counter), -1) |
| 224 |
} |