github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/page/page.go (about)

     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 page contains the core interfaces and types for the Page resource,
    15  // a core component in Hugo.
    16  package page
    17  
    18  import (
    19  	"html/template"
    20  
    21  	"github.com/gohugoio/hugo/identity"
    22  
    23  	"github.com/bep/gitmap"
    24  	"github.com/gohugoio/hugo/config"
    25  	"github.com/gohugoio/hugo/tpl"
    26  
    27  	"github.com/gohugoio/hugo/common/hugo"
    28  	"github.com/gohugoio/hugo/common/maps"
    29  	"github.com/gohugoio/hugo/compare"
    30  	"github.com/gohugoio/hugo/hugofs/files"
    31  
    32  	"github.com/gohugoio/hugo/navigation"
    33  	"github.com/gohugoio/hugo/related"
    34  	"github.com/gohugoio/hugo/resources/resource"
    35  	"github.com/gohugoio/hugo/source"
    36  )
    37  
    38  // Clear clears any global package state.
    39  func Clear() error {
    40  	spc.clear()
    41  	return nil
    42  }
    43  
    44  // AlternativeOutputFormatsProvider provides alternative output formats for a
    45  // Page.
    46  type AlternativeOutputFormatsProvider interface {
    47  	// AlternativeOutputFormats gives the alternative output formats for the
    48  	// current output.
    49  	// Note that we use the term "alternative" and not "alternate" here, as it
    50  	// does not necessarily replace the other format, it is an alternative representation.
    51  	AlternativeOutputFormats() OutputFormats
    52  }
    53  
    54  // AuthorProvider provides author information.
    55  type AuthorProvider interface {
    56  	Author() Author
    57  	Authors() AuthorList
    58  }
    59  
    60  // ChildCareProvider provides accessors to child resources.
    61  type ChildCareProvider interface {
    62  	Pages() Pages
    63  
    64  	// RegularPages returns a list of pages of kind 'Page'.
    65  	// In Hugo 0.57 we changed the Pages method so it returns all page
    66  	// kinds, even sections. If you want the old behaviour, you can
    67  	// use RegularPages.
    68  	RegularPages() Pages
    69  
    70  	// RegularPagesRecursive returns all regular pages below the current
    71  	// section.
    72  	RegularPagesRecursive() Pages
    73  
    74  	Resources() resource.Resources
    75  }
    76  
    77  // ContentProvider provides the content related values for a Page.
    78  type ContentProvider interface {
    79  	Content() (interface{}, error)
    80  	Plain() string
    81  	PlainWords() []string
    82  	Summary() template.HTML
    83  	Truncated() bool
    84  	FuzzyWordCount() int
    85  	WordCount() int
    86  	ReadingTime() int
    87  	Len() int
    88  }
    89  
    90  // FileProvider provides the source file.
    91  type FileProvider interface {
    92  	File() source.File
    93  }
    94  
    95  // GetPageProvider provides the GetPage method.
    96  type GetPageProvider interface {
    97  	// GetPage looks up a page for the given ref.
    98  	//    {{ with .GetPage "blog" }}{{ .Title }}{{ end }}
    99  	//
   100  	// This will return nil when no page could be found, and will return
   101  	// an error if the ref is ambiguous.
   102  	GetPage(ref string) (Page, error)
   103  
   104  	// GetPageWithTemplateInfo is for internal use only.
   105  	GetPageWithTemplateInfo(info tpl.Info, ref string) (Page, error)
   106  }
   107  
   108  // GitInfoProvider provides Git info.
   109  type GitInfoProvider interface {
   110  	GitInfo() *gitmap.GitInfo
   111  }
   112  
   113  // InSectionPositioner provides section navigation.
   114  type InSectionPositioner interface {
   115  	NextInSection() Page
   116  	PrevInSection() Page
   117  }
   118  
   119  // InternalDependencies is considered an internal interface.
   120  type InternalDependencies interface {
   121  	GetRelatedDocsHandler() *RelatedDocsHandler
   122  }
   123  
   124  // OutputFormatsProvider provides the OutputFormats of a Page.
   125  type OutputFormatsProvider interface {
   126  	OutputFormats() OutputFormats
   127  }
   128  
   129  // Page is the core interface in Hugo.
   130  type Page interface {
   131  	ContentProvider
   132  	TableOfContentsProvider
   133  	PageWithoutContent
   134  }
   135  
   136  // PageMetaProvider provides page metadata, typically provided via front matter.
   137  type PageMetaProvider interface {
   138  	// The 4 page dates
   139  	resource.Dated
   140  
   141  	// Aliases forms the base for redirects generation.
   142  	Aliases() []string
   143  
   144  	// BundleType returns the bundle type: "leaf", "branch" or an empty string if it is none.
   145  	// See https://gohugo.io/content-management/page-bundles/
   146  	BundleType() files.ContentClass
   147  
   148  	// A configured description.
   149  	Description() string
   150  
   151  	// Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag.
   152  	Draft() bool
   153  
   154  	// IsHome returns whether this is the home page.
   155  	IsHome() bool
   156  
   157  	// Configured keywords.
   158  	Keywords() []string
   159  
   160  	// The Page Kind. One of page, home, section, taxonomy, term.
   161  	Kind() string
   162  
   163  	// The configured layout to use to render this page. Typically set in front matter.
   164  	Layout() string
   165  
   166  	// The title used for links.
   167  	LinkTitle() string
   168  
   169  	// IsNode returns whether this is an item of one of the list types in Hugo,
   170  	// i.e. not a regular content
   171  	IsNode() bool
   172  
   173  	// IsPage returns whether this is a regular content
   174  	IsPage() bool
   175  
   176  	// Param looks for a param in Page and then in Site config.
   177  	Param(key interface{}) (interface{}, error)
   178  
   179  	// Path gets the relative path, including file name and extension if relevant,
   180  	// to the source of this Page. It will be relative to any content root.
   181  	Path() string
   182  
   183  	// The slug, typically defined in front matter.
   184  	Slug() string
   185  
   186  	// This page's language code. Will be the same as the site's.
   187  	Lang() string
   188  
   189  	// IsSection returns whether this is a section
   190  	IsSection() bool
   191  
   192  	// Section returns the first path element below the content root.
   193  	Section() string
   194  
   195  	// Returns a slice of sections (directories if it's a file) to this
   196  	// Page.
   197  	SectionsEntries() []string
   198  
   199  	// SectionsPath is SectionsEntries joined with a /.
   200  	SectionsPath() string
   201  
   202  	// Sitemap returns the sitemap configuration for this page.
   203  	Sitemap() config.Sitemap
   204  
   205  	// Type is a discriminator used to select layouts etc. It is typically set
   206  	// in front matter, but will fall back to the root section.
   207  	Type() string
   208  
   209  	// The configured weight, used as the first sort value in the default
   210  	// page sort if non-zero.
   211  	Weight() int
   212  }
   213  
   214  // PageRenderProvider provides a way for a Page to render content.
   215  type PageRenderProvider interface {
   216  	Render(layout ...string) (template.HTML, error)
   217  	RenderString(args ...interface{}) (template.HTML, error)
   218  }
   219  
   220  // PageWithoutContent is the Page without any of the content methods.
   221  type PageWithoutContent interface {
   222  	RawContentProvider
   223  	resource.Resource
   224  	PageMetaProvider
   225  	resource.LanguageProvider
   226  
   227  	// For pages backed by a file.
   228  	FileProvider
   229  
   230  	GitInfoProvider
   231  
   232  	// Output formats
   233  	OutputFormatsProvider
   234  	AlternativeOutputFormatsProvider
   235  
   236  	// Tree navigation
   237  	ChildCareProvider
   238  	TreeProvider
   239  
   240  	// Horizontal navigation
   241  	InSectionPositioner
   242  	PageRenderProvider
   243  	PaginatorProvider
   244  	Positioner
   245  	navigation.PageMenusProvider
   246  
   247  	// TODO(bep)
   248  	AuthorProvider
   249  
   250  	// Page lookups/refs
   251  	GetPageProvider
   252  	RefProvider
   253  
   254  	resource.TranslationKeyProvider
   255  	TranslationsProvider
   256  
   257  	SitesProvider
   258  
   259  	// Helper methods
   260  	ShortcodeInfoProvider
   261  	compare.Eqer
   262  	maps.Scratcher
   263  	RelatedKeywordsProvider
   264  
   265  	// GetTerms gets the terms of a given taxonomy,
   266  	// e.g. GetTerms("categories")
   267  	GetTerms(taxonomy string) Pages
   268  
   269  	// Used in change/dependency tracking.
   270  	identity.Provider
   271  
   272  	DeprecatedWarningPageMethods
   273  }
   274  
   275  // Positioner provides next/prev navigation.
   276  type Positioner interface {
   277  	Next() Page
   278  	Prev() Page
   279  
   280  	// Deprecated: Use Prev. Will be removed in Hugo 0.57
   281  	PrevPage() Page
   282  
   283  	// Deprecated: Use Next. Will be removed in Hugo 0.57
   284  	NextPage() Page
   285  }
   286  
   287  // RawContentProvider provides the raw, unprocessed content of the page.
   288  type RawContentProvider interface {
   289  	RawContent() string
   290  }
   291  
   292  // RefProvider provides the methods needed to create reflinks to pages.
   293  type RefProvider interface {
   294  	Ref(argsm map[string]interface{}) (string, error)
   295  	RefFrom(argsm map[string]interface{}, source interface{}) (string, error)
   296  	RelRef(argsm map[string]interface{}) (string, error)
   297  	RelRefFrom(argsm map[string]interface{}, source interface{}) (string, error)
   298  }
   299  
   300  // RelatedKeywordsProvider allows a Page to be indexed.
   301  type RelatedKeywordsProvider interface {
   302  	// Make it indexable as a related.Document
   303  	RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error)
   304  }
   305  
   306  // ShortcodeInfoProvider provides info about the shortcodes in a Page.
   307  type ShortcodeInfoProvider interface {
   308  	// HasShortcode return whether the page has a shortcode with the given name.
   309  	// This method is mainly motivated with the Hugo Docs site's need for a list
   310  	// of pages with the `todo` shortcode in it.
   311  	HasShortcode(name string) bool
   312  }
   313  
   314  // SitesProvider provide accessors to get sites.
   315  type SitesProvider interface {
   316  	Site() Site
   317  	Sites() Sites
   318  }
   319  
   320  // TableOfContentsProvider provides the table of contents for a Page.
   321  type TableOfContentsProvider interface {
   322  	TableOfContents() template.HTML
   323  }
   324  
   325  // TranslationsProvider provides access to any translations.
   326  type TranslationsProvider interface {
   327  
   328  	// IsTranslated returns whether this content file is translated to
   329  	// other language(s).
   330  	IsTranslated() bool
   331  
   332  	// AllTranslations returns all translations, including the current Page.
   333  	AllTranslations() Pages
   334  
   335  	// Translations returns the translations excluding the current Page.
   336  	Translations() Pages
   337  }
   338  
   339  // TreeProvider provides section tree navigation.
   340  type TreeProvider interface {
   341  
   342  	// IsAncestor returns whether the current page is an ancestor of the given
   343  	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
   344  	IsAncestor(other interface{}) (bool, error)
   345  
   346  	// CurrentSection returns the page's current section or the page itself if home or a section.
   347  	// Note that this will return nil for pages that is not regular, home or section pages.
   348  	CurrentSection() Page
   349  
   350  	// IsDescendant returns whether the current page is a descendant of the given
   351  	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
   352  	IsDescendant(other interface{}) (bool, error)
   353  
   354  	// FirstSection returns the section on level 1 below home, e.g. "/docs".
   355  	// For the home page, this will return itself.
   356  	FirstSection() Page
   357  
   358  	// InSection returns whether the given page is in the current section.
   359  	// Note that this will always return false for pages that are
   360  	// not either regular, home or section pages.
   361  	InSection(other interface{}) (bool, error)
   362  
   363  	// Parent returns a section's parent section or a page's section.
   364  	// To get a section's subsections, see Page's Sections method.
   365  	Parent() Page
   366  
   367  	// Sections returns this section's subsections, if any.
   368  	// Note that for non-sections, this method will always return an empty list.
   369  	Sections() Pages
   370  
   371  	// Page returns a reference to the Page itself, kept here mostly
   372  	// for legacy reasons.
   373  	Page() Page
   374  }
   375  
   376  // DeprecatedWarningPageMethods lists deprecated Page methods that will trigger
   377  // a WARNING if invoked.
   378  // This was added in Hugo 0.55.
   379  type DeprecatedWarningPageMethods interface {
   380  	source.FileWithoutOverlap
   381  	DeprecatedWarningPageMethods1
   382  }
   383  
   384  type DeprecatedWarningPageMethods1 interface {
   385  	IsDraft() bool
   386  	Hugo() hugo.Info
   387  	LanguagePrefix() string
   388  	GetParam(key string) interface{}
   389  	RSSLink() template.URL
   390  	URL() string
   391  }
   392  
   393  // Move here to trigger ERROR instead of WARNING.
   394  // TODO(bep) create wrappers and put into the Page once it has some methods.
   395  type DeprecatedErrorPageMethods interface {
   396  }