| 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" |
| 19 |
"sync" |
| 20 |
|
| 21 |
"github.com/bep/helpers/maphelpers" |
| 22 |
"github.com/gohugoio/hugo/config" |
| 23 |
"github.com/gohugoio/hugo/config/allconfig" |
| 24 |
"github.com/gohugoio/hugo/internal/warpc" |
| 25 |
"github.com/gohugoio/hugo/output" |
| 26 |
"github.com/gohugoio/hugo/resources/internal" |
| 27 |
"github.com/gohugoio/hugo/resources/jsconfig" |
| 28 |
"github.com/gohugoio/hugo/resources/page/pagemeta" |
| 29 |
|
| 30 |
"github.com/gohugoio/hugo/common/herrors" |
| 31 |
"github.com/gohugoio/hugo/common/hexec" |
| 32 |
"github.com/gohugoio/hugo/common/hsync" |
| 33 |
"github.com/gohugoio/hugo/common/loggers" |
| 34 |
"github.com/gohugoio/hugo/common/paths" |
| 35 |
"github.com/gohugoio/hugo/common/types" |
| 36 |
|
| 37 |
"github.com/gohugoio/hugo/identity" |
| 38 |
|
| 39 |
"github.com/gohugoio/hugo/helpers" |
| 40 |
"github.com/gohugoio/hugo/resources/postpub" |
| 41 |
|
| 42 |
"github.com/gohugoio/hugo/cache/dynacache" |
| 43 |
"github.com/gohugoio/hugo/cache/filecache" |
| 44 |
"github.com/gohugoio/hugo/media" |
| 45 |
"github.com/gohugoio/hugo/resources/images" |
| 46 |
"github.com/gohugoio/hugo/resources/page" |
| 47 |
"github.com/gohugoio/hugo/resources/resource" |
| 48 |
) |
| 49 |
|
| 50 |
func NewSpec( |
| 51 |
s *helpers.PathSpec, |
| 52 |
common *SpecCommon, // may be nil |
| 53 |
wasmDispatchers *warpc.Dispatchers, |
| 54 |
fileCaches filecache.Caches, |
| 55 |
memCache *dynacache.Cache, |
| 56 |
incr identity.Incrementer, |
| 57 |
logger loggers.Logger, |
| 58 |
errorHandler herrors.ErrorSender, |
| 59 |
execHelper *hexec.Exec, |
| 60 |
buildClosers types.CloseAdder, |
| 61 |
rebuilder Rebuilder, |
| 62 |
) (*Spec, error) { |
| 63 |
conf := s.Cfg.GetConfig().(*allconfig.Config) |
| 64 |
imgConfig := conf.Imaging |
| 65 |
|
| 66 |
imagesDebugl := logger.DebugCommand("images") |
| 67 |
imagesWarnl := logger.WarnCommand("images") |
| 68 |
|
| 69 |
imaging, err := images.NewImageProcessor(imagesDebugl, imagesWarnl, wasmDispatchers, imgConfig) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
|
| 74 |
if incr == nil { |
| 75 |
incr = &identity.IncrementByOne{} |
| 76 |
} |
| 77 |
|
| 78 |
if logger == nil { |
| 79 |
logger = loggers.NewDefault() |
| 80 |
} |
| 81 |
|
| 82 |
permalinks, err := page.NewPermalinkExpander(s.URLize, conf.Permalinks) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
if common == nil { |
| 88 |
common = &SpecCommon{ |
| 89 |
incr: incr, |
| 90 |
FileCaches: fileCaches, |
| 91 |
PostBuildAssets: &PostBuildAssets{ |
| 92 |
PostProcessResources: maphelpers.NewConcurrentMap[string, postpub.PostPublishedResource](), |
| 93 |
JSConfigBuilder: jsconfig.NewBuilder(), |
| 94 |
}, |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
rs := &Spec{ |
| 99 |
PathSpec: s, |
| 100 |
Logger: logger, |
| 101 |
ErrorSender: errorHandler, |
| 102 |
BuildClosers: buildClosers, |
| 103 |
Rebuilder: rebuilder, |
| 104 |
Imaging: imaging, |
| 105 |
ImageCache: newImageCache( |
| 106 |
fileCaches.ImageCache(), |
| 107 |
memCache, |
| 108 |
s, |
| 109 |
), |
| 110 |
ExecHelper: execHelper, |
| 111 |
|
| 112 |
Permalinks: permalinks, |
| 113 |
|
| 114 |
SpecCommon: common, |
| 115 |
} |
| 116 |
|
| 117 |
rs.ResourceCache = newResourceCache(rs, memCache) |
| 118 |
|
| 119 |
return rs, nil |
| 120 |
} |
| 121 |
|
| 122 |
type Rebuilder interface { |
| 123 |
identity.SignalRebuilder |
| 124 |
identity.IsRebuildProvider |
| 125 |
} |
| 126 |
|
| 127 |
type Spec struct { |
| 128 |
*helpers.PathSpec |
| 129 |
|
| 130 |
Logger loggers.Logger |
| 131 |
ErrorSender herrors.ErrorSender |
| 132 |
BuildClosers types.CloseAdder |
| 133 |
Rebuilder Rebuilder |
| 134 |
|
| 135 |
Permalinks page.PermalinkExpander |
| 136 |
|
| 137 |
ImageCache *ImageCache |
| 138 |
|
| 139 |
// Holds default filter settings etc. |
| 140 |
Imaging *images.ImageProcessor |
| 141 |
|
| 142 |
ExecHelper *hexec.Exec |
| 143 |
|
| 144 |
*SpecCommon |
| 145 |
} |
| 146 |
|
| 147 |
// The parts of Spec that's common for all sites. |
| 148 |
type SpecCommon struct { |
| 149 |
incr identity.Incrementer |
| 150 |
ResourceCache *ResourceCache |
| 151 |
FileCaches filecache.Caches |
| 152 |
|
| 153 |
// Assets used after the build is done. |
| 154 |
// This is shared between all sites. |
| 155 |
*PostBuildAssets |
| 156 |
} |
| 157 |
|
| 158 |
type PostBuildAssets struct { |
| 159 |
PostProcessResources *maphelpers.ConcurrentMap[string, postpub.PostPublishedResource] |
| 160 |
JSConfigBuilder *jsconfig.Builder |
| 161 |
} |
| 162 |
|
| 163 |
func (r *Spec) NewResourceWrapperFromResourceConfig(rc *pagemeta.ResourceConfig) (resource.Resource, error) { |
| 164 |
content := rc.Content |
| 165 |
switch r := content.Value.(type) { |
| 166 |
case resource.Resource: |
| 167 |
return cloneWithMetadataFromResourceConfigIfNeeded(rc, r), nil |
| 168 |
default: |
| 169 |
return nil, fmt.Errorf("failed to create resource for path %q, expected a resource.Resource, got %T", rc.PathInfo.Path(), content.Value) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// NewResource creates a new Resource from the given ResourceSourceDescriptor. |
| 174 |
func (r *Spec) NewResource(rd ResourceSourceDescriptor) (resource.Resource, error) { |
| 175 |
if err := rd.init(r); err != nil { |
| 176 |
return nil, err |
| 177 |
} |
| 178 |
|
| 179 |
dir, name := path.Split(rd.TargetPath) |
| 180 |
dir = paths.ToSlashPreserveLeading(dir) |
| 181 |
if dir == "/" { |
| 182 |
dir = "" |
| 183 |
} |
| 184 |
rp := internal.ResourcePaths{ |
| 185 |
File: name, |
| 186 |
Dir: dir, |
| 187 |
BaseDirTarget: rd.BasePathTargetPath, |
| 188 |
BaseDirLink: rd.BasePathRelPermalink, |
| 189 |
TargetBasePaths: rd.TargetBasePaths, |
| 190 |
} |
| 191 |
|
| 192 |
isImage := rd.MediaType.MainType == "image" |
| 193 |
var imgFormat images.Format |
| 194 |
var imgOpsSupport images.ImageResourceType |
| 195 |
if isImage { |
| 196 |
imgFormat, imgOpsSupport = images.ImageFormatFromMediaSubType(rd.MediaType.SubType) |
| 197 |
} |
| 198 |
|
| 199 |
gr := &genericResource{ |
| 200 |
Staler: &AtomicStaler{}, |
| 201 |
h: &resourceHash{}, |
| 202 |
publishInit: &hsync.OnceMore{}, |
| 203 |
keyInit: &sync.Once{}, |
| 204 |
includeHashInKey: isImage || rd.IncludeHashInKey, |
| 205 |
paths: rp, |
| 206 |
spec: r, |
| 207 |
sd: rd, |
| 208 |
params: rd.Params, |
| 209 |
name: rd.NameOriginal, |
| 210 |
title: rd.Title, |
| 211 |
} |
| 212 |
|
| 213 |
if imgOpsSupport >= images.ImageResourceTypeMetaOnly { |
| 214 |
ir := newImageResource(images.NewImage(imgFormat, r.Imaging, nil, gr), gr) |
| 215 |
return newResourceAdapter(gr.spec, rd.LazyPublish, ir), nil |
| 216 |
} |
| 217 |
|
| 218 |
return newResourceAdapter(gr.spec, rd.LazyPublish, gr), nil |
| 219 |
} |
| 220 |
|
| 221 |
func (r *Spec) MediaTypes() media.Types { |
| 222 |
return r.Cfg.GetConfigSection("mediaTypes").(media.Types) |
| 223 |
} |
| 224 |
|
| 225 |
func (r *Spec) OutputFormats() output.Formats { |
| 226 |
return r.Cfg.GetConfigSection("outputFormats").(output.Formats) |
| 227 |
} |
| 228 |
|
| 229 |
func (r *Spec) BuildConfig() config.BuildConfig { |
| 230 |
return r.Cfg.GetConfigSection("build").(config.BuildConfig) |
| 231 |
} |
| 232 |
|
| 233 |
func (s *Spec) String() string { |
| 234 |
return "spec" |
| 235 |
} |