Skip to content

Commit

Permalink
Make ge, le etc. work with the Hugo Version number
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Feb 22, 2018
1 parent 55bd46a commit 0602135
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 14 deletions.
8 changes: 8 additions & 0 deletions compare/eq.go → compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ package compare
type Eqer interface {
Eq(other interface{}) bool
}

// Comparer can be used to compare two values.
// This will be used when using the le, ge etc. operators in the templates.
// Compare returns -1 if the given version is less than, 0 if equal and 1 if greater than
// the running version.
type Comparer interface {
Compare(other interface{}) int
}
31 changes: 31 additions & 0 deletions helpers/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"strings"

"github.com/gohugoio/hugo/compare"
"github.com/spf13/cast"
)

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions helpers/hugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func TestHugoVersion(t *testing.T) {
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())
}

Expand Down
6 changes: 3 additions & 3 deletions hugolib/hugo_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
// 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.
Expand Down Expand Up @@ -33,15 +33,15 @@ var hugoInfo *HugoInfo

// HugoInfo contains information about the current Hugo environment
type HugoInfo struct {
Version string
Version helpers.HugoVersionString
Generator template.HTML
CommitHash string
BuildDate string
}

func init() {
hugoInfo = &HugoInfo{
Version: helpers.CurrentHugoVersion.String(),
Version: helpers.CurrentHugoVersion.Version(),
CommitHash: CommitHash,
BuildDate: BuildDate,
Generator: template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, helpers.CurrentHugoVersion.String())),
Expand Down
33 changes: 33 additions & 0 deletions hugolib/hugo_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 hugolib

import (
"fmt"
"testing"

"github.com/gohugoio/hugo/helpers"
"github.com/stretchr/testify/require"
)

func TestHugoInfo(t *testing.T) {
assert := require.New(t)

assert.Equal(helpers.CurrentHugoVersion.Version(), hugoInfo.Version)
assert.IsType(helpers.HugoVersionString(""), hugoInfo.Version)
assert.Equal(CommitHash, hugoInfo.CommitHash)
assert.Equal(BuildDate, hugoInfo.BuildDate)
assert.Contains(hugoInfo.Generator, fmt.Sprintf("Hugo %s", hugoInfo.Version))

}
43 changes: 33 additions & 10 deletions tpl/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tpl/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"testing"
"time"

"github.com/gohugoio/hugo/helpers"

"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -171,6 +173,13 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte
{tstEqerType1("a"), tstEqerType2("a"), 0},
{tstEqerType2("a"), tstEqerType1("a"), 0},
{tstEqerType2("a"), tstEqerType1("b"), -1},
{helpers.MustParseHugoVersion("0.32.1").Version(), helpers.MustParseHugoVersion("0.32").Version(), 1},
{helpers.MustParseHugoVersion("0.35").Version(), helpers.MustParseHugoVersion("0.32").Version(), 1},
{helpers.MustParseHugoVersion("0.36").Version(), helpers.MustParseHugoVersion("0.36").Version(), 0},
{helpers.MustParseHugoVersion("0.32").Version(), helpers.MustParseHugoVersion("0.36").Version(), -1},
{helpers.MustParseHugoVersion("0.32").Version(), "0.36", -1},
{"0.36", helpers.MustParseHugoVersion("0.32").Version(), 1},
{"0.36", helpers.MustParseHugoVersion("0.36").Version(), 0},

This comment has been minimized.

Copy link
@kaushalmodi

kaushalmodi Feb 22, 2018

Contributor

Can you also add "0.37-DEV" to the test.. should return -1 when compared with "0.37", and 1 when compared with "0.36".

This comment has been minimized.

Copy link
@bep

bep Feb 22, 2018

Author Member

Hmm... I thought I had a test for that elsewhere ... Thanks.

} {
result := funcUnderTest(test.left, test.right)
success := false
Expand Down
4 changes: 3 additions & 1 deletion tpl/compare/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func init() {

ns.AddMethodMapping(ctx.Ge,
[]string{"ge"},
[][2]string{},
[][2]string{
{`{{ if ge .Hugo.Version "0.36" }}Reasonable new Hugo version!{{ end }}`, `Reasonable new Hugo version!`},
},
)

ns.AddMethodMapping(ctx.Gt,
Expand Down
2 changes: 2 additions & 0 deletions tpl/tplimpl/template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ func TestTemplateFuncsExamples(t *testing.T) {
var data struct {
Title string
Section string
Hugo map[string]interface{}
Params map[string]interface{}
}

data.Title = "**BatMan**"
data.Section = "blog"
data.Params = map[string]interface{}{"langCode": "en"}
data.Hugo = map[string]interface{}{"Version": helpers.MustParseHugoVersion("0.36.1").Version()}

for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
ns := nsf(d)
Expand Down

0 comments on commit 0602135

Please sign in to comment.