Introduce HugoSites type

And a Hugo global variable which contains the site under build.

This is really needed to get some level of control of the "multiple languages" in play.

There are still work related to this scattered around, but that will come.

With this commit, the multilingual feature is starting to work.
This commit is contained in:
Bjørn Erik Pedersen
2016-07-26 10:24:27 +02:00
parent 618948e4a8
commit 75dd596e6c
16 changed files with 117 additions and 81 deletions

View File

@@ -57,8 +57,8 @@ func benchmark(cmd *cobra.Command, args []string) error {
return err
}
for i := 0; i < benchmarkTimes; i++ {
Sites = nil
_ = buildSite()
_ = buildSites()
Hugo.Reset()
}
pprof.WriteHeapProfile(f)
f.Close()
@@ -76,8 +76,8 @@ func benchmark(cmd *cobra.Command, args []string) error {
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for i := 0; i < benchmarkTimes; i++ {
Sites = nil
_ = buildSite()
_ = buildSites()
Hugo.Reset()
}
}

View File

@@ -46,10 +46,20 @@ import (
"github.com/spf13/viper"
)
// Sites represents the Hugo sites to build. This variable is exported as it
type HugoSites []*hugolib.Site
// Reset resets the sites, making it ready for a full rebuild.
// TODO(bep) multilingo
func (h HugoSites) Reset() {
for i, s := range h {
h[i] = s.Reset()
}
}
// Hugo represents the Hugo sites to build. This variable is exported as it
// is used by at least one external library (the Hugo caddy plugin). We should
// provide a cleaner external API, but until then, this is it.
var Sites map[string]*hugolib.Site
var Hugo HugoSites
// Reset resets Hugo ready for a new full build. This is mainly only useful
// for benchmark testing etc. via the CLI commands.
@@ -493,7 +503,15 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
helpers.HugoReleaseVersion(), minVersion)
}
return readMultilingualConfiguration()
h, err := readMultilingualConfiguration()
if err != nil {
return err
}
//TODO(bep) refactor ...
Hugo = h
return nil
}
@@ -510,8 +528,8 @@ func watchConfig() {
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
// Force a full rebuild
Sites = nil
utils.CheckErr(buildSite(true))
Hugo.Reset()
utils.CheckErr(buildSites(true))
if !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
livereload.ForceRefresh()
@@ -537,7 +555,7 @@ func build(watches ...bool) error {
if len(watches) > 0 && watches[0] {
watch = true
}
if err := buildSite(buildWatch || watch); err != nil {
if err := buildSites(buildWatch || watch); err != nil {
return fmt.Errorf("Error building site: %s", err)
}
@@ -704,32 +722,21 @@ func getDirList() []string {
return a
}
func buildSite(watching ...bool) (err error) {
func buildSites(watching ...bool) (err error) {
fmt.Println("Started building site")
t0 := time.Now()
if Sites == nil {
Sites = make(map[string]*hugolib.Site)
}
for _, lang := range langConfigsList {
for _, site := range Hugo {
t1 := time.Now()
mainSite, present := Sites[lang.Lang]
if !present {
mainSite = new(hugolib.Site)
Sites[lang.Lang] = mainSite
mainSite.SetMultilingualConfig(lang, langConfigsList)
}
if len(watching) > 0 && watching[0] {
mainSite.RunMode.Watching = true
site.RunMode.Watching = true
}
if err := mainSite.Build(); err != nil {
if err := site.Build(); err != nil {
return err
}
mainSite.Stats(lang.Lang, t1)
site.Stats(t1)
}
jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds()))
@@ -737,18 +744,17 @@ func buildSite(watching ...bool) (err error) {
return nil
}
func rebuildSite(events []fsnotify.Event) error {
func rebuildSites(events []fsnotify.Event) error {
t0 := time.Now()
for _, lang := range langConfigsList {
for _, site := range Hugo {
t1 := time.Now()
site := Sites[lang.Lang]
if err := site.ReBuild(events); err != nil {
return err
}
site.Stats(lang.Lang, t1)
site.Stats(t1)
}
jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds()))
@@ -969,7 +975,7 @@ func NewWatcher(port int) error {
const layout = "2006-01-02 15:04 -0700"
fmt.Println(time.Now().Format(layout))
rebuildSite(dynamicEvents)
rebuildSites(dynamicEvents)
if !buildWatch && !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized

View File

@@ -11,24 +11,30 @@ import (
"github.com/spf13/viper"
)
var langConfigsList hugolib.Languages
func readMultilingualConfiguration() error {
func readMultilingualConfiguration() (HugoSites, error) {
h := make(HugoSites, 0)
multilingual := viper.GetStringMap("Multilingual")
if len(multilingual) == 0 {
// TODO(bep) multilingo langConfigsList = append(langConfigsList, hugolib.NewLanguage("en"))
return nil
h = append(h, hugolib.NewSite(hugolib.NewLanguage("en")))
return h, nil
}
var err error
langConfigsList, err = toSortedLanguages(multilingual)
langConfigsList, err := toSortedLanguages(multilingual)
if err != nil {
return fmt.Errorf("Failed to parse multilingual config: %s", err)
return nil, fmt.Errorf("Failed to parse multilingual config: %s", err)
}
return nil
for _, lang := range langConfigsList {
s := hugolib.NewSite(lang)
s.SetMultilingualConfig(lang, langConfigsList)
h = append(h, s)
}
return h, nil
}
func toSortedLanguages(l map[string]interface{}) (hugolib.Languages, error) {