Skip to content

Commit

Permalink
hugolib: Simplify bundle lookup via .Site.GetPage, ref, relref
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Jan 24, 2018
1 parent a195639 commit 517b6b6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions hugolib/page_bundler_capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions hugolib/page_bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 15 additions & 3 deletions hugolib/page_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 517b6b6

Please sign in to comment.