Debian Codegraph @

resources/page/hugoinfo.go

raw
1 // Copyright 2026 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 "fmt"
18 "html/template"
19
20 "github.com/gohugoio/hugo/common/hstore"
21 "github.com/gohugoio/hugo/common/hugo"
22 "github.com/gohugoio/hugo/common/version"
23 "github.com/gohugoio/hugo/identity"
24 "github.com/gohugoio/hugo/resources/page/siteidentities"
25 )
26
27 var _ hstore.StoreProvider = (*HugoInfo)(nil)
28
29 type hugoInfoProviders struct {
30 HugoInfoHugoSitesProvider
31 }
32
33 // HugoInfo contains information about the current Hugo environment.
34 type HugoInfo struct {
35 CommitHash string
36 BuildDate string
37
38 // Version of Go that the Hugo binary was built with.
39 GoVersion string
40
41 // Avoid exporting the embedded providers directly, as they may be used by the template layer.
42 hugoInfoProviders
43
44 // Context gives access to some of the context scoped variables.
45 Context hugo.Context
46
47 opts HugoInfoOptions
48 store *hstore.Scratch
49 }
50
51 // Version returns the current version as a comparable version string.
52 func (i HugoInfo) Version() version.VersionString {
53 return hugo.CurrentVersion.Version()
54 }
55
56 // Generator returns a Hugo meta generator HTML tag.
57 func (i HugoInfo) Generator() template.HTML {
58 return template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s">`, hugo.CurrentVersion.String()))
59 }
60
61 // Environment returns the build environment.
62 // Defaults are "production" (hugo) and "development" (hugo server).
63 // This can also be set by the user.
64 // It can be any string, but it will be all lower case.
65 func (i HugoInfo) Environment() string {
66 return i.opts.Conf.Environment()
67 }
68
69 // IsDevelopment reports whether the current running environment is "development".
70 func (i HugoInfo) IsDevelopment() bool {
71 return i.Environment() == hugo.EnvironmentDevelopment
72 }
73
74 // IsProduction reports whether the current running environment is "production".
75 func (i HugoInfo) IsProduction() bool {
76 return i.Environment() == hugo.EnvironmentProduction
77 }
78
79 // IsServer reports whether the built-in server is running.
80 func (i HugoInfo) IsServer() bool {
81 return i.opts.Conf.Running()
82 }
83
84 // IsExtended reports whether the Hugo binary is the extended version.
85 func (i HugoInfo) IsExtended() bool {
86 return hugo.IsExtended
87 }
88
89 // WorkingDir returns the project working directory.
90 func (i HugoInfo) WorkingDir() string {
91 return i.opts.Conf.WorkingDir()
92 }
93
94 // Deps gets a list of dependencies for this Hugo build.
95 func (i HugoInfo) Deps() []*hugo.Dependency {
96 return i.opts.Deps
97 }
98
99 func (i HugoInfo) Store() *hstore.Scratch {
100 return i.store
101 }
102
103 // IsMultihost reports whether each configured language has a unique baseURL.
104 func (i HugoInfo) IsMultihost() bool {
105 return i.opts.Conf.IsMultihost()
106 }
107
108 // IsMultilingual reports whether there are two or more configured languages.
109 func (i HugoInfo) IsMultilingual() bool {
110 return i.opts.Conf.IsMultilingual()
111 }
112
113 // For internal use.
114 func (s HugoInfo) ForEeachIdentityByName(name string, f func(identity.Identity) bool) {
115 if id, found := siteidentities.FromString(name); found {
116 if f(id) {
117 return
118 }
119 }
120 }
121
122 // HugoInfoConfigProvider represents the config options that are relevant for HugoInfo.
123 type HugoInfoConfigProvider interface {
124 Environment() string
125 Running() bool
126 WorkingDir() string
127 IsMultihost() bool
128 IsMultilingual() bool
129 }
130
131 // HugoInfoHugoSitesProvider provides what HugoInfo needs from hugolib.HugoSites.
132 type HugoInfoHugoSitesProvider interface {
133 SitesProvider
134 DataProvider
135 }
136
137 // HugoInfoOptions defines the providers required to initialize HugoInfo.
138 type HugoInfoOptions struct {
139 Conf HugoInfoConfigProvider
140 HugoInfoHugoSitesProvider HugoInfoHugoSitesProvider
141
142 Deps []*hugo.Dependency
143 CommitHash string
144 BuildDate string
145 GoVersion string
146 }
147
148 // NewHugoInfo creates a new Hugo Info object.
149 func NewHugoInfo(opts HugoInfoOptions) HugoInfo {
150 if opts.Conf == nil {
151 panic("config provider not set")
152 }
153 if opts.Conf.Environment() == "" {
154 panic("environment not set")
155 }
156 if opts.HugoInfoHugoSitesProvider == nil {
157 opts.HugoInfoHugoSitesProvider = nopHugoSitesProvider{}
158 }
159
160 return HugoInfo{
161 CommitHash: opts.CommitHash,
162 BuildDate: opts.BuildDate,
163 GoVersion: opts.GoVersion,
164
165 HugoInfoHugoSitesProvider: opts.HugoInfoHugoSitesProvider,
166
167 opts: opts,
168 store: hstore.NewScratch(),
169 }
170 }