hugolib, output: Add theme logic to LayoutHandler
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 6 Mar 2017 18:54:46 +0000 (19:54 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 27 Mar 2017 13:43:56 +0000 (15:43 +0200)
hugolib/shortcode_test.go
hugolib/site.go
hugolib/site_render.go
hugolib/testhelpers_test.go
output/layout.go
output/layout_test.go

index 0b429306edf122070f7a9408eda083658fffdd4f..abfbb425e3f814e4034bfbe751c5dea4b7a3533d 100644 (file)
@@ -37,7 +37,7 @@ func pageFromString(in, filename string, withTemplate ...func(templ tpl.Template
                var err error
                cfg, fs := newTestCfg()
 
-               d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Fs: fs, WithTemplate: withTemplate[0]}
+               d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Cfg: cfg, Fs: fs, WithTemplate: withTemplate[0]}
 
                s, err = NewSiteForCfg(d)
                if err != nil {
index 784510fdb1a33ed98145f86af052a5424317d47e..b3825a9bb7d804c318b33cb186a09dee41f186da 100644 (file)
@@ -124,7 +124,7 @@ func (s *Site) isEnabled(kind string) bool {
 
 // reset returns a new Site prepared for rebuild.
 func (s *Site) reset() *Site {
-       return &Site{Deps: s.Deps, layoutHandler: &output.LayoutHandler{}, disabledKinds: s.disabledKinds, Language: s.Language, owner: s.owner, PageCollections: newPageCollections()}
+       return &Site{Deps: s.Deps, layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()), disabledKinds: s.disabledKinds, Language: s.Language, owner: s.owner, PageCollections: newPageCollections()}
 }
 
 // newSite creates a new site with the given configuration.
@@ -140,7 +140,7 @@ func newSite(cfg deps.DepsCfg) (*Site, error) {
                disabledKinds[disabled] = true
        }
 
-       s := &Site{PageCollections: c, layoutHandler: &output.LayoutHandler{}, Language: cfg.Language, disabledKinds: disabledKinds}
+       s := &Site{PageCollections: c, layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""), Language: cfg.Language, disabledKinds: disabledKinds}
 
        s.Info = newSiteInfo(siteBuilderCfg{s: s, pageCollections: c, language: s.Language})
 
@@ -193,7 +193,7 @@ func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tpl.Templ
                return nil
        }
 
-       cfg := deps.DepsCfg{WithTemplate: withTemplates, Language: lang}
+       cfg := deps.DepsCfg{WithTemplate: withTemplates, Language: lang, Cfg: lang}
 
        return NewSiteForCfg(cfg)
 
@@ -1676,7 +1676,7 @@ func errorCollator(results <-chan error, errs chan<- error) {
        close(errs)
 }
 
-// TODO(bep) output move
+// TODO(bep) output remove
 func (s *Site) appendThemeTemplates(in []string) []string {
        if !s.PathSpec.ThemeSet() {
                return in
index 51886d34ba03d0b093b3ff4ea7d773c4215a8c46..10d10d40d47dc93fb064fe0b9ecb11569367f3c4 100644 (file)
@@ -82,7 +82,7 @@ func pageRenderer(s *Site, pages <-chan *PageOutput, results chan<- error, wg *s
 
                        s.Log.DEBUG.Printf("Render %s to %q with layouts %q", p.Kind, targetPath, layouts)
 
-                       if err := s.renderAndWritePage("page "+p.FullFilePath(), targetPath, p, s.appendThemeTemplates(layouts)...); err != nil {
+                       if err := s.renderAndWritePage("page "+p.FullFilePath(), targetPath, p, layouts...); err != nil {
                                results <- err
                        }
 
index a78b73c8df590602735858a5853685b3318f7baf..a38a12bdca03478306d3b954bb016ba90932977e 100644 (file)
@@ -104,7 +104,7 @@ func newTestSite(t testing.TB, configKeyValues ...interface{}) *Site {
                cfg.Set(configKeyValues[i].(string), configKeyValues[i+1])
        }
 
-       d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Fs: fs}
+       d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Fs: fs, Cfg: cfg}
 
        s, err := NewSiteForCfg(d)
 
index ba246237af3776e7fc73ded41ed14d65b489c67a..0e0f33dad44783cb468b63c4826079c79de00550 100644 (file)
@@ -30,6 +30,11 @@ type LayoutIdentifier interface {
 // Layout calculates the layout template to use to render a given output type.
 // TODO(bep) output improve names
 type LayoutHandler struct {
+       hasTheme bool
+}
+
+func NewLayoutHandler(hasTheme bool) *LayoutHandler {
+       return &LayoutHandler{hasTheme: hasTheme}
 }
 
 // TODO(bep) output theme layouts
@@ -63,8 +68,30 @@ func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type)
                layouts = regularPageLayouts(id.PageType(), layout)
        }
 
-       for _, l := range layouts {
-               layouts = append(layouts, "theme/"+l)
+       if l.hasTheme {
+               layoutsWithThemeLayouts := []string{}
+               // First place all non internal templates
+               for _, t := range layouts {
+                       if !strings.HasPrefix(t, "_internal/") {
+                               layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, t)
+                       }
+               }
+
+               // Then place theme templates with the same names
+               for _, t := range layouts {
+                       if !strings.HasPrefix(t, "_internal/") {
+                               layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, "theme/"+t)
+                       }
+               }
+
+               // Lastly place internal templates
+               for _, t := range layouts {
+                       if strings.HasPrefix(t, "_internal/") {
+                               layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, t)
+                       }
+               }
+
+               return layoutsWithThemeLayouts
        }
 
        return layouts
index 5b95e01d8b491e3f194611277305a4077f146179..333216e17110e8de16ed3d2152798ff36875fd1c 100644 (file)
@@ -14,6 +14,7 @@
 package output
 
 import (
+       "fmt"
        "testing"
 
        "github.com/stretchr/testify/require"
@@ -43,28 +44,45 @@ func (l testLayoutIdentifier) PageSection() string {
 }
 
 func TestLayout(t *testing.T) {
-       l := &LayoutHandler{}
 
-       for _, this := range []struct {
+       for i, this := range []struct {
                li             testLayoutIdentifier
+               hasTheme       bool
                layoutOverride string
                tp             Type
                expect         []string
        }{
-               {testLayoutIdentifier{"home", "", "", ""}, "", HTMLType, []string{"index.html", "_default/list.html", "theme/index.html", "theme/_default/list.html"}},
-               {testLayoutIdentifier{"section", "sect1", "", ""}, "", HTMLType, []string{"section/sect1.html", "sect1/list.html"}},
-               {testLayoutIdentifier{"taxonomy", "tag", "", ""}, "", HTMLType, []string{"taxonomy/tag.html", "indexes/tag.html"}},
-               {testLayoutIdentifier{"taxonomyTerm", "categories", "", ""}, "", HTMLType, []string{"taxonomy/categories.terms.html", "_default/terms.html"}},
-               {testLayoutIdentifier{"page", "", "", ""}, "", HTMLType, []string{"_default/single.html", "theme/_default/single.html"}},
-               {testLayoutIdentifier{"page", "", "mylayout", ""}, "", HTMLType, []string{"_default/mylayout.html"}},
-               {testLayoutIdentifier{"page", "", "mylayout", "myttype"}, "", HTMLType, []string{"myttype/mylayout.html", "_default/mylayout.html"}},
-               {testLayoutIdentifier{"page", "", "mylayout", "myttype/mysubtype"}, "", HTMLType, []string{"myttype/mysubtype/mylayout.html", "myttype/mylayout.html", "_default/mylayout.html"}},
-               {testLayoutIdentifier{"page", "", "mylayout", "myttype"}, "myotherlayout", HTMLType, []string{"myttype/myotherlayout.html", "_default/myotherlayout.html"}},
+               {testLayoutIdentifier{"home", "", "", ""}, true, "", HTMLType,
+                       []string{"index.html", "_default/list.html", "theme/index.html", "theme/_default/list.html"}},
+               {testLayoutIdentifier{"section", "sect1", "", ""}, false, "", HTMLType,
+                       []string{"section/sect1.html", "sect1/list.html"}},
+               {testLayoutIdentifier{"taxonomy", "tag", "", ""}, false, "", HTMLType,
+                       []string{"taxonomy/tag.html", "indexes/tag.html"}},
+               {testLayoutIdentifier{"taxonomyTerm", "categories", "", ""}, false, "", HTMLType,
+                       []string{"taxonomy/categories.terms.html", "_default/terms.html"}},
+               {testLayoutIdentifier{"page", "", "", ""}, true, "", HTMLType,
+                       []string{"_default/single.html", "theme/_default/single.html"}},
+               {testLayoutIdentifier{"page", "", "mylayout", ""}, false, "", HTMLType,
+                       []string{"_default/mylayout.html"}},
+               {testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "", HTMLType,
+                       []string{"myttype/mylayout.html", "_default/mylayout.html"}},
+               {testLayoutIdentifier{"page", "", "mylayout", "myttype/mysubtype"}, false, "", HTMLType,
+                       []string{"myttype/mysubtype/mylayout.html", "myttype/mylayout.html", "_default/mylayout.html"}},
+               {testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "myotherlayout", HTMLType,
+                       []string{"myttype/myotherlayout.html", "_default/myotherlayout.html"}},
        } {
+               l := NewLayoutHandler(this.hasTheme)
+               logMsg := fmt.Sprintf("Test %d", i)
                layouts := l.For(this.li, this.layoutOverride, this.tp)
-               require.NotNil(t, layouts)
-               require.True(t, len(layouts) >= len(this.expect))
+               require.NotNil(t, layouts, logMsg)
+               require.True(t, len(layouts) >= len(this.expect), logMsg)
                // Not checking the complete list for now ...
-               require.Equal(t, this.expect, layouts[:len(this.expect)])
+               require.Equal(t, this.expect, layouts[:len(this.expect)], logMsg)
+
+               if !this.hasTheme {
+                       for _, layout := range layouts {
+                               require.NotContains(t, layout, "theme", logMsg)
+                       }
+               }
        }
 }