Add hugo.IsProduction function

Fixes #6873
This commit is contained in:
Joshua Wong 2020-02-26 00:58:07 +08:00 committed by GitHub
parent d184e5059c
commit 1352bc880d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View file

@ -54,6 +54,10 @@ func (i Info) Generator() template.HTML {
return template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, CurrentVersion.String())) return template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, CurrentVersion.String()))
} }
func (i Info) IsProduction() bool {
return i.Environment == EnvironmentProduction
}
// NewInfo creates a new Hugo Info object. // NewInfo creates a new Hugo Info object.
func NewInfo(environment string) Info { func NewInfo(environment string) Info {
if environment == "" { if environment == "" {

View file

@ -31,5 +31,9 @@ func TestHugoInfo(t *testing.T) {
c.Assert(hugoInfo.BuildDate, qt.Equals, buildDate) c.Assert(hugoInfo.BuildDate, qt.Equals, buildDate)
c.Assert(hugoInfo.Environment, qt.Equals, "production") c.Assert(hugoInfo.Environment, qt.Equals, "production")
c.Assert(string(hugoInfo.Generator()), qt.Contains, fmt.Sprintf("Hugo %s", hugoInfo.Version())) c.Assert(string(hugoInfo.Generator()), qt.Contains, fmt.Sprintf("Hugo %s", hugoInfo.Version()))
c.Assert(hugoInfo.IsProduction(), qt.Equals, true)
devHugoInfo := NewInfo("development")
c.Assert(devHugoInfo.IsProduction(), qt.Equals, false)
} }

View file

@ -33,7 +33,7 @@ hugo.Version
`hugo` returns an instance that contains the following functions: `hugo` returns an instance that contains the following functions:
hugo.Environment hugo.Environment
: the current running environment as defined through the `--environment` cli tag. : the current running environment as defined through the `--environment` cli tag
hugo.CommitHash hugo.CommitHash
: the git commit hash of the current Hugo binary e.g. `0e8bed9ccffba0df554728b46c5bbf6d78ae5247` : the git commit hash of the current Hugo binary e.g. `0e8bed9ccffba0df554728b46c5bbf6d78ae5247`
@ -41,7 +41,8 @@ hugo.CommitHash
hugo.BuildDate hugo.BuildDate
: the compile date of the current Hugo binary formatted with RFC 3339 e.g. `2002-10-02T10:00:00-05:00` : the compile date of the current Hugo binary formatted with RFC 3339 e.g. `2002-10-02T10:00:00-05:00`
hugo.IsProduction
: returns true if `hugo.Environment` is set to the production environment
{{% note "Use the Hugo Generator Tag" %}} {{% note "Use the Hugo Generator Tag" %}}
We highly recommend using `hugo.Generator` in your website's `<head>`. `hugo.Generator` is included by default in all themes hosted on [themes.gohugo.io](https://themes.gohugo.io). The generator tag allows the Hugo team to track the usage and popularity of Hugo. We highly recommend using `hugo.Generator` in your website's `<head>`. `hugo.Generator` is included by default in all themes hosted on [themes.gohugo.io](https://themes.gohugo.io). The generator tag allows the Hugo team to track the usage and popularity of Hugo.