shithub: hugo

Download patch

ref: 517b6b62389d23bfe41fe3ae551a691b11bdcaa7
parent: a19563910eec5fed08f3b02563b9a7b38026183d
author: Bjørn Erik Pedersen <[email protected]>
date: Wed Jan 24 04:47:30 EST 2018

hugolib: Simplify bundle lookup via .Site.GetPage, ref, relref

Given a bundle in `blog/my-bundle/index.en.md` all of these will now worK:

* `blog/my-bundle/index.en.md`
* `blog/my-bundle/index`
* `blog/my-bundle`
* `my-bundle`

The last one is potentially ambigous.

Fixes #4312

--- a/hugolib/page_bundler_capture_test.go
+++ b/hugolib/page_bundler_capture_test.go
@@ -186,9 +186,11 @@
 __bundle/en/work/base/bb/_index.md/resources/en/work/base/bb/a.png|en/work/base/bb/b.png|nn/work/base/bb/c.nn.png
 __bundle/en/work/base/bc/_index.md/resources/en/work/base/bc/logo-bc.png
 __bundle/en/work/base/bd/index.md/resources/en/work/base/bd/page.md
+__bundle/en/work/base/bf/my-bf-bundle/index.md/resources/en/work/base/bf/my-bf-bundle/page.md
 __bundle/en/work/base/lb/index.md/resources/en/work/base/lb/1.md|en/work/base/lb/2.md|en/work/base/lb/c/d/deep.png|en/work/base/lb/c/logo.png|en/work/base/lb/c/one.png|en/work/base/lb/c/page.md
 __bundle/nn/work/base/bb/_index.nn.md/resources/en/work/base/bb/a.png|nn/work/base/bb/b.nn.png|nn/work/base/bb/c.nn.png
 __bundle/nn/work/base/bd/index.md/resources/nn/work/base/bd/page.nn.md
+__bundle/nn/work/base/bf/my-bf-bundle/index.nn.md/resources
 __bundle/nn/work/base/lb/index.nn.md/resources/en/work/base/lb/c/d/deep.png|en/work/base/lb/c/one.png|nn/work/base/lb/2.nn.md|nn/work/base/lb/c/logo.nn.png
 C:
 /work/base/1s/mylogo.png
--- a/hugolib/page_bundler_test.go
+++ b/hugolib/page_bundler_test.go
@@ -192,6 +192,32 @@
 				bundleWithSubPath := s.getPage(KindPage, "lb/index")
 				assert.NotNil(bundleWithSubPath)
 
+				// See https://github.com/gohugoio/hugo/issues/4312
+				// Before that issue:
+				// A bundle in a/b/index.en.md
+				// a/b/index.en.md => OK
+				// a/b/index => OK
+				// index.en.md => ambigous, but OK.
+				// With bundles, the file name has little meaning, the folder it lives in does. So this should also work:
+				// a/b
+				// and probably also just b (aka "my-bundle")
+				// These may also be translated, so we also need to test that.
+				//  "bf", "my-bf-bundle", "index.md + nn
+				bfBundle := s.getPage(KindPage, "bf/my-bf-bundle/index")
+				assert.NotNil(bfBundle)
+				assert.Equal("en", bfBundle.Lang())
+				assert.Equal(bfBundle, s.getPage(KindPage, "bf/my-bf-bundle/index.md"))
+				assert.Equal(bfBundle, s.getPage(KindPage, "bf/my-bf-bundle"))
+				assert.Equal(bfBundle, s.getPage(KindPage, "my-bf-bundle"))
+
+				nnSite := sites.Sites[1]
+				bfBundleNN := nnSite.getPage(KindPage, "bf/my-bf-bundle/index")
+				assert.NotNil(bfBundleNN)
+				assert.Equal("nn", bfBundleNN.Lang())
+				assert.Equal(bfBundleNN, nnSite.getPage(KindPage, "bf/my-bf-bundle/index.nn.md"))
+				assert.Equal(bfBundleNN, nnSite.getPage(KindPage, "bf/my-bf-bundle"))
+				assert.Equal(bfBundleNN, nnSite.getPage(KindPage, "my-bf-bundle"))
+
 				// See https://github.com/gohugoio/hugo/issues/4295
 				// Every resource should have its Name prefixed with its base folder.
 				cBundleResources := bundleWithSubPath.Resources.ByPrefix("c/")
@@ -517,6 +543,11 @@
 	writeSource(t, fs, filepath.Join(workDir, "base", "lb", "c", "logo.nn.png"), "content")
 	writeSource(t, fs, filepath.Join(workDir, "base", "lb", "c", "one.png"), "content")
 	writeSource(t, fs, filepath.Join(workDir, "base", "lb", "c", "d", "deep.png"), "content")
+
+	//Translated bundle in some sensible sub path.
+	writeSource(t, fs, filepath.Join(workDir, "base", "bf", "my-bf-bundle", "index.md"), pageContent)
+	writeSource(t, fs, filepath.Join(workDir, "base", "bf", "my-bf-bundle", "index.nn.md"), pageContent)
+	writeSource(t, fs, filepath.Join(workDir, "base", "bf", "my-bf-bundle", "page.md"), pageContent)
 
 	return cfg, fs
 }
--- a/hugolib/page_collections.go
+++ b/hugolib/page_collections.go
@@ -16,8 +16,10 @@
 import (
 	"path"
 	"path/filepath"
+	"strings"
 
 	"github.com/gohugoio/hugo/cache"
+	"github.com/gohugoio/hugo/helpers"
 )
 
 // PageCollections contains the page collections for a site.
@@ -72,12 +74,22 @@
 				for _, pageCollection := range []Pages{c.AllRegularPages, c.headlessPages} {
 					for _, p := range pageCollection {
 						cache[filepath.ToSlash(p.Source.Path())] = p
-						// Ref/Relref supports this potentially ambiguous lookup.
-						cache[p.Source.LogicalName()] = p
 
 						if s != nil && p.s == s {
+							// Ref/Relref supports this potentially ambiguous lookup.
+							cache[p.Source.LogicalName()] = p
+
+							translasionBaseName := p.Source.TranslationBaseName()
+							dir := filepath.ToSlash(strings.TrimSuffix(p.Dir(), helpers.FilePathSeparator))
+
+							if translasionBaseName == "index" {
+								_, name := path.Split(dir)
+								cache[name] = p
+								cache[dir] = p
+							}
+
 							// We need a way to get to the current language version.
-							pathWithNoExtensions := path.Join(filepath.ToSlash(p.Source.Dir()), p.Source.TranslationBaseName())
+							pathWithNoExtensions := path.Join(dir, translasionBaseName)
 							cache[pathWithNoExtensions] = p
 						}
 					}