Debian Codegraph @

resources/page/site.go

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