hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 6cceef65c2f4b7c262bf67a249867658112b6de4
parent ffcb4aeb8e392a80da7cad0f1e03a4102efb24ec
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Mon,  9 Mar 2020 14:01:28 +0100

Fix ambigous error on site.GetPage

Fixes #7016

Diffstat:
Mhugolib/pagecollections.go | 20+++++++++++++++++---
Mhugolib/pagecollections_test.go | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+), 3 deletions(-)
diff --git a/hugolib/pagecollections.go b/hugolib/pagecollections.go
@@ -206,6 +206,22 @@ func (c *PageCollections) getSectionOrPage(ref string) (*contentNode, string) {
 
 }
 
+// For Ref/Reflink and .Site.GetPage do simple name lookups for the potentially ambigous myarticle.md and /myarticle.md,
+// but not when we get ./myarticle*, section/myarticle.
+func shouldDoSimpleLookup(ref string) bool {
+	if ref[0] == '.' {
+		return false
+	}
+
+	slashCount := strings.Count(ref, "/")
+
+	if slashCount > 1 {
+		return false
+	}
+
+	return slashCount == 0 || ref[0] == '/'
+}
+
 func (c *PageCollections) getContentNode(context page.Page, isReflink bool, ref string) (*contentNode, error) {
 	ref = filepath.ToSlash(strings.ToLower(strings.TrimSpace(ref)))
 	if ref == "" {
@@ -215,9 +231,7 @@ func (c *PageCollections) getContentNode(context page.Page, isReflink bool, ref 
 	navUp := strings.HasPrefix(ref, "..")
 	var doSimpleLookup bool
 	if isReflink || context == nil {
-		// For Ref/Reflink and .Site.GetPage do simple name lookups for the potentially ambigous myarticle.md and /myarticle.md,
-		// but not when we get ./myarticle*, section/myarticle.
-		doSimpleLookup = ref[0] != '.' || ref[0] == '/' && strings.Count(ref, "/") == 1
+		doSimpleLookup = shouldDoSimpleLookup(ref)
 	}
 
 	if context != nil && !strings.HasPrefix(ref, "/") {
diff --git a/hugolib/pagecollections_test.go b/hugolib/pagecollections_test.go
@@ -334,3 +334,52 @@ NOT FOUND
 	b.AssertFileContent("public/who/index.html", `NOT FOUND`)
 
 }
+
+// https://github.com/gohugoio/hugo/issues/7016
+func TestGetPageMultilingual(t *testing.T) {
+	b := newTestSitesBuilder(t)
+
+	b.WithConfigFile("yaml", `
+baseURL: "http://example.org/"
+languageCode: "en-us"
+defaultContentLanguage: ru
+title: "My New Hugo Site"
+uglyurls: true
+
+languages:
+  ru: {}
+  en: {}
+`)
+
+	b.WithContent(
+		"docs/1.md", "\n---title: p1\n---",
+		"news/1.md", "\n---title: p1\n---",
+		"news/1.en.md", "\n---title: p1en\n---",
+		"news/about/1.md", "\n---title: about1\n---",
+		"news/about/1.en.md", "\n---title: about1en\n---",
+	)
+
+	b.WithTemplates("index.html", `
+{{ with site.GetPage "docs/1" }}
+    Docs p1: {{ .Title }}
+{{ else }}
+NOT FOUND
+{{ end }}
+`)
+
+	b.Build(BuildCfg{})
+
+	b.AssertFileContent("public/index.html", `Docs p1: p1`)
+	b.AssertFileContent("public/en/index.html", `NOT FOUND`)
+
+}
+
+func TestShouldDoSimpleLookup(t *testing.T) {
+	c := qt.New(t)
+
+	c.Assert(shouldDoSimpleLookup("foo.md"), qt.Equals, true)
+	c.Assert(shouldDoSimpleLookup("/foo.md"), qt.Equals, true)
+	c.Assert(shouldDoSimpleLookup("./foo.md"), qt.Equals, false)
+	c.Assert(shouldDoSimpleLookup("docs/foo.md"), qt.Equals, false)
+
+}