shithub: hugo

Download patch

ref: a823b1572ba93444fe60eeba7c2c7b500ac2cd67
parent: 28696b5dca6b14cfb0935901163d07ce216f9903
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Aug 12 14:17:00 EDT 2016

Set lang template globals for each site when render shortcodes

We should get rid of these globals, but that is another month.

--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -394,6 +394,9 @@
 func (h *HugoSites) preRender() error {
 
 	for _, s := range h.Sites {
+		if err := s.setCurrentLanguageConfig(); err != nil {
+			return err
+		}
 		// Run "render prepare"
 		if err := s.renderHomePage(true); err != nil {
 			return err
@@ -409,13 +412,20 @@
 		}
 	}
 
-	pageChan := make(chan *Page)
+	for _, s := range h.Sites {
+		if err := s.setCurrentLanguageConfig(); err != nil {
+			return err
+		}
+		renderShortcodesForSite(s)
+	}
 
+	return nil
+}
+
+func renderShortcodesForSite(s *Site) {
+	pageChan := make(chan *Page)
 	wg := &sync.WaitGroup{}
 
-	// We want all the pages, so just pick one.
-	s := h.Sites[0]
-
 	for i := 0; i < getGoMaxProcs()*4; i++ {
 		wg.Add(1)
 		go func(pages <-chan *Page, wg *sync.WaitGroup) {
@@ -456,7 +466,7 @@
 		}(pageChan, wg)
 	}
 
-	for _, p := range s.AllPages {
+	for _, p := range s.Pages {
 		pageChan <- p
 	}
 
@@ -464,7 +474,6 @@
 
 	wg.Wait()
 
-	return nil
 }
 
 // Pages returns all pages for all sites.
--- a/hugolib/hugo_sites_test.go
+++ b/hugolib/hugo_sites_test.go
@@ -258,6 +258,10 @@
 	assertFileContent(t, "public/en/index.html", true, "Home Page 1", "Hello", "Hugo Rocks!")
 	assertFileContent(t, "public/fr/index.html", true, "Home Page 1", "Bonjour", "Hugo Rocks!")
 
+	// check single page content
+	assertFileContent(t, "public/fr/sect/doc1/index.html", true, "Single", "Shortcode: Bonjour")
+	assertFileContent(t, "public/en/sect/doc1-slug/index.html", true, "Single", "Shortcode: Hello")
+
 	// Check node translations
 	homeEn := enSite.getNode("home-0")
 	require.NotNil(t, homeEn)
@@ -566,8 +570,6 @@
 	require.Len(t, svPage.Translations(), 2)
 	require.Len(t, svPage.AllTranslations(), 3)
 	require.Equal(t, "en", svPage.Translations()[0].Lang())
-	//noFile := readDestination(t, "/public/no/doc1/index.html")
-	//require.True(t, strings.Contains("foo", noFile), noFile)
 
 }
 
@@ -719,7 +721,7 @@
 	// Add some layouts
 	if err := afero.WriteFile(hugofs.Source(),
 		filepath.Join("layouts", "_default/single.html"),
-		[]byte("Single: {{ .Title }}|{{ i18n \"hello\" }} {{ .Content }}"),
+		[]byte("Single: {{ .Title }}|{{ i18n \"hello\" }}|{{.Lang}}|{{ .Content }}"),
 		0755); err != nil {
 		t.Fatalf("Failed to write layout file: %s", err)
 	}
@@ -738,6 +740,14 @@
 		t.Fatalf("Failed to write layout file: %s", err)
 	}
 
+	// Add a shortcode
+	if err := afero.WriteFile(hugofs.Source(),
+		filepath.Join("layouts", "shortcodes", "shortcode.html"),
+		[]byte("Shortcode: {{ i18n \"hello\" }}"),
+		0755); err != nil {
+		t.Fatalf("Failed to write layout file: %s", err)
+	}
+
 	// Add some language files
 	if err := afero.WriteFile(hugofs.Source(),
 		filepath.Join("i18n", "en.yaml"),
@@ -769,6 +779,9 @@
 ---
 # doc1
 *some "content"*
+
+{{< shortcode >}}
+
 NOTE: slug should be used as URL
 `)},
 		{filepath.FromSlash("sect/doc1.fr.md"), []byte(`---
@@ -780,6 +793,9 @@
 ---
 # doc1
 *quelque "contenu"*
+
+{{< shortcode >}}
+
 NOTE: should be in the 'en' Page's 'Translations' field.
 NOTE: date is after "doc3"
 `)},
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -776,11 +776,15 @@
 	}
 }
 
-func (s *Site) render() (err error) {
+func (s *Site) setCurrentLanguageConfig() error {
 	// There are sadly some global template funcs etc. that need the language information.
 	viper.Set("Multilingual", s.multilingualEnabled())
 	viper.Set("CurrentContentLanguage", s.Language)
-	if err = tpl.SetTranslateLang(s.Language.Lang); err != nil {
+	return tpl.SetTranslateLang(s.Language.Lang)
+}
+
+func (s *Site) render() (err error) {
+	if err = s.setCurrentLanguageConfig(); err != nil {
 		return
 	}