site.go (3413B)
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 // Returns the Language configured for this Site.
33 Language() *langs.Language
34
35 // Returns all the regular Pages in this Site.
36 RegularPages() Pages
37
38 // Returns all Pages in this Site.
39 Pages() Pages
40
41 // A shortcut to the home page.
42 Home() Page
43
44 // Returns true if we're running in a server.
45 IsServer() bool
46
47 // Returns the server port.
48 ServerPort() int
49
50 // Returns the configured title for this Site.
51 Title() string
52
53 // Returns all Sites for all languages.
54 Sites() Sites
55
56 // Returns Site currently rendering.
57 Current() Site
58
59 // Returns a struct with some information about the build.
60 Hugo() hugo.Info
61
62 // Returns the BaseURL for this Site.
63 BaseURL() template.URL
64
65 // Retuns a taxonomy map.
66 Taxonomies() any
67
68 // Returns the last modification date of the content.
69 LastChange() time.Time
70
71 // Returns the Menus for this site.
72 Menus() navigation.Menus
73
74 // Returns the Params configured for this site.
75 Params() maps.Params
76
77 // Returns a map of all the data inside /data.
78 Data() map[string]any
79 }
80
81 // Sites represents an ordered list of sites (languages).
82 type Sites []Site
83
84 // First is a convenience method to get the first Site, i.e. the main language.
85 func (s Sites) First() Site {
86 if len(s) == 0 {
87 return nil
88 }
89 return s[0]
90 }
91
92 type testSite struct {
93 h hugo.Info
94 l *langs.Language
95 }
96
97 func (t testSite) Hugo() hugo.Info {
98 return t.h
99 }
100
101 func (t testSite) ServerPort() int {
102 return 1313
103 }
104
105 func (testSite) LastChange() (t time.Time) {
106 return
107 }
108
109 func (t testSite) Title() string {
110 return "foo"
111 }
112
113 func (t testSite) Sites() Sites {
114 return nil
115 }
116
117 func (t testSite) Current() Site {
118 return t
119 }
120
121 func (t testSite) IsServer() bool {
122 return false
123 }
124
125 func (t testSite) Language() *langs.Language {
126 return t.l
127 }
128
129 func (t testSite) Home() Page {
130 return nil
131 }
132
133 func (t testSite) Pages() Pages {
134 return nil
135 }
136
137 func (t testSite) RegularPages() Pages {
138 return nil
139 }
140
141 func (t testSite) Menus() navigation.Menus {
142 return nil
143 }
144
145 func (t testSite) Taxonomies() any {
146 return nil
147 }
148
149 func (t testSite) BaseURL() template.URL {
150 return ""
151 }
152
153 func (t testSite) Params() maps.Params {
154 return nil
155 }
156
157 func (t testSite) Data() map[string]any {
158 return nil
159 }
160
161 // NewDummyHugoSite creates a new minimal test site.
162 func NewDummyHugoSite(cfg config.Provider) Site {
163 return testSite{
164 h: hugo.NewInfo(hugo.EnvironmentProduction, nil),
165 l: langs.NewLanguage("en", cfg),
166 }
167 }