shithub: hugo

Download patch

ref: f441f675126ef1123d9f94429872dd683b40e011
parent: 7ed22e9fb6a5b74c52ae6054b843b8c64e83f4b6
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Thu Jan 23 12:34:19 EST 2020

Fix baseof with regular define regression

Fixes #6790

--- a/hugolib/template_test.go
+++ b/hugolib/template_test.go
@@ -361,6 +361,43 @@
 
 }
 
+// https://github.com/gohugoio/hugo/issues/6790
+func TestTemplateNoBasePlease(t *testing.T) {
+	t.Parallel()
+	b := newTestSitesBuilder(t).WithSimpleConfigFile()
+
+	b.WithTemplates("_default/list.html", `
+	{{ define "main" }}
+	  Bonjour
+	{{ end }}
+
+	{{ printf "list" }}
+
+
+	`)
+
+	b.WithTemplates(
+		"_default/single.html", `
+{{ printf "single" }}
+{{ define "main" }}
+  Bonjour
+{{ end }}
+
+
+`)
+
+	b.WithContent("blog/p1.md", `---
+title: The Page
+---
+`)
+
+	b.Build(BuildCfg{})
+
+	b.AssertFileContent("public/blog/p1/index.html", `single`)
+	b.AssertFileContent("public/blog/index.html", `list`)
+
+}
+
 func TestTemplateLookupSite(t *testing.T) {
 	t.Run("basic", func(t *testing.T) {
 		t.Parallel()
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -72,8 +72,15 @@
 	_ tpl.Info     = (*templateState)(nil)
 )
 
-var defineRe = regexp.MustCompile(`{{-?\s?define`)
+// A template needing a base template is a template with only define sections,
+// but we check only for the start.
+// If a base template does not exist, we will handle that when it's used.
+var baseTemplateDefineRe = regexp.MustCompile(`^\s*{{-?\s*define`)
 
+func needsBaseTemplate(templ string) bool {
+	return baseTemplateDefineRe.MatchString(templ)
+}
+
 func newIdentity(name string) identity.Manager {
 	return identity.NewManager(identity.NewPathIdentity(files.ComponentFolderLayouts, name))
 }
@@ -379,10 +386,6 @@
 			}
 		}
 
-		if !found {
-			return nil, false, errors.Errorf("no baseof layout found for %q:", name)
-		}
-
 		templ, err := t.applyBaseTemplate(overlay, base)
 		if err != nil {
 			return nil, false, err
@@ -389,11 +392,14 @@
 		}
 
 		ts := newTemplateState(templ, overlay)
-		ts.baseInfo = base
 
-		// Add the base identity to detect changes
-		ts.Add(identity.NewPathIdentity(files.ComponentFolderLayouts, base.name))
+		if found {
+			ts.baseInfo = base
 
+			// Add the base identity to detect changes
+			ts.Add(identity.NewPathIdentity(files.ComponentFolderLayouts, base.name))
+		}
+
 		t.applyTemplateTransformers(t.main, ts)
 
 		return ts, true, nil
@@ -537,13 +543,13 @@
 		return err
 	}
 
-	if isBaseTemplate(name) {
+	if isBaseTemplatePath(name) {
 		// Store it for later.
 		t.baseof[name] = tinfo
 		return nil
 	}
 
-	needsBaseof := !t.noBaseNeeded(name) && defineRe.MatchString(tinfo.template)
+	needsBaseof := !t.noBaseNeeded(name) && baseTemplateDefineRe.MatchString(tinfo.template)
 	if needsBaseof {
 		t.needsBaseof[name] = tinfo
 		return nil
@@ -565,10 +571,18 @@
 
 func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Template, error) {
 	if overlay.isText {
-		templ, err := t.main.prototypeTextClone.New(overlay.name).Parse(base.template)
-		if err != nil {
-			return nil, base.errWithFileContext("parse failed", err)
+		var (
+			templ = t.main.prototypeTextClone.New(overlay.name)
+			err   error
+		)
+
+		if !base.IsZero() {
+			templ, err = templ.Parse(base.template)
+			if err != nil {
+				return nil, base.errWithFileContext("parse failed", err)
+			}
 		}
+
 		templ, err = templ.Parse(overlay.template)
 		if err != nil {
 			return nil, overlay.errWithFileContext("parse failed", err)
@@ -576,9 +590,16 @@
 		return templ, nil
 	}
 
-	templ, err := t.main.prototypeHTMLClone.New(overlay.name).Parse(base.template)
-	if err != nil {
-		return nil, base.errWithFileContext("parse failed", err)
+	var (
+		templ = t.main.prototypeHTMLClone.New(overlay.name)
+		err   error
+	)
+
+	if !base.IsZero() {
+		templ, err = templ.Parse(base.template)
+		if err != nil {
+			return nil, base.errWithFileContext("parse failed", err)
+		}
 	}
 
 	templ, err = htmltemplate.Must(templ.Clone()).Parse(overlay.template)
@@ -890,7 +911,7 @@
 	return path[len(path)-1] == '~'
 }
 
-func isBaseTemplate(path string) bool {
+func isBaseTemplatePath(path string) bool {
 	return strings.Contains(filepath.Base(path), baseFileBase)
 }
 
--- a/tpl/tplimpl/template_errors.go
+++ b/tpl/tplimpl/template_errors.go
@@ -34,6 +34,10 @@
 	realFilename string
 }
 
+func (t templateInfo) IsZero() bool {
+	return t.name == ""
+}
+
 func (t templateInfo) resolveType() templateType {
 	return resolveTemplateType(t.name)
 }
--- /dev/null
+++ b/tpl/tplimpl/template_test.go
@@ -1,0 +1,33 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package tplimpl
+
+import (
+	"testing"
+
+	qt "github.com/frankban/quicktest"
+)
+
+func TestNeedsBaseTemplate(t *testing.T) {
+	c := qt.New(t)
+
+	c.Assert(needsBaseTemplate(`{{ define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`{{define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`{{-  define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`{{-define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`    {{ define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`    
+{{ define "main" }}`), qt.Equals, true)
+	c.Assert(needsBaseTemplate(`  A  {{ define "main" }}`), qt.Equals, false)
+
+}