Skip to content

Commit

Permalink
Make taxonomies configurable per language
Browse files Browse the repository at this point in the history
See #2312
  • Loading branch information
bep committed Aug 20, 2016
1 parent df2a13f commit 0cb0ed9
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 113 deletions.
4 changes: 2 additions & 2 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,12 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
helpers.HugoReleaseVersion(), minVersion)
}

h, err := readMultilingualConfiguration()
h, err := hugolib.NewHugoSitesFromConfiguration()

if err != nil {
return err
}
//TODO(bep) refactor ...
//TODO(bep) ml refactor ...
Hugo = h

return nil
Expand Down
75 changes: 0 additions & 75 deletions commands/multilingual.go

This file was deleted.

19 changes: 19 additions & 0 deletions docs/content/content/multilingual.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ and taxonomy pages will be rendered below `/en` in English, and below `/fr` in F

Only the obvious non-global options can be overridden per language. Examples of global options are `BaseURL`, `BuildDrafts`, etc.

Taxonomies configuration can also be set per language, example:

```
[Taxonomies]
tag = "tags"
[Languages]
[Languages.en]
weight = 1
title = "English"
[Languages.fr]
weight = 2
title = "Français"
[Languages.fr.Taxonomies]
plaque = "plaques"
```


### Translating your content

Translated articles are identified by the name of the content file.
Expand Down
4 changes: 2 additions & 2 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestLoadGlobalConfig(t *testing.T) {
PaginatePath = "side"
`

writeSource(t, "config.toml", configContent)
writeSource(t, "hugo.toml", configContent)

require.NoError(t, LoadGlobalConfig("", "config.toml"))
require.NoError(t, LoadGlobalConfig("", "hugo.toml"))
assert.Equal(t, "side", viper.GetString("PaginatePath"))
// default
assert.Equal(t, "layouts", viper.GetString("LayoutDir"))
Expand Down
33 changes: 31 additions & 2 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package hugolib

import (
"errors"
"fmt"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -53,6 +54,34 @@ func NewHugoSites(sites ...*Site) (*HugoSites, error) {
return &HugoSites{Multilingual: langConfig, Sites: sites}, nil
}

// NewHugoSitesFromConfiguration creates HugoSites from the global Viper config.
func NewHugoSitesFromConfiguration() (*HugoSites, error) {
sites := make([]*Site, 0)
multilingual := viper.GetStringMap("Languages")
if len(multilingual) == 0 {
// TODO(bep) multilingo langConfigsList = append(langConfigsList, NewLanguage("en"))
sites = append(sites, NewSite(NewLanguage("en")))
}

if len(multilingual) > 0 {
var err error

languages, err := toSortedLanguages(multilingual)

if err != nil {
return nil, fmt.Errorf("Failed to parse multilingual config: %s", err)
}

for _, lang := range languages {
sites = append(sites, NewSite(lang))
}

}

return NewHugoSites(sites...)

}

// Reset resets the sites, making it ready for a full rebuild.
// TODO(bep) multilingo
func (h HugoSites) Reset() {
Expand All @@ -61,7 +90,7 @@ func (h HugoSites) Reset() {
}
}

func (h HugoSites) siteInfos() []*SiteInfo {
func (h HugoSites) toSiteInfos() []*SiteInfo {
infos := make([]*SiteInfo, len(h.Sites))
for i, s := range h.Sites {
infos[i] = &s.Info
Expand Down Expand Up @@ -220,7 +249,7 @@ func (h *HugoSites) render() error {
smLayouts := []string{"sitemapindex.xml", "_default/sitemapindex.xml", "_internal/_default/sitemapindex.xml"}

if err := s.renderAndWriteXML("sitemapindex", sitemapDefault.Filename,
h.siteInfos(), s.appendThemeTemplates(smLayouts)...); err != nil {
h.toSiteInfos(), s.appendThemeTemplates(smLayouts)...); err != nil {
return err
}

Expand Down
80 changes: 53 additions & 27 deletions hugolib/hugo_sites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (

func init() {
testCommonResetState()
jww.SetStdoutThreshold(jww.LevelError)

jww.SetStdoutThreshold(jww.LevelCritical)
}

func testCommonResetState() {
Expand All @@ -38,8 +37,8 @@ func testCommonResetState() {

}

func TestMultiSites(t *testing.T) {

func TestMultiSitesBuild(t *testing.T) {
testCommonResetState()
sites := createMultiTestSites(t)

err := sites.Build(BuildCfg{})
Expand Down Expand Up @@ -128,10 +127,20 @@ func TestMultiSites(t *testing.T) {
sitemapFr := readDestination(t, "public/fr/sitemap.xml")
require.True(t, strings.Contains(sitemapEn, "http://example.com/blog/en/sect/doc2/"), sitemapEn)
require.True(t, strings.Contains(sitemapFr, "http://example.com/blog/fr/sect/doc1/"), sitemapFr)

// Check taxonomies
enTags := enSite.Taxonomies["tags"]
frTags := frSite.Taxonomies["plaques"]
require.Len(t, enTags, 2, fmt.Sprintf("Tags in en: %=v", enTags))
require.Len(t, frTags, 2, fmt.Sprintf("Tags in fr: %=v", frTags))
require.NotNil(t, enTags["tag1"])
require.NotNil(t, frTags["frtag1"])
readDestination(t, "public/fr/plaques/frtag1/index.html")
readDestination(t, "public/en/tags/tag1/index.html")
}

func TestMultiSitesRebuild(t *testing.T) {

testCommonResetState()
sites := createMultiTestSites(t)
cfg := BuildCfg{}

Expand Down Expand Up @@ -294,16 +303,6 @@ func TestMultiSitesRebuild(t *testing.T) {
}

func createMultiTestSites(t *testing.T) *HugoSites {
// General settings
hugofs.InitMemFs()

viper.Set("DefaultExtension", "html")
viper.Set("baseurl", "http://example.com/blog")
viper.Set("DisableSitemap", false)
viper.Set("DisableRSS", false)
viper.Set("RSSUri", "index.xml")
viper.Set("Taxonomies", map[string]string{"tag": "tags"})
viper.Set("Permalinks", map[string]string{"other": "/somewhere/else/:filename"})

// Add some layouts
if err := afero.WriteFile(hugofs.Source(),
Expand Down Expand Up @@ -362,9 +361,9 @@ NOTE: slug should be used as URL
`)},
{filepath.FromSlash("sect/doc1.fr.md"), []byte(`---
title: doc1
tags:
- tag1
- tag2
plaques:
- frtag1
- frtag2
publishdate: "2000-01-04"
---
# doc1
Expand Down Expand Up @@ -393,8 +392,8 @@ NOTE: third 'en' doc, should trigger pagination on home page.
`)},
{filepath.FromSlash("sect/doc4.md"), []byte(`---
title: doc4
tags:
- tag1
plaques:
- frtag1
publishdate: "2000-01-05"
---
# doc4
Expand Down Expand Up @@ -446,13 +445,39 @@ draft: true
`)},
}

// Multilingual settings
viper.Set("Multilingual", true)
en := NewLanguage("en")
viper.Set("DefaultContentLanguage", "fr")
viper.Set("paginate", "2")
tomlConfig := `
DefaultExtension = "html"
baseurl = "http://example.com/blog"
DisableSitemap = false
DisableRSS = false
RSSUri = "index.xml"
paginate = 2
DefaultContentLanguage = "fr"
[permalinks]
other = "/somewhere/else/:filename"
[Taxonomies]
tag = "tags"
languages := NewLanguages(en, NewLanguage("fr"))
[Languages]
[Languages.en]
weight = 1
title = "English"
[Languages.fr]
weight = 2
title = "Français"
[Languages.fr.Taxonomies]
plaque = "plaques"
`

writeSource(t, "multilangconfig.toml", tomlConfig)
if err := LoadGlobalConfig("", "multilangconfig.toml"); err != nil {
t.Fatalf("Failed to load config: %s", err)
}

// Hugo support using ByteSource's directly (for testing),
// but to make it more real, we write them to the mem file system.
Expand All @@ -466,7 +491,8 @@ draft: true
if err != nil {
t.Fatalf("Unable to locate file")
}
sites, err := newHugoSitesFromLanguages(languages)

sites, err := NewHugoSitesFromConfiguration()

if err != nil {
t.Fatalf("Failed to create sites: %s", err)
Expand Down
38 changes: 38 additions & 0 deletions hugolib/multilingual.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sort"
"strings"

"fmt"

"github.com/spf13/cast"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -112,3 +114,39 @@ func (s *Site) currentLanguageString() string {
func (s *Site) currentLanguage() *Language {
return s.Language
}

func toSortedLanguages(l map[string]interface{}) (Languages, error) {
langs := make(Languages, len(l))
i := 0

for lang, langConf := range l {
langsMap, ok := langConf.(map[string]interface{})

if !ok {
return nil, fmt.Errorf("Language config is not a map: %v", langsMap)
}

language := NewLanguage(lang)

for k, v := range langsMap {
loki := strings.ToLower(k)
switch loki {
case "title":
language.Title = cast.ToString(v)
case "weight":
language.Weight = cast.ToInt(v)
}

// Put all into the Params map
// TODO(bep) reconsile with the type handling etc. from other params handlers.
language.SetParam(loki, v)
}

langs[i] = language
i++
}

sort.Sort(langs)

return langs, nil
}
Loading

0 comments on commit 0cb0ed9

Please sign in to comment.