github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/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  
    22  	"github.com/gohugoio/hugo/config"
    23  
    24  	"github.com/gohugoio/hugo/common/hugo"
    25  	"github.com/gohugoio/hugo/langs"
    26  	"github.com/gohugoio/hugo/navigation"
    27  )
    28  
    29  // Site represents a site in the build. This is currently a very narrow interface,
    30  // but the actual implementation will be richer, see hugolib.SiteInfo.
    31  type Site interface {
    32  	Language() *langs.Language
    33  	RegularPages() Pages
    34  	Pages() Pages
    35  	Home() Page
    36  	IsServer() bool
    37  	ServerPort() int
    38  	Title() string
    39  	Sites() Sites
    40  	Hugo() hugo.Info
    41  	BaseURL() template.URL
    42  	Taxonomies() interface{}
    43  	LastChange() time.Time
    44  	Menus() navigation.Menus
    45  	Params() maps.Params
    46  	Data() map[string]interface{}
    47  }
    48  
    49  // Sites represents an ordered list of sites (languages).
    50  type Sites []Site
    51  
    52  // First is a convenience method to get the first Site, i.e. the main language.
    53  func (s Sites) First() Site {
    54  	if len(s) == 0 {
    55  		return nil
    56  	}
    57  	return s[0]
    58  }
    59  
    60  type testSite struct {
    61  	h hugo.Info
    62  	l *langs.Language
    63  }
    64  
    65  func (t testSite) Hugo() hugo.Info {
    66  	return t.h
    67  }
    68  
    69  func (t testSite) ServerPort() int {
    70  	return 1313
    71  }
    72  
    73  func (testSite) LastChange() (t time.Time) {
    74  	return
    75  }
    76  
    77  func (t testSite) Title() string {
    78  	return "foo"
    79  }
    80  
    81  func (t testSite) Sites() Sites {
    82  	return nil
    83  }
    84  
    85  func (t testSite) IsServer() bool {
    86  	return false
    87  }
    88  
    89  func (t testSite) Language() *langs.Language {
    90  	return t.l
    91  }
    92  
    93  func (t testSite) Home() Page {
    94  	return nil
    95  }
    96  
    97  func (t testSite) Pages() Pages {
    98  	return nil
    99  }
   100  
   101  func (t testSite) RegularPages() Pages {
   102  	return nil
   103  }
   104  
   105  func (t testSite) Menus() navigation.Menus {
   106  	return nil
   107  }
   108  
   109  func (t testSite) Taxonomies() interface{} {
   110  	return nil
   111  }
   112  
   113  func (t testSite) BaseURL() template.URL {
   114  	return ""
   115  }
   116  
   117  func (t testSite) Params() maps.Params {
   118  	return nil
   119  }
   120  
   121  func (t testSite) Data() map[string]interface{} {
   122  	return nil
   123  }
   124  
   125  // NewDummyHugoSite creates a new minimal test site.
   126  func NewDummyHugoSite(cfg config.Provider) Site {
   127  	return testSite{
   128  		h: hugo.NewInfo(hugo.EnvironmentProduction, nil),
   129  		l: langs.NewLanguage("en", cfg),
   130  	}
   131  }