Make paginate settings configurable per language
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Sep 2016 07:32:52 +0000 (09:32 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Sep 2016 07:32:52 +0000 (09:32 +0200)
Fixes #2449

helpers/configProvider.go
helpers/language.go
helpers/path.go
hugolib/config.go
hugolib/config_test.go
hugolib/hugo_sites_test.go
hugolib/pagination.go
hugolib/site.go

index 16a20330290cd22c2884fccd69a4969c41bd4440..35500f6de73d857222cbe1db376c68229468c94b 100644 (file)
 // string operations on content.
 package helpers
 
+import (
+       "github.com/spf13/viper"
+)
+
 // ConfigProvider provides the configuration settings for Hugo.
 type ConfigProvider interface {
        GetString(key string) string
+       GetInt(key string) int
        GetStringMap(key string) map[string]interface{}
        GetStringMapString(key string) map[string]string
 }
+
+// Config returns the currently active Hugo config. This will be set
+// per site (language) rendered.
+func Config() ConfigProvider {
+       return viper.Get("CurrentContentLanguage").(ConfigProvider)
+}
index c822dedd0cb1bffd7c268ba18174b4e5a804c9e6..9ebec5a65dc0d86ab2aea55dde0ea44716717fa9 100644 (file)
@@ -84,9 +84,9 @@ func (l *Language) SetParam(k string, v interface{}) {
        l.params[k] = v
 }
 
-func (l *Language) GetBool(key string) bool { return cast.ToBool(l.Get(key)) }
-
+func (l *Language) GetBool(key string) bool     { return cast.ToBool(l.Get(key)) }
 func (l *Language) GetString(key string) string { return cast.ToString(l.Get(key)) }
+func (l *Language) GetInt(key string) int       { return cast.ToInt(l.Get(key)) }
 
 func (ml *Language) GetStringMap(key string) map[string]interface{} {
        return cast.ToStringMap(ml.Get(key))
index b7e6f51a9bc7673000d926b1d1375ea6802d75ba..b8f6424709b938a4d2ee62012aa800d9143352b5 100644 (file)
@@ -341,7 +341,7 @@ func GetRelativePath(path, base string) (final string, err error) {
 
 // PaginateAliasPath creates a path used to access the aliases in the paginator.
 func PaginateAliasPath(base string, page int) string {
-       paginatePath := viper.GetString("paginatePath")
+       paginatePath := Config().GetString("paginatePath")
        uglify := viper.GetBool("UglyURLs")
        var p string
        if base != "" {
index d0d34ac55b422278b37813d1bc8e4d1080f608ed..d31a38a5acd065739fbfd9c1fd35c015cca25492 100644 (file)
@@ -102,6 +102,7 @@ func loadDefaultSettings() {
        viper.SetDefault("EnableEmoji", false)
        viper.SetDefault("PygmentsCodeFencesGuessSyntax", false)
        viper.SetDefault("UseModTimeAsFallback", false)
+       viper.SetDefault("CurrentContentLanguage", helpers.NewDefaultLanguage())
        viper.SetDefault("DefaultContentLanguage", "en")
        viper.SetDefault("DefaultContentLanguageInSubdir", false)
 }
index 7bbb2670a415c974cc2e171c04990b9a584833bc..8c307bd257d921c9a727b2129926fe520f1c6f47 100644 (file)
@@ -16,6 +16,8 @@ package hugolib
 import (
        "testing"
 
+       "github.com/spf13/hugo/helpers"
+
        "github.com/spf13/viper"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
@@ -31,7 +33,7 @@ func TestLoadGlobalConfig(t *testing.T) {
        writeSource(t, "hugo.toml", configContent)
 
        require.NoError(t, LoadGlobalConfig("", "hugo.toml"))
-       assert.Equal(t, "side", viper.GetString("PaginatePath"))
+       assert.Equal(t, "side", helpers.Config().GetString("paginatePath"))
        // default
        assert.Equal(t, "layouts", viper.GetString("LayoutDir"))
 }
index 52a3910a363297bf9350cd4e5207794618076fbc..5df295d5549fc01e5e947e562871da1045000f64 100644 (file)
@@ -141,7 +141,9 @@ func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) {
        assertFileContent(t, "public/en/tags/tag1/page/1/index.html", defaultInSubDir, `refresh" content="0; url=http://example.com/blog/en/tags/tag1/"`)
        assertFileContent(t, "public/fr/plaques/frtag1/page/2/index.html", defaultInSubDir, "List Page 2", "Bonjour", "http://example.com/blog/fr/plaques/frtag1/")
        assertFileContent(t, "public/en/tags/tag1/page/2/index.html", defaultInSubDir, "List Page 2", "Hello", "http://example.com/blog/en/tags/tag1/")
-
+       // nn (Nynorsk) and nb (Bokmål) have custom pagePath: side ("page" in Norwegian)
+       assertFileContent(t, "public/nn/side/1/index.html", defaultInSubDir, `refresh" content="0; url=http://example.com/blog/nn/"`)
+       assertFileContent(t, "public/nb/side/1/index.html", defaultInSubDir, `refresh" content="0; url=http://example.com/blog/nb/"`)
 }
 
 func replaceDefaultContentLanguageValue(value string, defaultInSubDir bool) string {
@@ -652,6 +654,7 @@ plaque = "plaques"
 weight = 30
 title = "På nynorsk"
 languageName = "Nynorsk"
+paginatePath = "side"
 [Languages.nn.Taxonomies]
 lag = "lag"
 [[Languages.nn.menu.main]]
@@ -663,6 +666,7 @@ weight = 1
 weight = 40
 title = "På bokmål"
 languageName = "Bokmål"
+paginatePath = "side"
 [Languages.nb.Taxonomies]
 lag = "lag"
 `
@@ -708,6 +712,7 @@ Languages:
         weight: 30
         title: "På nynorsk"
         languageName: "Nynorsk"
+        paginatePath: "side"
         Taxonomies:
             lag: "lag"
         menu:
@@ -719,6 +724,7 @@ Languages:
         weight: 40
         title: "På bokmål"
         languageName: "Bokmål"
+        paginatePath: "side"
         Taxonomies:
             lag: "lag"
 
@@ -771,6 +777,7 @@ var multiSiteJSONConfig = `
     "nn": {
       "weight": 30,
       "title": "På nynorsk",
+      "paginatePath": "side",
       "languageName": "Nynorsk",
       "Taxonomies": {
         "lag": "lag"
@@ -788,6 +795,7 @@ var multiSiteJSONConfig = `
     "nb": {
       "weight": 40,
       "title": "På bokmål",
+      "paginatePath": "side",
       "languageName": "Bokmål",
       "Taxonomies": {
         "lag": "lag"
index f7f9cbf8c2fd906f890fd7b750ef039659cec4e5..5bba5d89fb02d0ef4fb4f6b0e60f907bd6e52fc3 100644 (file)
@@ -24,7 +24,6 @@ import (
 
        "github.com/spf13/cast"
        "github.com/spf13/hugo/helpers"
-       "github.com/spf13/viper"
 )
 
 // Pager represents one of the elements in a paginator.
@@ -358,7 +357,7 @@ func (n *Node) Paginate(seq interface{}, options ...interface{}) (*Pager, error)
 
 func resolvePagerSize(options ...interface{}) (int, error) {
        if len(options) == 0 {
-               return viper.GetInt("paginate"), nil
+               return helpers.Config().GetInt("paginate"), nil
        }
 
        if len(options) > 1 {
@@ -509,7 +508,7 @@ func newPaginator(elements []paginatedElement, total, size int, urlFactory pagin
 }
 
 func newPaginationURLFactory(pathElements ...string) paginationURLFactory {
-       paginatePath := viper.GetString("paginatePath")
+       paginatePath := helpers.Config().GetString("paginatePath")
 
        return func(page int) string {
                var rel string
index c46604422abb045fb6e6aea1c96b5dc9b1dedb0b..87c440d382b5eedb6ee2ea4e90a65b448ff0fbb3 100644 (file)
@@ -1811,7 +1811,7 @@ func taxonomyRenderer(prepare bool, s *Site, taxes <-chan taxRenderInfo, results
 
                if n.paginator != nil {
 
-                       paginatePath = viper.GetString("paginatePath")
+                       paginatePath = helpers.Config().GetString("paginatePath")
 
                        // write alias for page 1
                        s.writeDestAlias(helpers.PaginateAliasPath(baseWithLanguagePrefix, 1), n.Permalink())
@@ -1946,7 +1946,7 @@ func (s *Site) renderSectionLists(prepare bool) error {
 
                if n.paginator != nil {
 
-                       paginatePath := viper.GetString("paginatePath")
+                       paginatePath := helpers.Config().GetString("paginatePath")
 
                        // write alias for page 1
                        s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base))
@@ -2006,7 +2006,7 @@ func (s *Site) renderHomePage(prepare bool) error {
        }
 
        if n.paginator != nil {
-               paginatePath := viper.GetString("paginatePath")
+               paginatePath := helpers.Config().GetString("paginatePath")
 
                {
                        // write alias for page 1