From b86a605bfb14450d56f4e7faeb4fc5fa8c936438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 15 Sep 2016 09:32:52 +0200 Subject: [PATCH] Make paginate settings configurable per language Fixes #2449 --- helpers/configProvider.go | 11 +++++++++++ helpers/language.go | 4 ++-- helpers/path.go | 2 +- hugolib/config.go | 1 + hugolib/config_test.go | 4 +++- hugolib/hugo_sites_test.go | 10 +++++++++- hugolib/pagination.go | 5 ++--- hugolib/site.go | 6 +++--- 8 files changed, 32 insertions(+), 11 deletions(-) diff --git a/helpers/configProvider.go b/helpers/configProvider.go index 16a203302..35500f6de 100644 --- 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) +} diff --git a/helpers/language.go b/helpers/language.go index c822dedd0..9ebec5a65 100644 --- a/helpers/language.go +++ b/helpers/language.go @@ -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)) diff --git a/helpers/path.go b/helpers/path.go index b7e6f51a9..b8f642470 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -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 != "" { diff --git a/hugolib/config.go b/hugolib/config.go index d0d34ac55..d31a38a5a 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -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) } diff --git a/hugolib/config_test.go b/hugolib/config_test.go index 7bbb2670a..8c307bd25 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -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")) } diff --git a/hugolib/hugo_sites_test.go b/hugolib/hugo_sites_test.go index 52a3910a3..5df295d55 100644 --- a/hugolib/hugo_sites_test.go +++ b/hugolib/hugo_sites_test.go @@ -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" diff --git a/hugolib/pagination.go b/hugolib/pagination.go index f7f9cbf8c..5bba5d89f 100644 --- a/hugolib/pagination.go +++ b/hugolib/pagination.go @@ -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 diff --git a/hugolib/site.go b/hugolib/site.go index c46604422..87c440d38 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -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