From 517b6b62389d23bfe41fe3ae551a691b11bdcaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 24 Jan 2018 09:47:30 +0100 Subject: [PATCH] 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 --- hugolib/page_bundler_capture_test.go | 2 ++ hugolib/page_bundler_test.go | 31 ++++++++++++++++++++++++++++ hugolib/page_collections.go | 18 +++++++++++++--- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/hugolib/page_bundler_capture_test.go b/hugolib/page_bundler_capture_test.go index e528ccd45..176f752e0 100644 --- a/hugolib/page_bundler_capture_test.go +++ b/hugolib/page_bundler_capture_test.go @@ -186,9 +186,11 @@ D: __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 diff --git a/hugolib/page_bundler_test.go b/hugolib/page_bundler_test.go index ab268dee3..bb4980c04 100644 --- a/hugolib/page_bundler_test.go +++ b/hugolib/page_bundler_test.go @@ -192,6 +192,32 @@ func TestPageBundlerSiteMultilingual(t *testing.T) { 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/") @@ -518,6 +544,11 @@ TheContent. 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 } diff --git a/hugolib/page_collections.go b/hugolib/page_collections.go index a9335ad41..c9104de3b 100644 --- a/hugolib/page_collections.go +++ b/hugolib/page_collections.go @@ -16,8 +16,10 @@ package hugolib 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 @@ func (c *PageCollections) refreshPageCaches() { 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 } }