summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-11-26 10:11:22 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-12-06 14:37:25 +0100
commit831d23cb4d1ca99cdc15ed31c8ee1f981497be8f (patch)
tree8fe47b1b1b9233448297f8015ce61bbb7da13fc7 /common
parent514e18dc27ce37a0e9a231741d616cf29d50d610 (diff)
Add tpl/site and tpl/hugo
This means that the current `.Site` and ´.Hugo` is available as a globals, so you can do `site.IsServer`, `hugo.Version` etc. Fixes #5470 Fixes #5467 Fixes #5503
Diffstat (limited to 'common')
-rw-r--r--common/hugo/hugo.go45
-rw-r--r--common/hugo/hugo_test.go34
-rw-r--r--common/hugo/site.go24
-rw-r--r--common/hugo/vars_extended.go18
-rw-r--r--common/hugo/vars_regular.go18
-rw-r--r--common/hugo/version.go239
-rw-r--r--common/hugo/version_current.go22
-rw-r--r--common/hugo/version_test.go70
8 files changed, 470 insertions, 0 deletions
diff --git a/common/hugo/hugo.go b/common/hugo/hugo.go
new file mode 100644
index 000000000..b93b10bf1
--- /dev/null
+++ b/common/hugo/hugo.go
@@ -0,0 +1,45 @@
+// Copyright 2018 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 hugo
+
+import (
+ "fmt"
+ "html/template"
+)
+
+var (
+ // CommitHash contains the current Git revision. Use make to build to make
+ // sure this gets set.
+ CommitHash string
+
+ // BuildDate contains the date of the current build.
+ BuildDate string
+)
+
+// Info contains information about the current Hugo environment
+type Info struct {
+ Version VersionString
+ Generator template.HTML
+ CommitHash string
+ BuildDate string
+}
+
+func NewInfo() Info {
+ return Info{
+ Version: CurrentVersion.Version(),
+ CommitHash: CommitHash,
+ BuildDate: BuildDate,
+ Generator: template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, CurrentVersion.String())),
+ }
+}
diff --git a/common/hugo/hugo_test.go b/common/hugo/hugo_test.go
new file mode 100644
index 000000000..18a9b594f
--- /dev/null
+++ b/common/hugo/hugo_test.go
@@ -0,0 +1,34 @@
+// Copyright 2018 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 hugo
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestHugoInfo(t *testing.T) {
+ assert := require.New(t)
+
+ hugoInfo := NewInfo()
+
+ assert.Equal(CurrentVersion.Version(), hugoInfo.Version)
+ assert.IsType(VersionString(""), hugoInfo.Version)
+ assert.Equal(CommitHash, hugoInfo.CommitHash)
+ assert.Equal(BuildDate, hugoInfo.BuildDate)
+ assert.Contains(hugoInfo.Generator, fmt.Sprintf("Hugo %s", hugoInfo.Version))
+
+}
diff --git a/common/hugo/site.go b/common/hugo/site.go
new file mode 100644
index 000000000..08391858a
--- /dev/null
+++ b/common/hugo/site.go
@@ -0,0 +1,24 @@
+// Copyright 2018 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 hugo
+
+import "github.com/gohugoio/hugo/langs"
+
+// Site represents a site in the build. This is currently a very narrow interface,
+// but the actual implementation will be richer, see hugolib.SiteInfo.
+type Site interface {
+ Language() *langs.Language
+ IsServer() bool
+ Hugo() Info
+}
diff --git a/common/hugo/vars_extended.go b/common/hugo/vars_extended.go
new file mode 100644
index 000000000..20683b804
--- /dev/null
+++ b/common/hugo/vars_extended.go
@@ -0,0 +1,18 @@
+// Copyright 2018 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.
+
+// +build extended
+
+package hugo
+
+var isExtended = true
diff --git a/common/hugo/vars_regular.go b/common/hugo/vars_regular.go
new file mode 100644
index 000000000..e1ece83fb
--- /dev/null
+++ b/common/hugo/vars_regular.go
@@ -0,0 +1,18 @@
+// Copyright 2018 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.
+
+// +build !extended
+
+package hugo
+
+var isExtended = false
diff --git a/common/hugo/version.go b/common/hugo/version.go
new file mode 100644
index 000000000..204f8f747
--- /dev/null
+++ b/common/hugo/version.go
@@ -0,0 +1,239 @@
+// Copyright 2018 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 hugo
+
+import (
+ "fmt"
+
+ "runtime"
+ "strings"
+
+ "github.com/gohugoio/hugo/compare"
+ "github.com/spf13/cast"
+)
+
+// Version represents the Hugo build version.
+type Version struct {
+ // Major and minor version.
+ Number float32
+
+ // Increment this for bug releases
+ PatchLevel int
+
+ // HugoVersionSuffix is the suffix used in the Hugo version string.
+ // It will be blank for release versions.
+ Suffix string
+}
+
+var (
+ _ compare.Eqer = (*VersionString)(nil)
+ _ compare.Comparer = (*VersionString)(nil)
+)
+
+func (v Version) String() string {
+ return version(v.Number, v.PatchLevel, v.Suffix)
+}
+
+// Version returns the Hugo version.
+func (v Version) Version() VersionString {
+ return VersionString(v.String())
+}
+
+// VersionString represents a Hugo version string.
+type VersionString string
+
+func (h VersionString) String() string {
+ return string(h)
+}
+
+// Compare implements the compare.Comparer interface.
+func (h VersionString) Compare(other interface{}) int {
+ v := MustParseVersion(h.String())
+ return compareVersionsWithSuffix(v.Number, v.PatchLevel, v.Suffix, other)
+}
+
+// Eq implements the compare.Eqer interface.
+func (h VersionString) Eq(other interface{}) bool {
+ s, err := cast.ToStringE(other)
+ if err != nil {
+ return false
+ }
+ return s == h.String()
+}
+
+var versionSuffixes = []string{"-test", "-DEV"}
+
+// ParseVersion parses a version string.
+func ParseVersion(s string) (Version, error) {
+ var vv Version
+ for _, suffix := range versionSuffixes {
+ if strings.HasSuffix(s, suffix) {
+ vv.Suffix = suffix
+ s = strings.TrimSuffix(s, suffix)
+ }
+ }
+
+ v, p := parseVersion(s)
+
+ vv.Number = v
+ vv.PatchLevel = p
+
+ return vv, nil
+}
+
+// MustParseVersion parses a version string
+// and panics if any error occurs.
+func MustParseVersion(s string) Version {
+ vv, err := ParseVersion(s)
+ if err != nil {
+ panic(err)
+ }
+ return vv
+}
+
+// ReleaseVersion represents the release version.
+func (v Version) ReleaseVersion() Version {
+ v.Suffix = ""
+ return v
+}
+
+// Next returns the next Hugo release version.
+func (v Version) Next() Version {
+ return Version{Number: v.Number + 0.01}
+}
+
+// Prev returns the previous Hugo release version.
+func (v Version) Prev() Version {
+ return Version{Number: v.Number - 0.01}
+}
+
+// NextPatchLevel returns the next patch/bugfix Hugo version.
+// This will be a patch increment on the previous Hugo version.
+func (v Version) NextPatchLevel(level int) Version {
+ return Version{Number: v.Number - 0.01, PatchLevel: level}
+}
+
+// BuildVersionString creates a version string. This is what you see when
+// running "hugo version".
+func BuildVersionString() string {
+ program := "Hugo Static Site Generator"
+
+ version := "v" + CurrentVersion.String()
+ if CommitHash != "" {
+ version += "-" + strings.ToUpper(CommitHash)
+ }
+ if isExtended {
+ version += "/extended"
+ }
+
+ osArch := runtime.GOOS + "/" + runtime.GOARCH
+
+ var buildDate string
+ if BuildDate != "" {
+ buildDate = BuildDate
+ } else {
+ buildDate = "unknown"
+ }
+
+ return fmt.Sprintf("%s %s %s BuildDate: %s", program, version, osArch, buildDate)
+
+}
+
+func version(version float32, patchVersion int, suffix string) string {
+ if patchVersion > 0 {
+ return fmt.Sprintf("%.2f.%d%s", version, patchVersion, suffix)
+ }
+ return fmt.Sprintf("%.2f%s", version, suffix)
+}
+
+// CompareVersion compares the given version string or number against the
+// running Hugo version.
+// It returns -1 if the given version is less than, 0 if equal and 1 if greater than
+// the running version.
+func CompareVersion(version interface{}) int {
+ return compareVersionsWithSuffix(CurrentVersion.Number, CurrentVersion.PatchLevel, CurrentVersion.Suffix, version)
+}
+
+func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int {
+ return compareVersionsWithSuffix(inVersion, inPatchVersion, "", in)
+}
+
+func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix string, in interface{}) int {
+ var c int
+ switch d := in.(type) {
+ case float64:
+ c = compareFloatVersions(inVersion, float32(d))
+ case float32:
+ c = compareFloatVersions(inVersion, d)
+ case int:
+ c = compareFloatVersions(inVersion, float32(d))
+ case int32:
+ c = compareFloatVersions(inVersion, float32(d))
+ case int64:
+ c = compareFloatVersions(inVersion, float32(d))
+ default:
+ s, err := cast.ToStringE(in)
+ if err != nil {
+ return -1
+ }
+
+ v, err := ParseVersion(s)
+ if err != nil {
+ return -1
+ }
+
+ if v.Number == inVersion && v.PatchLevel == inPatchVersion {
+ return strings.Compare(suffix, v.Suffix)
+ }
+
+ if v.Number < inVersion || (v.Number == inVersion && v.PatchLevel < inPatchVersion) {
+ return -1
+ }
+
+ return 1
+ }
+
+ if c == 0 && suffix != "" {
+ return 1
+ }
+
+ return c
+}
+
+func parseVersion(s string) (float32, int) {
+ var (
+ v float32
+ p int
+ )
+
+ if strings.Count(s, ".") == 2 {
+ li := strings.LastIndex(s, ".")
+ p = cast.ToInt(s[li+1:])
+ s = s[:li]
+ }
+
+ v = float32(cast.ToFloat64(s))
+
+ return v, p
+}
+
+func compareFloatVersions(version float32, v float32) int {
+ if v == version {
+ return 0
+ }
+ if v < version {
+ return -1
+ }
+ return 1
+}
diff --git a/common/hugo/version_current.go b/common/hugo/version_current.go
new file mode 100644
index 000000000..0a42f3023
--- /dev/null
+++ b/common/hugo/version_current.go
@@ -0,0 +1,22 @@
+// Copyright 2018 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 hugo
+
+// CurrentVersion represents the current build version.
+// This should be the only one.
+var CurrentVersion = Version{
+ Number: 0.53,
+ PatchLevel: 0,
+ Suffix: "-DEV",
+}
diff --git a/common/hugo/version_test.go b/common/hugo/version_test.go
new file mode 100644
index 000000000..fb28750ec
--- /dev/null
+++ b/common/hugo/version_test.go
@@ -0,0 +1,70 @@
+// Copyright 2015 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 hugo
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestHugoVersion(t *testing.T) {
+ assert.Equal(t, "0.15-DEV", version(0.15, 0, "-DEV"))
+ assert.Equal(t, "0.15.2-DEV", version(0.15, 2, "-DEV"))
+
+ v := Version{Number: 0.21, PatchLevel: 0, Suffix: "-DEV"}
+
+ require.Equal(t, v.ReleaseVersion().String(), "0.21")
+ require.Equal(t, "0.21-DEV", v.String())
+ require.Equal(t, "0.22", v.Next().String())
+ nextVersionString := v.Next().Version()
+ require.Equal(t, "0.22", nextVersionString.String())
+ require.True(t, nextVersionString.Eq("0.22"))
+ require.False(t, nextVersionString.Eq("0.21"))
+ require.True(t, nextVersionString.Eq(nextVersionString))
+ require.Equal(t, "0.20.3", v.NextPatchLevel(3).String())
+}
+
+func TestCompareVersions(t *testing.T) {
+ require.Equal(t, 0, compareVersions(0.20, 0, 0.20))
+ require.Equal(t, 0, compareVersions(0.20, 0, float32(0.20)))
+ require.Equal(t, 0, compareVersions(0.20, 0, float64(0.20)))
+ require.Equal(t, 1, compareVersions(0.19, 1, 0.20))
+ require.Equal(t, 1, compareVersions(0.19, 3, "0.20.2"))
+ require.Equal(t, -1, compareVersions(0.19, 1, 0.01))
+ require.Equal(t, 1, compareVersions(0, 1, 3))
+ require.Equal(t, 1, compareVersions(0, 1, int32(3)))
+ require.Equal(t, 1, compareVersions(0, 1, int64(3)))
+ require.Equal(t, 0, compareVersions(0.20, 0, "0.20"))
+ require.Equal(t, 0, compareVersions(0.20, 1, "0.20.1"))
+ require.Equal(t, -1, compareVersions(0.20, 1, "0.20"))
+ require.Equal(t, 1, compareVersions(0.20, 0, "0.20.1"))
+ require.Equal(t, 1, compareVersions(0.20, 1, "0.20.2"))
+ require.Equal(t, 1, compareVersions(0.21, 1, "0.22.1"))
+ require.Equal(t, -1, compareVersions(0.22, 0, "0.22-DEV"))
+ require.Equal(t, 1, compareVersions(0.22, 0, "0.22.1-DEV"))
+ require.Equal(t, 1, compareVersionsWithSuffix(0.22, 0, "-DEV", "0.22"))
+ require.Equal(t, -1, compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22"))
+ require.Equal(t, 0, compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22.1-DEV"))
+
+}
+
+func TestParseHugoVersion(t *testing.T) {
+ require.Equal(t, "0.25", MustParseVersion("0.25").String())
+ require.Equal(t, "0.25.2", MustParseVersion("0.25.2").String())
+ require.Equal(t, "0.25-test", MustParseVersion("0.25-test").String())
+ require.Equal(t, "0.25-DEV", MustParseVersion("0.25-DEV").String())
+
+}