diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-12-21 16:21:13 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-12-23 10:02:42 +0100 |
commit | 822dc627a1cfdf1f97882f27761675ac6ace7669 (patch) | |
tree | b453158c329495fa59dc38374eb8296995ba0ce0 /resource | |
parent | 43f9df0194d229805d80b13c9e38a7a0fec12cf4 (diff) |
tpl/transform: Add transform.Unmarshal func
Fixes #5428
Diffstat (limited to 'resource')
-rw-r--r-- | resource/resource.go | 26 | ||||
-rw-r--r-- | resource/resource_test.go | 1 | ||||
-rw-r--r-- | resource/transform.go | 10 |
3 files changed, 31 insertions, 6 deletions
diff --git a/resource/resource.go b/resource/resource.go index a8f9dde06..0f5a43648 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -50,6 +50,7 @@ var ( _ ResourcesLanguageMerger = (*Resources)(nil) _ permalinker = (*genericResource)(nil) _ collections.Slicer = (*genericResource)(nil) + _ Identifier = (*genericResource)(nil) ) var noData = make(map[string]interface{}) @@ -76,6 +77,8 @@ type Cloner interface { // 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 @@ -87,9 +90,6 @@ type Resource interface { // For content pages, this value is "page". ResourceType() string - // MediaType is this resource's MIME type. - MediaType() media.Type - // 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. @@ -109,6 +109,13 @@ type Resource interface { 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 { @@ -121,12 +128,17 @@ 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 { - Resource + resourceBase // Content returns this resource's content. It will be equivalent to reading the content // that RelPermalink points to in the published folder. @@ -143,7 +155,7 @@ type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error) // ReadSeekCloserResource is a Resource that supports loading its content. type ReadSeekCloserResource interface { - Resource + resourceBase ReadSeekCloser() (hugio.ReadSeekCloser, error) } @@ -716,6 +728,10 @@ func (l *genericResource) RelPermalink() string { return l.relPermalinkFor(l.relTargetDirFile.path()) } +func (l *genericResource) Key() string { + return l.relTargetDirFile.path() +} + func (l *genericResource) relPermalinkFor(target string) string { return l.relPermalinkForRel(target, false) diff --git a/resource/resource_test.go b/resource/resource_test.go index b76f0a604..b3f6035b6 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -50,6 +50,7 @@ func TestGenericResourceWithLinkFacory(t *testing.T) { assert.Equal("https://example.com/foo/foo.css", r.Permalink()) assert.Equal("/foo/foo.css", r.RelPermalink()) + assert.Equal("foo.css", r.Key()) assert.Equal("css", r.ResourceType()) } diff --git a/resource/transform.go b/resource/transform.go index 796c7ee23..bd59d0658 100644 --- a/resource/transform.go +++ b/resource/transform.go @@ -38,6 +38,7 @@ var ( _ ContentResource = (*transformedResource)(nil) _ ReadSeekCloserResource = (*transformedResource)(nil) _ collections.Slicer = (*transformedResource)(nil) + _ Identifier = (*transformedResource)(nil) ) func (s *Spec) Transform(r Resource, t ResourceTransformation) (Resource, error) { @@ -249,6 +250,13 @@ func (r *transformedResource) MediaType() media.Type { return m } +func (r *transformedResource) Key() string { + if err := r.initTransform(false, false); err != nil { + return "" + } + return r.linker.relPermalinkFor(r.Target) +} + func (r *transformedResource) Permalink() string { if err := r.initTransform(false, true); err != nil { return "" @@ -481,8 +489,8 @@ func (r *transformedResource) transform(setContent, publish bool) (err error) { } return nil - } + func (r *transformedResource) initTransform(setContent, publish bool) error { r.transformInit.Do(func() { r.published = publish |