mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-25 00:11:27 +00:00
parent
bbb11a4a0f
commit
b86a605bfb
8 changed files with 32 additions and 11 deletions
|
@ -17,9 +17,20 @@
|
||||||
// string operations on content.
|
// string operations on content.
|
||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
// ConfigProvider provides the configuration settings for Hugo.
|
// ConfigProvider provides the configuration settings for Hugo.
|
||||||
type ConfigProvider interface {
|
type ConfigProvider interface {
|
||||||
GetString(key string) string
|
GetString(key string) string
|
||||||
|
GetInt(key string) int
|
||||||
GetStringMap(key string) map[string]interface{}
|
GetStringMap(key string) map[string]interface{}
|
||||||
GetStringMapString(key string) map[string]string
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -84,9 +84,9 @@ func (l *Language) SetParam(k string, v interface{}) {
|
||||||
l.params[k] = v
|
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) 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{} {
|
func (ml *Language) GetStringMap(key string) map[string]interface{} {
|
||||||
return cast.ToStringMap(ml.Get(key))
|
return cast.ToStringMap(ml.Get(key))
|
||||||
|
|
|
@ -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.
|
// PaginateAliasPath creates a path used to access the aliases in the paginator.
|
||||||
func PaginateAliasPath(base string, page int) string {
|
func PaginateAliasPath(base string, page int) string {
|
||||||
paginatePath := viper.GetString("paginatePath")
|
paginatePath := Config().GetString("paginatePath")
|
||||||
uglify := viper.GetBool("UglyURLs")
|
uglify := viper.GetBool("UglyURLs")
|
||||||
var p string
|
var p string
|
||||||
if base != "" {
|
if base != "" {
|
||||||
|
|
|
@ -102,6 +102,7 @@ func loadDefaultSettings() {
|
||||||
viper.SetDefault("EnableEmoji", false)
|
viper.SetDefault("EnableEmoji", false)
|
||||||
viper.SetDefault("PygmentsCodeFencesGuessSyntax", false)
|
viper.SetDefault("PygmentsCodeFencesGuessSyntax", false)
|
||||||
viper.SetDefault("UseModTimeAsFallback", false)
|
viper.SetDefault("UseModTimeAsFallback", false)
|
||||||
|
viper.SetDefault("CurrentContentLanguage", helpers.NewDefaultLanguage())
|
||||||
viper.SetDefault("DefaultContentLanguage", "en")
|
viper.SetDefault("DefaultContentLanguage", "en")
|
||||||
viper.SetDefault("DefaultContentLanguageInSubdir", false)
|
viper.SetDefault("DefaultContentLanguageInSubdir", false)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ package hugolib
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/hugo/helpers"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -31,7 +33,7 @@ func TestLoadGlobalConfig(t *testing.T) {
|
||||||
writeSource(t, "hugo.toml", configContent)
|
writeSource(t, "hugo.toml", configContent)
|
||||||
|
|
||||||
require.NoError(t, LoadGlobalConfig("", "hugo.toml"))
|
require.NoError(t, LoadGlobalConfig("", "hugo.toml"))
|
||||||
assert.Equal(t, "side", viper.GetString("PaginatePath"))
|
assert.Equal(t, "side", helpers.Config().GetString("paginatePath"))
|
||||||
// default
|
// default
|
||||||
assert.Equal(t, "layouts", viper.GetString("LayoutDir"))
|
assert.Equal(t, "layouts", viper.GetString("LayoutDir"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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/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/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/")
|
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 {
|
func replaceDefaultContentLanguageValue(value string, defaultInSubDir bool) string {
|
||||||
|
@ -652,6 +654,7 @@ plaque = "plaques"
|
||||||
weight = 30
|
weight = 30
|
||||||
title = "På nynorsk"
|
title = "På nynorsk"
|
||||||
languageName = "Nynorsk"
|
languageName = "Nynorsk"
|
||||||
|
paginatePath = "side"
|
||||||
[Languages.nn.Taxonomies]
|
[Languages.nn.Taxonomies]
|
||||||
lag = "lag"
|
lag = "lag"
|
||||||
[[Languages.nn.menu.main]]
|
[[Languages.nn.menu.main]]
|
||||||
|
@ -663,6 +666,7 @@ weight = 1
|
||||||
weight = 40
|
weight = 40
|
||||||
title = "På bokmål"
|
title = "På bokmål"
|
||||||
languageName = "Bokmål"
|
languageName = "Bokmål"
|
||||||
|
paginatePath = "side"
|
||||||
[Languages.nb.Taxonomies]
|
[Languages.nb.Taxonomies]
|
||||||
lag = "lag"
|
lag = "lag"
|
||||||
`
|
`
|
||||||
|
@ -708,6 +712,7 @@ Languages:
|
||||||
weight: 30
|
weight: 30
|
||||||
title: "På nynorsk"
|
title: "På nynorsk"
|
||||||
languageName: "Nynorsk"
|
languageName: "Nynorsk"
|
||||||
|
paginatePath: "side"
|
||||||
Taxonomies:
|
Taxonomies:
|
||||||
lag: "lag"
|
lag: "lag"
|
||||||
menu:
|
menu:
|
||||||
|
@ -719,6 +724,7 @@ Languages:
|
||||||
weight: 40
|
weight: 40
|
||||||
title: "På bokmål"
|
title: "På bokmål"
|
||||||
languageName: "Bokmål"
|
languageName: "Bokmål"
|
||||||
|
paginatePath: "side"
|
||||||
Taxonomies:
|
Taxonomies:
|
||||||
lag: "lag"
|
lag: "lag"
|
||||||
|
|
||||||
|
@ -771,6 +777,7 @@ var multiSiteJSONConfig = `
|
||||||
"nn": {
|
"nn": {
|
||||||
"weight": 30,
|
"weight": 30,
|
||||||
"title": "På nynorsk",
|
"title": "På nynorsk",
|
||||||
|
"paginatePath": "side",
|
||||||
"languageName": "Nynorsk",
|
"languageName": "Nynorsk",
|
||||||
"Taxonomies": {
|
"Taxonomies": {
|
||||||
"lag": "lag"
|
"lag": "lag"
|
||||||
|
@ -788,6 +795,7 @@ var multiSiteJSONConfig = `
|
||||||
"nb": {
|
"nb": {
|
||||||
"weight": 40,
|
"weight": 40,
|
||||||
"title": "På bokmål",
|
"title": "På bokmål",
|
||||||
|
"paginatePath": "side",
|
||||||
"languageName": "Bokmål",
|
"languageName": "Bokmål",
|
||||||
"Taxonomies": {
|
"Taxonomies": {
|
||||||
"lag": "lag"
|
"lag": "lag"
|
||||||
|
|
|
@ -24,7 +24,6 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
"github.com/spf13/hugo/helpers"
|
"github.com/spf13/hugo/helpers"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pager represents one of the elements in a paginator.
|
// 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) {
|
func resolvePagerSize(options ...interface{}) (int, error) {
|
||||||
if len(options) == 0 {
|
if len(options) == 0 {
|
||||||
return viper.GetInt("paginate"), nil
|
return helpers.Config().GetInt("paginate"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(options) > 1 {
|
if len(options) > 1 {
|
||||||
|
@ -509,7 +508,7 @@ func newPaginator(elements []paginatedElement, total, size int, urlFactory pagin
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPaginationURLFactory(pathElements ...string) paginationURLFactory {
|
func newPaginationURLFactory(pathElements ...string) paginationURLFactory {
|
||||||
paginatePath := viper.GetString("paginatePath")
|
paginatePath := helpers.Config().GetString("paginatePath")
|
||||||
|
|
||||||
return func(page int) string {
|
return func(page int) string {
|
||||||
var rel string
|
var rel string
|
||||||
|
|
|
@ -1811,7 +1811,7 @@ func taxonomyRenderer(prepare bool, s *Site, taxes <-chan taxRenderInfo, results
|
||||||
|
|
||||||
if n.paginator != nil {
|
if n.paginator != nil {
|
||||||
|
|
||||||
paginatePath = viper.GetString("paginatePath")
|
paginatePath = helpers.Config().GetString("paginatePath")
|
||||||
|
|
||||||
// write alias for page 1
|
// write alias for page 1
|
||||||
s.writeDestAlias(helpers.PaginateAliasPath(baseWithLanguagePrefix, 1), n.Permalink())
|
s.writeDestAlias(helpers.PaginateAliasPath(baseWithLanguagePrefix, 1), n.Permalink())
|
||||||
|
@ -1946,7 +1946,7 @@ func (s *Site) renderSectionLists(prepare bool) error {
|
||||||
|
|
||||||
if n.paginator != nil {
|
if n.paginator != nil {
|
||||||
|
|
||||||
paginatePath := viper.GetString("paginatePath")
|
paginatePath := helpers.Config().GetString("paginatePath")
|
||||||
|
|
||||||
// write alias for page 1
|
// write alias for page 1
|
||||||
s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base))
|
s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base))
|
||||||
|
@ -2006,7 +2006,7 @@ func (s *Site) renderHomePage(prepare bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.paginator != nil {
|
if n.paginator != nil {
|
||||||
paginatePath := viper.GetString("paginatePath")
|
paginatePath := helpers.Config().GetString("paginatePath")
|
||||||
|
|
||||||
{
|
{
|
||||||
// write alias for page 1
|
// write alias for page 1
|
||||||
|
|
Loading…
Reference in a new issue