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