diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-01-02 11:58:32 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-01-02 14:25:37 +0100 |
commit | ce8a09a4c0661dece931ab1173e4f09e8e04aa38 (patch) | |
tree | 870372211e342312d0ab3034ee03285c6f698f9d | |
parent | 669ada436787311cc5d02dae5b88e60a09adda58 (diff) |
resources: Move resource interfaces into its own package
-rw-r--r-- | deps/deps.go | 8 | ||||
-rw-r--r-- | hugolib/collections.go | 2 | ||||
-rw-r--r-- | hugolib/page.go | 2 | ||||
-rw-r--r-- | hugolib/page_output.go | 2 | ||||
-rw-r--r-- | hugolib/page_resource.go | 2 | ||||
-rw-r--r-- | hugolib/pagebundler_handlers.go | 7 | ||||
-rw-r--r-- | hugolib/pages_language_merge_test.go | 2 | ||||
-rw-r--r-- | hugolib/resource_chain_test.go | 2 | ||||
-rw-r--r-- | hugolib/site.go | 4 | ||||
-rw-r--r-- | resources/image.go (renamed from resource/image.go) | 14 | ||||
-rw-r--r-- | resources/image_cache.go (renamed from resource/image_cache.go) | 4 | ||||
-rw-r--r-- | resources/image_test.go (renamed from resource/image_test.go) | 4 | ||||
-rw-r--r-- | resources/internal/glob.go | 48 | ||||
-rw-r--r-- | resources/resource.go (renamed from resource/resource.go) | 266 | ||||
-rw-r--r-- | resources/resource/resources.go | 123 | ||||
-rw-r--r-- | resources/resource/resourcetypes.go | 106 | ||||
-rw-r--r-- | resources/resource_cache.go (renamed from resource/resource_cache.go) | 18 | ||||
-rw-r--r-- | resources/resource_factories/bundler/bundler.go (renamed from resource/resource_factories/bundler/bundler.go) | 19 | ||||
-rw-r--r-- | resources/resource_factories/create/create.go (renamed from resource/resource_factories/create/create.go) | 17 | ||||
-rw-r--r-- | resources/resource_metadata.go (renamed from resource/resource_metadata.go) | 11 | ||||
-rw-r--r-- | resources/resource_metadata_test.go (renamed from resource/resource_metadata_test.go) | 11 | ||||
-rw-r--r-- | resources/resource_test.go (renamed from resource/resource_test.go) | 22 | ||||
-rw-r--r-- | resources/resource_transformers/integrity/integrity.go (renamed from resource/resource_transformers/integrity/integrity.go) | 13 | ||||
-rw-r--r-- | resources/resource_transformers/minifier/minify.go (renamed from resource/resource_transformers/minifier/minify.go) | 15 | ||||
-rw-r--r-- | resources/resource_transformers/postcss/postcss.go (renamed from resource/resource_transformers/postcss/postcss.go) | 15 | ||||
-rw-r--r-- | resources/resource_transformers/templates/execute_as_template.go (renamed from resource/resource_transformers/templates/execute_as_template.go) | 15 | ||||
-rw-r--r-- | resources/resource_transformers/tocss/scss/client.go (renamed from resource/resource_transformers/tocss/scss/client.go) | 12 | ||||
-rw-r--r-- | resources/resource_transformers/tocss/scss/tocss.go (renamed from resource/resource_transformers/tocss/scss/tocss.go) | 4 | ||||
-rw-r--r-- | resources/resource_transformers/tocss/scss/tocss_notavailable.go (renamed from resource/resource_transformers/tocss/scss/tocss_notavailable.go) | 4 | ||||
-rw-r--r-- | resources/smartcrop.go (renamed from resource/smartcrop.go) | 4 | ||||
-rw-r--r-- | resources/testdata/1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg (renamed from resource/testdata/1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg) | bin | 90587 -> 90587 bytes | |||
-rw-r--r-- | resources/testdata/circle.svg (renamed from resource/testdata/circle.svg) | 0 | ||||
-rw-r--r-- | resources/testdata/gohugoio.png (renamed from resource/testdata/gohugoio.png) | bin | 73886 -> 73886 bytes | |||
-rw-r--r-- | resources/testdata/sub/gohugoio2.png (renamed from resource/testdata/sub/gohugoio2.png) | bin | 73886 -> 73886 bytes | |||
-rw-r--r-- | resources/testdata/sunset.jpg (renamed from resource/testdata/sunset.jpg) | bin | 90587 -> 90587 bytes | |||
-rw-r--r-- | resources/testhelpers_test.go (renamed from resource/testhelpers_test.go) | 7 | ||||
-rw-r--r-- | resources/transform.go (renamed from resource/transform.go) | 25 | ||||
-rw-r--r-- | resources/transform_test.go (renamed from resource/transform_test.go) | 4 | ||||
-rw-r--r-- | tpl/resources/resources.go | 18 | ||||
-rw-r--r-- | tpl/transform/unmarshal.go | 4 | ||||
-rw-r--r-- | tpl/transform/unmarshal_test.go | 4 |
41 files changed, 461 insertions, 377 deletions
diff --git a/deps/deps.go b/deps/deps.go index 7fba0e153..628019961 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -16,7 +16,7 @@ import ( "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/metrics" "github.com/gohugoio/hugo/output" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/source" "github.com/gohugoio/hugo/tpl" jww "github.com/spf13/jwalterweatherman" @@ -52,7 +52,7 @@ type Deps struct { SourceSpec *source.SourceSpec `json:"-"` // The Resource Spec to use - ResourceSpec *resource.Spec + ResourceSpec *resources.Spec // The configuration to use Cfg config.Provider `json:"-"` @@ -214,7 +214,7 @@ func New(cfg DepsCfg) (*Deps, error) { return nil, errors.WithMessage(err, "failed to create file caches from configuration") } - resourceSpec, err := resource.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes) + resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes) if err != nil { return nil, err } @@ -281,7 +281,7 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er // The resource cache is global so reuse. // TODO(bep) clean up these inits. resourceCache := d.ResourceSpec.ResourceCache - d.ResourceSpec, err = resource.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, cfg.OutputFormats, cfg.MediaTypes) + d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, cfg.OutputFormats, cfg.MediaTypes) if err != nil { return nil, err } diff --git a/hugolib/collections.go b/hugolib/collections.go index b9992c425..cf75d3732 100644 --- a/hugolib/collections.go +++ b/hugolib/collections.go @@ -16,7 +16,7 @@ package hugolib import ( "fmt" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources/resource" "github.com/gohugoio/hugo/common/collections" ) diff --git a/hugolib/page.go b/hugolib/page.go index 9f09dc9bd..71070d1e8 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -35,7 +35,7 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugolib/pagemeta" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources/resource" "github.com/gohugoio/hugo/output" "github.com/mitchellh/mapstructure" diff --git a/hugolib/page_output.go b/hugolib/page_output.go index 9a6531207..0a3eef9a6 100644 --- a/hugolib/page_output.go +++ b/hugolib/page_output.go @@ -24,7 +24,7 @@ import ( "github.com/gohugoio/hugo/tpl" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources/resource" "github.com/gohugoio/hugo/media" diff --git a/hugolib/page_resource.go b/hugolib/page_resource.go index 808a692da..201076e8b 100644 --- a/hugolib/page_resource.go +++ b/hugolib/page_resource.go @@ -14,7 +14,7 @@ package hugolib import ( - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources/resource" ) var ( diff --git a/hugolib/pagebundler_handlers.go b/hugolib/pagebundler_handlers.go index fdff76e24..2df1f8765 100644 --- a/hugolib/pagebundler_handlers.go +++ b/hugolib/pagebundler_handlers.go @@ -22,7 +22,8 @@ import ( "strings" "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources" + "github.com/gohugoio/hugo/resources/resource" ) var ( @@ -255,7 +256,7 @@ func (c *contentHandlers) parsePage(h contentHandler) contentHandler { // Assign metadata from front matter if set if len(p.resourcesMetadata) > 0 { - resource.AssignMetadata(p.resourcesMetadata, p.Resources...) + resources.AssignMetadata(p.resourcesMetadata, p.Resources...) } } @@ -309,7 +310,7 @@ func (c *contentHandlers) createResource() contentHandler { } resource, err := c.s.ResourceSpec.New( - resource.ResourceSourceDescriptor{ + resources.ResourceSourceDescriptor{ TargetPathBuilder: ctx.parentPage.subResourceTargetPathFactory, SourceFile: ctx.source, RelTargetFilename: ctx.target, diff --git a/hugolib/pages_language_merge_test.go b/hugolib/pages_language_merge_test.go index 6706af3bc..efcfbf04b 100644 --- a/hugolib/pages_language_merge_test.go +++ b/hugolib/pages_language_merge_test.go @@ -17,7 +17,7 @@ import ( "fmt" "testing" - "github.com/gohugoio/hugo/resource" + "github.com/gohugoio/hugo/resources/resource" "github.com/stretchr/testify/require" ) diff --git a/hugolib/resource_chain_test.go b/hugolib/resource_chain_test.go index 0fe9c70c1..f53ab4966 100644 --- a/hugolib/resource_chain_test.go +++ b/hugolib/resource_chain_test.go @@ -25,7 +25,7 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/resource/resource_transformers/tocss/scss" + "github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss" ) func TestSCSSWithIncludePaths(t *testing.T) { diff --git a/hugolib/site.go b/hugolib/site.go index 9c225b332..43b398b70 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -39,7 +39,6 @@ import ( "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/publisher" - "github.com/gohugoio/hugo/resource" _errors "github.com/pkg/errors" "github.com/gohugoio/hugo/langs" @@ -62,6 +61,7 @@ import ( "github.com/gohugoio/hugo/hugolib/pagemeta" "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/related" + "github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/source" "github.com/gohugoio/hugo/tpl" "github.com/spf13/afero" @@ -760,7 +760,7 @@ func (s *Site) processPartial(events []fsnotify.Event) (whatChanged, error) { cachePartitions := make([]string, len(events)) for i, ev := range events { - cachePartitions[i] = resource.ResourceKeyPartition(ev.Name) + cachePartitions[i] = resources.ResourceKeyPartition(ev.Name) if s.isContentDirEvent(ev) { logger.Println("Source changed", ev) diff --git a/resource/image.go b/resources/image.go index 6dea4cbb8..d46facac5 100644 --- a/resource/image.go +++ b/resources/image.go @@ -1,4 +1,4 @@ -// Copyright 2017-present The Hugo Authors. All rights reserved. +// Copyright 2019 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. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package resource +package resources import ( "errors" @@ -27,6 +27,8 @@ import ( "strings" "sync" + "github.com/gohugoio/hugo/resources/resource" + _errors "github.com/pkg/errors" "github.com/disintegration/imaging" @@ -43,9 +45,9 @@ import ( ) var ( - _ Resource = (*Image)(nil) - _ Source = (*Image)(nil) - _ Cloner = (*Image)(nil) + _ resource.Resource = (*Image)(nil) + _ resource.Source = (*Image)(nil) + _ resource.Cloner = (*Image)(nil) ) // Imaging contains default image processing configuration. This will be fetched @@ -146,7 +148,7 @@ func (i *Image) Height() int { } // WithNewBase implements the Cloner interface. -func (i *Image) WithNewBase(base string) Resource { +func (i *Image) WithNewBase(base string) resource.Resource { return &Image{ imaging: i.imaging, format: i.format, diff --git a/resource/image_cache.go b/resources/image_cache.go index 8cc626caf..58be839b3 100644 --- a/resource/image_cache.go +++ b/resources/image_cache.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2019 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. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package resource +package resources import ( "fmt" diff --git a/resource/image_test.go b/resources/image_test.go index 07eab5d56..ffa482296 100644 --- a/resource/image_test.go +++ b/resources/image_test.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2019 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. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package resource +package resources import ( "fmt" diff --git a/resources/internal/glob.go b/resources/internal/glob.go new file mode 100644 index 000000000..a87a23f13 --- /dev/null +++ b/resources/internal/glob.go @@ -0,0 +1,48 @@ +// Copyright 2019 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 internal + +import ( + "strings" + "sync" + + "github.com/gobwas/glob" +) + +var ( + globCache = make(map[string]glob.Glob) + globMu sync.RWMutex +) + +func GetGlob(pattern string) (glob.Glob, error) { + var g glob.Glob + + globMu.RLock() + g, found := globCache[pattern] + globMu.RUnlock() + if !found { + var err error + g, err = glob.Compile(strings.ToLower(pattern), '/') + if err != nil { + return nil, err + } + + globMu.Lock() + globCache[pattern] = g + globMu.Unlock() + } + + return g, nil + +} diff --git a/resource/resource.go b/resources/resource.go index 0f5a43648..742903e80 100644 --- a/resource/resource.go +++ b/resources/resource.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2019 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. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package resource +package resources import ( "fmt" @@ -24,6 +24,8 @@ import ( "strings" "sync" + "github.com/gohugoio/hugo/media" + "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/tpl" "github.com/pkg/errors" @@ -32,35 +34,28 @@ import ( "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/loggers" + "github.com/gohugoio/hugo/resources/resource" "github.com/spf13/afero" - "github.com/gobwas/glob" "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/source" ) var ( - _ ContentResource = (*genericResource)(nil) - _ ReadSeekCloserResource = (*genericResource)(nil) - _ Resource = (*genericResource)(nil) - _ Source = (*genericResource)(nil) - _ Cloner = (*genericResource)(nil) - _ ResourcesLanguageMerger = (*Resources)(nil) - _ permalinker = (*genericResource)(nil) - _ collections.Slicer = (*genericResource)(nil) - _ Identifier = (*genericResource)(nil) + _ resource.ContentResource = (*genericResource)(nil) + _ resource.ReadSeekCloserResource = (*genericResource)(nil) + _ resource.Resource = (*genericResource)(nil) + _ resource.Source = (*genericResource)(nil) + _ resource.Cloner = (*genericResource)(nil) + _ resource.ResourcesLanguageMerger = (*resource.Resources)(nil) + _ permalinker = (*genericResource)(nil) + _ collections.Slicer = (*genericResource)(nil) + _ resource.Identifier = (*genericResource)(nil) ) var noData = make(map[string]interface{}) -// Source is an internal template and not meant for use in the templates. It -// may change without notice. -type Source interface { - Publish() error -} - type permalinker interface { relPermalinkFor(target string) string permalinkFor(target string) string @@ -69,215 +64,6 @@ type permalinker interface { targetPath() string } -// Cloner is an internal template and not meant for use in the templates. It -// may change without notice. -type Cloner interface { - WithNewBase(base string) Resource -} - -// Resource represents a linkable resource, i.e. a content page, image etc. -type Resource interface { - resourceBase - - // Permalink represents the absolute link to this resource. - Permalink() string - - // RelPermalink represents the host relative link to this resource. - RelPermalink() string - - // ResourceType is the resource type. For most file types, this is the main - // part of the MIME type, e.g. "image", "application", "text" etc. - // For content pages, this value is "page". - ResourceType() string - - // Name is the logical name of this resource. This can be set in the front matter - // metadata for this resource. If not set, Hugo will assign a value. - // This will in most cases be the base filename. - // So, for the image "/some/path/sunset.jpg" this will be "sunset.jpg". - // The value returned by this method will be used in the GetByPrefix and ByPrefix methods - // on Resources. - Name() string - - // Title returns the title if set in front matter. For content pages, this will be the expected value. - Title() string - - // Resource specific data set by Hugo. - // One example would be.Data.Digest for fingerprinted resources. - Data() interface{} - - // Params set in front matter for this resource. - Params() map[string]interface{} -} - -// resourceBase pulls out the minimal set of operations to define a Resource, -// to simplify testing etc. -type resourceBase interface { - // MediaType is this resource's MIME type. - MediaType() media.Type -} - -// ResourcesLanguageMerger describes an interface for merging resources from a -// different language. -type ResourcesLanguageMerger interface { - MergeByLanguage(other Resources) Resources - // Needed for integration with the tpl package. - MergeByLanguageInterface(other interface{}) (interface{}, error) -} - -type translatedResource interface { - TranslationKey() string -} - -// Identifier identifies a resource. -type Identifier interface { - Key() string -} - -// ContentResource represents a Resource that provides a way to get to its content. -// Most Resource types in Hugo implements this interface, including Page. -// This should be used with care, as it will read the file content into memory, but it -// should be cached as effectively as possible by the implementation. -type ContentResource interface { - resourceBase - - // Content returns this resource's content. It will be equivalent to reading the content - // that RelPermalink points to in the published folder. - // The return type will be contextual, and should be what you would expect: - // * Page: template.HTML - // * JSON: String - // * Etc. - Content() (interface{}, error) -} - -// OpenReadSeekCloser allows setting some other way (than reading from a filesystem) -// to open or create a ReadSeekCloser. -type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error) - -// ReadSeekCloserResource is a Resource that supports loading its content. -type ReadSeekCloserResource interface { - resourceBase - ReadSeekCloser() (hugio.ReadSeekCloser, error) -} - -// Resources represents a slice of resources, which can be a mix of different types. -// I.e. both pages and images etc. -type Resources []Resource - -// ResourcesConverter converts a given slice of Resource objects to Resources. -type ResourcesConverter interface { - ToResources() Resources -} - -// ByType returns resources of a given resource type (ie. "image"). -func (r Resources) ByType(tp string) Resources { - var filtered Resources - - for _, resource := range r { - if resource.ResourceType() == tp { - filtered = append(filtered, resource) - } - } - return filtered -} - -// GetMatch finds the first Resource matching the given pattern, or nil if none found. -// See Match for a more complete explanation about the rules used. -func (r Resources) GetMatch(pattern string) Resource { - g, err := getGlob(pattern) - if err != nil { - return nil - } - - for _, resource := range r { - if g.Match(strings.ToLower(resource.Name())) { - return resource - } - } - - return nil -} - -// Match gets all reso |