Skip to content

Commit

Permalink
Create a struct with all of Hugo's config options
Browse files Browse the repository at this point in the history
Primary motivation is documentation, but it will also hopefully simplify the code.
  • Loading branch information
bep committed Feb 26, 2023
1 parent 2662faf commit ff96321
Show file tree
Hide file tree
Showing 84 changed files with 2,546 additions and 1,313 deletions.
2 changes: 2 additions & 0 deletions cache/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cache contains the differenct cache implementations.
package cache
9 changes: 5 additions & 4 deletions cache/filecache/filecache_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package filecache provides a file based cache for Hugo.
package filecache

import (
Expand Down Expand Up @@ -39,7 +40,7 @@ const (
cacheDirProject = ":cacheDir/:project"
)

var defaultCacheConfig = Config{
var defaultCacheConfig = FileCacheConfig{
MaxAge: -1, // Never expire
Dir: cacheDirProject,
}
Expand All @@ -53,7 +54,7 @@ const (
cacheKeyGetResource = "getresource"
)

type Configs map[string]Config
type Configs map[string]FileCacheConfig

func (c Configs) CacheDirModules() string {
return c[cacheKeyModules].Dir
Expand All @@ -74,13 +75,13 @@ var defaultCacheConfigs = Configs{
MaxAge: -1,
Dir: resourcesGenDir,
},
cacheKeyGetResource: Config{
cacheKeyGetResource: FileCacheConfig{
MaxAge: -1, // Never expire
Dir: cacheDirProject,
},
}

type Config struct {
type FileCacheConfig struct {
// Max age of cache entries in this cache. Any items older than this will
// be removed and not returned from the cache.
// a negative value means forever, 0 means cache is disabled.
Expand Down
47 changes: 47 additions & 0 deletions common/hstrings/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2019 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 hstrings

import (
"fmt"
"strings"

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

var _ compare.Eqer = StringEqualFold("")

// StringEqualFold is a string that implements the compare.Eqer interface and considers
// two strings equal if they are equal when folded to lower case.
// The compare.Eqer interface is used in Hugo to compare values in templates (e.g. using the eq template function).
type StringEqualFold string

func (s StringEqualFold) EqualFold(s2 string) bool {
return strings.EqualFold(string(s), s2)
}

func (s StringEqualFold) String() string {
return string(s)
}

func (s StringEqualFold) Eq(s2 any) bool {
switch ss := s2.(type) {
case string:
return s.EqualFold(ss)
case fmt.Stringer:
return s.EqualFold(ss.String())
}

return false
}
36 changes: 36 additions & 0 deletions common/hstrings/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 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 hstrings

import (
"testing"

qt "github.com/frankban/quicktest"
)

func TestStringEqualFold(t *testing.T) {
c := qt.New(t)

s1 := "A"
s2 := "a"

c.Assert(StringEqualFold(s1).EqualFold(s2), qt.Equals, true)
c.Assert(StringEqualFold(s1).EqualFold(s1), qt.Equals, true)
c.Assert(StringEqualFold(s2).EqualFold(s1), qt.Equals, true)
c.Assert(StringEqualFold(s2).EqualFold(s2), qt.Equals, true)
c.Assert(StringEqualFold(s1).EqualFold("b"), qt.Equals, false)
c.Assert(StringEqualFold(s1).Eq(s2), qt.Equals, true)
c.Assert(StringEqualFold(s1).Eq("b"), qt.Equals, false)

}
17 changes: 17 additions & 0 deletions common/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ func LookupEqualFold[T any | string](m map[string]T, key string) (T, bool) {
return s, false
}

// MergeShallow merges src into dst, but only if the key does not already exist in dst.
// The keys are compared case insensitively.
func MergeShallow(dst, src map[string]any) {
for k, v := range src {
found := false
for dk := range dst {
if strings.EqualFold(dk, k) {
found = true
break
}
}
if !found {
dst[k] = v
}
}
}

type keyRename struct {
pattern glob.Glob
newKey string
Expand Down
2 changes: 1 addition & 1 deletion common/maps/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p Params) IsZero() bool {
return false
}

for k, _ := range p {
for k := range p {
return k == mergeStrategyKey
}

Expand Down
Loading

0 comments on commit ff96321

Please sign in to comment.