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
This commit is contained in:
Bjørn Erik Pedersen
2018-02-22 09:15:12 +01:00
parent 55bd46a633
commit 0602135fd4
9 changed files with 127 additions and 14 deletions

View File

@@ -88,11 +88,12 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{},
// Eq returns the boolean truth of arg1 == arg2.
func (*Namespace) Eq(x, y interface{}) bool {
// hugolib.Page implements compare.Eqer to make Page and PageOutput comparable.
if e1, ok := x.(compare.Eqer); ok {
if e2, ok := y.(compare.Eqer); ok {
return e1.Eq(e2)
}
if e, ok := x.(compare.Eqer); ok {
return e.Eq(y)
}
if e, ok := y.(compare.Eqer); ok {
return e.Eq(x)
}
normalize := func(v interface{}) interface{} {
@@ -120,25 +121,25 @@ func (n *Namespace) Ne(x, y interface{}) bool {
// Ge returns the boolean truth of arg1 >= arg2.
func (n *Namespace) Ge(a, b interface{}) bool {
left, right := n.compareGetFloat(a, b)
left, right := n.compareGet(a, b)
return left >= right
}
// Gt returns the boolean truth of arg1 > arg2.
func (n *Namespace) Gt(a, b interface{}) bool {
left, right := n.compareGetFloat(a, b)
left, right := n.compareGet(a, b)
return left > right
}
// Le returns the boolean truth of arg1 <= arg2.
func (n *Namespace) Le(a, b interface{}) bool {
left, right := n.compareGetFloat(a, b)
left, right := n.compareGet(a, b)
return left <= right
}
// Lt returns the boolean truth of arg1 < arg2.
func (n *Namespace) Lt(a, b interface{}) bool {
left, right := n.compareGetFloat(a, b)
left, right := n.compareGet(a, b)
return left < right
}
@@ -151,7 +152,29 @@ func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} {
return b
}
func (*Namespace) compareGetFloat(a interface{}, b interface{}) (float64, float64) {
func (*Namespace) compareGet(a interface{}, b interface{}) (float64, float64) {
if ac, ok := a.(compare.Comparer); ok {
c := ac.Compare(b)
if c < 0 {
return 1, 0
} else if c == 0 {
return 0, 0
} else {
return 0, 1
}
}
if bc, ok := b.(compare.Comparer); ok {
c := bc.Compare(a)
if c < 0 {
return 0, 1
} else if c == 0 {
return 0, 0
} else {
return 1, 0
}
}
var left, right float64
var leftStr, rightStr *string
av := reflect.ValueOf(a)