diff options
Diffstat (limited to 'resources/resource/resourcetypes.go')
-rw-r--r-- | resources/resource/resourcetypes.go | 98 |
1 files changed, 79 insertions, 19 deletions
diff --git a/resources/resource/resourcetypes.go b/resources/resource/resourcetypes.go index 120d753e4..5a5839735 100644 --- a/resources/resource/resourcetypes.go +++ b/resources/resource/resourcetypes.go @@ -14,6 +14,7 @@ package resource import ( + "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/common/hugio" @@ -27,19 +28,32 @@ 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 + ResourceTypesProvider + ResourceLinksProvider + ResourceMetaProvider + ResourceParamsProvider + ResourceDataProvider +} - // RelPermalink represents the host relative link to this resource. - RelPermalink() string +type ResourceTypesProvider interface { + // MediaType is this resource's MIME type. + MediaType() media.Type // 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 +} +type ResourceLinksProvider interface { + // Permalink represents the absolute link to this resource. + Permalink() string + + // RelPermalink represents the host relative link to this resource. + RelPermalink() string +} + +type ResourceMetaProvider interface { // 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. @@ -50,20 +64,17 @@ type Resource interface { // 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{} - +type ResourceParamsProvider 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 +type ResourceDataProvider interface { + // Resource specific data set by Hugo. + // One example would be.Data.Digest for fingerprinted resources. + Data() interface{} } // ResourcesLanguageMerger describes an interface for merging resources from a @@ -81,11 +92,15 @@ type Identifier interface { // 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 + MediaType() media.Type + ContentProvider +} +// ContentProvider provides Content. +// 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 ContentProvider interface { // 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: @@ -101,6 +116,51 @@ type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error) // ReadSeekCloserResource is a Resource that supports loading its content. type ReadSeekCloserResource interface { - resourceBase + MediaType() media.Type ReadSeekCloser() (hugio.ReadSeekCloser, error) } + +// LengthProvider is a Resource that provides a length +// (typically the length of the content). +type LengthProvider interface { + Len() int +} + +// LanguageProvider is a Resource in a language. +type LanguageProvider interface { + Language() *langs.Language +} + +// TranslationKeyProvider connects translations of the same Resource. +type TranslationKeyProvider interface { + TranslationKey() string +} + +type resourceTypesHolder struct { + mediaType media.Type + resourceType string +} + +func (r resourceTypesHolder) MediaType() media.Type { + return r.mediaType +} + +func (r resourceTypesHolder) ResourceType() string { + return r.resourceType +} + +func NewResourceTypesProvider(mediaType media.Type, resourceType string) ResourceTypesProvider { + return resourceTypesHolder{mediaType: mediaType, resourceType: resourceType} +} + +type languageHolder struct { + lang *langs.Language +} + +func (l languageHolder) Language() *langs.Language { + return l.lang +} + +func NewLanguageProvider(lang *langs.Language) LanguageProvider { + return languageHolder{lang: lang} +} |