github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/page/site.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
    15  
    16  import (
    17  	"html/template"
    18  	"time"
    19  
    20  	"github.com/gohugoio/hugo/common/maps"
    21  	"github.com/gohugoio/hugo/config/privacy"
    22  	"github.com/gohugoio/hugo/config/services"
    23  	"github.com/gohugoio/hugo/identity"
    24  	"github.com/gohugoio/hugo/tpl"
    25  
    26  	"github.com/gohugoio/hugo/config"
    27  
    28  	"github.com/gohugoio/hugo/common/hugo"
    29  	"github.com/gohugoio/hugo/langs"
    30  	"github.com/gohugoio/hugo/navigation"
    31  )
    32  
    33  // Site represents a site. There can be multople sites in a multilingual setup.
    34  type Site interface {
    35  	// Returns the Language configured for this Site.
    36  	Language() *langs.Language
    37  
    38  	// Returns all the languages configured for all sites.
    39  	Languages() langs.Languages
    40  
    41  	GetPage(ref ...string) (Page, error)
    42  
    43  	// AllPages returns all pages for all languages.
    44  	AllPages() Pages
    45  
    46  	// Returns all the regular Pages in this Site.
    47  	RegularPages() Pages
    48  
    49  	// Returns all Pages in this Site.
    50  	Pages() Pages
    51  
    52  	// Returns all the top level sections.
    53  	Sections() Pages
    54  
    55  	// A shortcut to the home
    56  	Home() Page
    57  
    58  	// Returns true if we're running in a server.
    59  	// Deprecated: use hugo.IsServer instead
    60  	IsServer() bool
    61  
    62  	// Returns the server port.
    63  	ServerPort() int
    64  
    65  	// Returns the configured title for this Site.
    66  	Title() string
    67  
    68  	// Returns the configured language code for this Site.
    69  	// Deprecated: Use .Language.LanguageCode instead.
    70  	LanguageCode() string
    71  
    72  	// Returns the configured copyright information for this Site.
    73  	Copyright() string
    74  
    75  	// Returns all Sites for all languages.
    76  	Sites() Sites
    77  
    78  	// Returns Site currently rendering.
    79  	Current() Site
    80  
    81  	// Returns a struct with some information about the build.
    82  	Hugo() hugo.HugoInfo
    83  
    84  	// Returns the BaseURL for this Site.
    85  	BaseURL() template.URL
    86  
    87  	// Returns a taxonomy map.
    88  	Taxonomies() TaxonomyList
    89  
    90  	// Returns the last modification date of the content.
    91  	LastChange() time.Time
    92  
    93  	// Returns the Menus for this site.
    94  	Menus() navigation.Menus
    95  
    96  	// The main sections in the site.
    97  	MainSections() []string
    98  
    99  	// Returns the Params configured for this site.
   100  	Params() maps.Params
   101  
   102  	// Param is a convenience method to do lookups in Params.
   103  	Param(key any) (any, error)
   104  
   105  	// Returns a map of all the data inside /data.
   106  	Data() map[string]any
   107  
   108  	// Returns the site config.
   109  	Config() SiteConfig
   110  
   111  	// Returns the identity of this site.
   112  	// This is for internal use only.
   113  	GetIdentity() identity.Identity
   114  
   115  	// Author is deprecated and will be removed in a future release.
   116  	Author() map[string]interface{}
   117  
   118  	// Authors is deprecated and will be removed in a future release.
   119  	Authors() AuthorList
   120  
   121  	// Returns the social links for this site.
   122  	Social() map[string]string
   123  
   124  	// Deprecated: Use Config().Services.GoogleAnalytics instead.
   125  	GoogleAnalytics() string
   126  
   127  	// Deprecated: Use Config().Privacy.Disqus instead.
   128  	DisqusShortname() string
   129  
   130  	// For internal use only.
   131  	GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error)
   132  
   133  	// BuildDrafts is deprecated and will be removed in a future release.
   134  	BuildDrafts() bool
   135  
   136  	// IsMultiLingual reports whether this site is configured with more than one language.
   137  	IsMultiLingual() bool
   138  
   139  	// LanguagePrefix returns the language prefix for this site.
   140  	LanguagePrefix() string
   141  
   142  	// Deprecated. Use site.Home.OutputFormats.Get "rss" instead.
   143  	RSSLink() template.URL
   144  }
   145  
   146  // Sites represents an ordered list of sites (languages).
   147  type Sites []Site
   148  
   149  // First is a convenience method to get the first Site, i.e. the main language.
   150  func (s Sites) First() Site {
   151  	if len(s) == 0 {
   152  		return nil
   153  	}
   154  	return s[0]
   155  }
   156  
   157  type siteWrapper struct {
   158  	s Site
   159  }
   160  
   161  func WrapSite(s Site) Site {
   162  	if s == nil {
   163  		panic("Site is nil")
   164  	}
   165  	return &siteWrapper{s: s}
   166  }
   167  
   168  func (s *siteWrapper) Social() map[string]string {
   169  	return s.s.Social()
   170  }
   171  
   172  func (s *siteWrapper) Author() map[string]interface{} {
   173  	return s.s.Author()
   174  }
   175  func (s *siteWrapper) Authors() AuthorList {
   176  	return AuthorList{}
   177  }
   178  
   179  // Deprecated: Use .Site.Config.Services.GoogleAnalytics.ID instead
   180  func (s *siteWrapper) GoogleAnalytics() string {
   181  	return s.s.GoogleAnalytics()
   182  }
   183  
   184  func (s *siteWrapper) GetPage(ref ...string) (Page, error) {
   185  	return s.s.GetPage(ref...)
   186  }
   187  
   188  func (s *siteWrapper) Language() *langs.Language {
   189  	return s.s.Language()
   190  }
   191  
   192  func (s *siteWrapper) Languages() langs.Languages {
   193  	return s.s.Languages()
   194  }
   195  
   196  func (s *siteWrapper) AllPages() Pages {
   197  	return s.s.AllPages()
   198  }
   199  
   200  func (s *siteWrapper) RegularPages() Pages {
   201  	return s.s.RegularPages()
   202  }
   203  
   204  func (s *siteWrapper) Pages() Pages {
   205  	return s.s.Pages()
   206  }
   207  
   208  func (s *siteWrapper) Sections() Pages {
   209  	return s.s.Sections()
   210  }
   211  
   212  func (s *siteWrapper) Home() Page {
   213  	return s.s.Home()
   214  }
   215  
   216  // Deprecated: use hugo.IsServer instead
   217  func (s *siteWrapper) IsServer() bool {
   218  	return s.s.IsServer()
   219  }
   220  
   221  func (s *siteWrapper) ServerPort() int {
   222  	return s.s.ServerPort()
   223  }
   224  
   225  func (s *siteWrapper) Title() string {
   226  	return s.s.Title()
   227  }
   228  
   229  func (s *siteWrapper) LanguageCode() string {
   230  	return s.s.LanguageCode()
   231  }
   232  
   233  func (s *siteWrapper) Copyright() string {
   234  	return s.s.Copyright()
   235  }
   236  
   237  func (s *siteWrapper) Sites() Sites {
   238  	return s.s.Sites()
   239  }
   240  
   241  func (s *siteWrapper) Current() Site {
   242  	return s.s.Current()
   243  }
   244  
   245  func (s *siteWrapper) Config() SiteConfig {
   246  	return s.s.Config()
   247  }
   248  
   249  func (s *siteWrapper) Hugo() hugo.HugoInfo {
   250  	return s.s.Hugo()
   251  }
   252  
   253  func (s *siteWrapper) BaseURL() template.URL {
   254  	return s.s.BaseURL()
   255  }
   256  
   257  func (s *siteWrapper) Taxonomies() TaxonomyList {
   258  	return s.s.Taxonomies()
   259  }
   260  
   261  func (s *siteWrapper) LastChange() time.Time {
   262  	return s.s.LastChange()
   263  }
   264  
   265  func (s *siteWrapper) Menus() navigation.Menus {
   266  	return s.s.Menus()
   267  }
   268  
   269  func (s *siteWrapper) MainSections() []string {
   270  	return s.s.MainSections()
   271  }
   272  
   273  func (s *siteWrapper) Params() maps.Params {
   274  	return s.s.Params()
   275  }
   276  
   277  func (s *siteWrapper) Param(key any) (any, error) {
   278  	return s.s.Param(key)
   279  }
   280  
   281  func (s *siteWrapper) Data() map[string]any {
   282  	return s.s.Data()
   283  }
   284  
   285  func (s *siteWrapper) GetIdentity() identity.Identity {
   286  	return s.s.GetIdentity()
   287  }
   288  
   289  func (s *siteWrapper) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error) {
   290  	return s.s.GetPageWithTemplateInfo(info, ref...)
   291  }
   292  
   293  func (s *siteWrapper) BuildDrafts() bool {
   294  	return s.s.BuildDrafts()
   295  }
   296  
   297  func (s *siteWrapper) IsMultiLingual() bool {
   298  	return s.s.IsMultiLingual()
   299  }
   300  
   301  // Deprecated: Use .Site.Config.Services.Disqus.Shortname instead
   302  func (s *siteWrapper) DisqusShortname() string {
   303  	return s.s.DisqusShortname()
   304  }
   305  
   306  func (s *siteWrapper) LanguagePrefix() string {
   307  	return s.s.LanguagePrefix()
   308  }
   309  
   310  func (s *siteWrapper) RSSLink() template.URL {
   311  	return s.s.RSSLink()
   312  }
   313  
   314  type testSite struct {
   315  	h hugo.HugoInfo
   316  	l *langs.Language
   317  }
   318  
   319  func (s testSite) Author() map[string]interface{} {
   320  	return nil
   321  }
   322  func (s testSite) Authors() AuthorList {
   323  	return AuthorList{}
   324  }
   325  
   326  func (s testSite) Social() map[string]string {
   327  	return make(map[string]string)
   328  }
   329  
   330  func (t testSite) Hugo() hugo.HugoInfo {
   331  	return t.h
   332  }
   333  
   334  func (t testSite) ServerPort() int {
   335  	return 1313
   336  }
   337  
   338  func (testSite) LastChange() (t time.Time) {
   339  	return
   340  }
   341  
   342  func (t testSite) Title() string {
   343  	return "foo"
   344  }
   345  
   346  func (t testSite) LanguageCode() string {
   347  	return t.l.Lang
   348  }
   349  
   350  func (t testSite) Copyright() string {
   351  	return ""
   352  }
   353  
   354  func (t testSite) Sites() Sites {
   355  	return nil
   356  }
   357  
   358  func (t testSite) Sections() Pages {
   359  	return nil
   360  }
   361  
   362  func (t testSite) GetPage(ref ...string) (Page, error) {
   363  	return nil, nil
   364  }
   365  
   366  func (t testSite) Current() Site {
   367  	return t
   368  }
   369  
   370  func (s testSite) LanguagePrefix() string {
   371  	return ""
   372  }
   373  
   374  func (t testSite) Languages() langs.Languages {
   375  	return nil
   376  }
   377  
   378  // Deprecated: Use .Site.Config.Services.GoogleAnalytics.ID instead
   379  func (t testSite) GoogleAnalytics() string {
   380  	return ""
   381  }
   382  
   383  func (t testSite) MainSections() []string {
   384  	return nil
   385  }
   386  
   387  func (t testSite) GetIdentity() identity.Identity {
   388  	return identity.KeyValueIdentity{Key: "site", Value: t.l.Lang}
   389  }
   390  
   391  // Deprecated: use hugo.IsServer instead
   392  func (t testSite) IsServer() bool {
   393  	return false
   394  }
   395  
   396  func (t testSite) Language() *langs.Language {
   397  	return t.l
   398  }
   399  
   400  func (t testSite) Home() Page {
   401  	return nil
   402  }
   403  
   404  func (t testSite) Pages() Pages {
   405  	return nil
   406  }
   407  
   408  func (t testSite) AllPages() Pages {
   409  	return nil
   410  }
   411  
   412  func (t testSite) RegularPages() Pages {
   413  	return nil
   414  }
   415  
   416  func (t testSite) Menus() navigation.Menus {
   417  	return nil
   418  }
   419  
   420  func (t testSite) Taxonomies() TaxonomyList {
   421  	return nil
   422  }
   423  
   424  func (t testSite) BaseURL() template.URL {
   425  	return ""
   426  }
   427  
   428  func (t testSite) Params() maps.Params {
   429  	return nil
   430  }
   431  
   432  func (t testSite) Data() map[string]any {
   433  	return nil
   434  }
   435  
   436  func (s testSite) Config() SiteConfig {
   437  	return SiteConfig{}
   438  }
   439  
   440  func (testSite) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error) {
   441  	return nil, nil
   442  }
   443  
   444  // Deprecated: Use .Site.Config.Services.Disqus.Shortname instead
   445  func (testSite) DisqusShortname() string {
   446  	return ""
   447  }
   448  
   449  func (s testSite) BuildDrafts() bool {
   450  	return false
   451  }
   452  
   453  func (s testSite) IsMultiLingual() bool {
   454  	return false
   455  }
   456  
   457  func (s testSite) Param(key any) (any, error) {
   458  	return nil, nil
   459  }
   460  
   461  func (s testSite) RSSLink() template.URL {
   462  	return ""
   463  }
   464  
   465  // NewDummyHugoSite creates a new minimal test site.
   466  func NewDummyHugoSite(conf config.AllProvider) Site {
   467  	return testSite{
   468  		h: hugo.NewInfo(conf, nil),
   469  		l: &langs.Language{
   470  			Lang: "en",
   471  		},
   472  	}
   473  }
   474  
   475  // SiteConfig holds the config in site.Config.
   476  type SiteConfig struct {
   477  	// This contains all privacy related settings that can be used to
   478  	// make the YouTube template etc. GDPR compliant.
   479  	Privacy privacy.Config
   480  
   481  	// Services contains config for services such as Google Analytics etc.
   482  	Services services.Config
   483  }