Make paginate settings configurable per language

Fixes #2449
This commit is contained in:
Bjørn Erik Pedersen 2016-09-15 09:32:52 +02:00
parent bbb11a4a0f
commit b86a605bfb
8 changed files with 32 additions and 11 deletions

View File

@ -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)
}

View 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))

View 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 != "" {

View 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)
}

View 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"))
}

View 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"

View 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

View 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