summaryrefslogtreecommitdiffstats
path: root/helpers/hugo.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-02-22 09:15:12 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-02-22 09:15:12 +0100
commit0602135fd44b0cfa0a51b0ec6e451ae58ac95666 (patch)
treebb045df0360b26dbacf682fa227d5d4e18d57bdc /helpers/hugo.go
parent55bd46a633d68f62e131457631ba839d6f876a55 (diff)
Make ge, le etc. work with the Hugo Version number
This means that you can do something ala: ```html {{ if ge .Hugo.Version "0.36" }}Reasonable new Hugo version!{{ end }} ``` The intented use is feature toggling, but please note that it will take some time and Hugo versions until this can be trusted. It does not work in older Hugo versions. Fixes #4443
Diffstat (limited to 'helpers/hugo.go')
-rw-r--r--helpers/hugo.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/helpers/hugo.go b/helpers/hugo.go
index c4c3f4a76..b9b42e6cf 100644
--- a/helpers/hugo.go
+++ b/helpers/hugo.go
@@ -18,6 +18,7 @@ import (
"fmt"
"strings"
+ "github.com/gohugoio/hugo/compare"
"github.com/spf13/cast"
)
@@ -34,10 +35,40 @@ type HugoVersion struct {
Suffix string
}
+var (
+ _ compare.Eqer = (*HugoVersionString)(nil)
+ _ compare.Comparer = (*HugoVersionString)(nil)
+)
+
+type HugoVersionString string
+
func (v HugoVersion) String() string {
return hugoVersion(v.Number, v.PatchLevel, v.Suffix)
}
+func (v HugoVersion) Version() HugoVersionString {
+ return HugoVersionString(v.String())
+}
+
+func (h HugoVersionString) String() string {
+ return string(h)
+}
+
+// Implements compare.Comparer
+func (h HugoVersionString) Compare(other interface{}) int {
+ v := MustParseHugoVersion(h.String())
+ return compareVersions(v.Number, v.PatchLevel, other)
+}
+
+// Implements compare.Eqer
+func (h HugoVersionString) Eq(other interface{}) bool {
+ s, err := cast.ToStringE(other)
+ if err != nil {
+ return false
+ }
+ return s == h.String()
+}
+
// ParseHugoVersion parses a version string.
func ParseHugoVersion(s string) (HugoVersion, error) {
var vv HugoVersion