shithub: hugo

Download patch

ref: b86a605bfb14450d56f4e7faeb4fc5fa8c936438
parent: bbb11a4a0f9b4673ecba52d349e90db8c352e444
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Sep 15 05:32:52 EDT 2016

Make paginate settings configurable per language

Fixes #2449

--- a/helpers/configProvider.go
+++ b/helpers/configProvider.go
@@ -17,9 +17,20 @@
 // 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)
 }
--- a/helpers/language.go
+++ b/helpers/language.go
@@ -84,9 +84,9 @@
 	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))
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -341,7 +341,7 @@
 
 // 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 != "" {
--- a/hugolib/config.go
+++ b/hugolib/config.go
@@ -102,6 +102,7 @@
 	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)
 }
--- a/hugolib/config_test.go
+++ b/hugolib/config_test.go
@@ -16,6 +16,8 @@
 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 @@
 	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"))
 }
--- a/hugolib/hugo_sites_test.go
+++ b/hugolib/hugo_sites_test.go
@@ -141,7 +141,9 @@
 	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 @@
 weight = 30
 title = "På nynorsk"
 languageName = "Nynorsk"
+paginatePath = "side"
 [Languages.nn.Taxonomies]
 lag = "lag"
 [[Languages.nn.menu.main]]
@@ -663,6 +666,7 @@
 weight = 40
 title = "På bokmål"
 languageName = "Bokmål"
+paginatePath = "side"
 [Languages.nb.Taxonomies]
 lag = "lag"
 `
@@ -708,6 +712,7 @@
         weight: 30
         title: "På nynorsk"
         languageName: "Nynorsk"
+        paginatePath: "side"
         Taxonomies:
             lag: "lag"
         menu:
@@ -719,6 +724,7 @@
         weight: 40
         title: "På bokmål"
         languageName: "Bokmål"
+        paginatePath: "side"
         Taxonomies:
             lag: "lag"
 
@@ -771,6 +777,7 @@
     "nn": {
       "weight": 30,
       "title": "På nynorsk",
+      "paginatePath": "side",
       "languageName": "Nynorsk",
       "Taxonomies": {
         "lag": "lag"
@@ -788,6 +795,7 @@
     "nb": {
       "weight": 40,
       "title": "På bokmål",
+      "paginatePath": "side",
       "languageName": "Bokmål",
       "Taxonomies": {
         "lag": "lag"
--- a/hugolib/pagination.go
+++ b/hugolib/pagination.go
@@ -24,7 +24,6 @@
 
 	"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 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 newPaginationURLFactory(pathElements ...string) paginationURLFactory {
-	paginatePath := viper.GetString("paginatePath")
+	paginatePath := helpers.Config().GetString("paginatePath")
 
 	return func(page int) string {
 		var rel string
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1811,7 +1811,7 @@
 
 		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 @@
 
 		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 @@
 	}
 
 	if n.paginator != nil {
-		paginatePath := viper.GetString("paginatePath")
+		paginatePath := helpers.Config().GetString("paginatePath")
 
 		{
 			// write alias for page 1