Permalink
Browse files

hugolib: Fix handling of pages bundled in sub-folders in ByPrefix etc.

Fixes #4295
  • Loading branch information...
bep committed Jan 20, 2018
1 parent 07700aa commit 5d03086981b4a7d4bc450269a6a2e0fd22dbeed7
Showing with 47 additions and 3 deletions.
  1. +5 −2 hugolib/page.go
  2. +1 −1 hugolib/page_bundler_capture_test.go
  3. +4 −0 hugolib/page_bundler_handlers.go
  4. +37 −0 hugolib/page_bundler_test.go
View
@@ -234,6 +234,8 @@ type Page struct {
// relative target path without extension and any base path element from the baseURL.
// This is used to construct paths in the page resources.
relTargetPathBase string
// Is set to a forward slashed path if this is a Page resources living in a folder below its owner.
resourcePath string
layoutDescriptor output.LayoutDescriptor
@@ -993,9 +995,10 @@ func (p *Page) RelPermalink() string {
}
// See resource.Resource
// This value is used, by default, in Resources.ByPrefix etc.
func (p *Page) Name() string {
if p.File != nil {
return p.File.BaseFileName()
if p.resourcePath != "" {
return p.resourcePath
}
return p.title
}
@@ -186,7 +186,7 @@ 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/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
__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/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
@@ -16,6 +16,7 @@ package hugolib
import (
"errors"
"fmt"
"path/filepath"
"sort"
"strings"
@@ -232,6 +233,9 @@ func (c *contentHandlers) parsePage(h contentHandler) contentHandler {
return res
}
if res.resource != nil {
if pageResource, ok := res.resource.(*Page); ok {
pageResource.resourcePath = filepath.ToSlash(childCtx.target)
}
p.Resources = append(p.Resources, res.resource)
}
}
@@ -168,6 +168,42 @@ func TestPageBundlerSite(t *testing.T) {
}
func TestPageBundlerSiteMultilingual(t *testing.T) {
t.Parallel()
for _, ugly := range []bool{false, true} {
t.Run(fmt.Sprintf("ugly=%t", ugly),
func(t *testing.T) {
assert := require.New(t)
cfg, fs := newTestBundleSourcesMultilingual(t)
cfg.Set("uglyURLs", ugly)
assert.NoError(loadDefaultSettingsFor(cfg))
sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
assert.NoError(err)
assert.Equal(2, len(sites.Sites))
assert.NoError(sites.Build(BuildCfg{}))
s := sites.Sites[0]
bundleWithSubPath := s.getPage(KindPage, "lb/index")
assert.NotNil(bundleWithSubPath)
// See https://github.com/gohugoio/hugo/issues/4295
// Every resource should have its Name prefixed with its base folder.
cBundleResources := bundleWithSubPath.Resources.ByPrefix("c/")
assert.Equal(4, len(cBundleResources))
bundlePage := bundleWithSubPath.Resources.GetByPrefix("c/page")
assert.NotNil(bundlePage)
assert.IsType(&Page{}, bundlePage)
})
}
}
func TestPageBundlerSiteWitSymbolicLinksInContent(t *testing.T) {
assert := require.New(t)
cfg, fs, workDir := newTestBundleSymbolicSources(t)
@@ -395,6 +431,7 @@ TheContent.
writeSource(t, fs, filepath.Join(workDir, "base", "lb", "1.md"), pageContent)
writeSource(t, fs, filepath.Join(workDir, "base", "lb", "2.md"), pageContent)
writeSource(t, fs, filepath.Join(workDir, "base", "lb", "2.nn.md"), pageContent)
writeSource(t, fs, filepath.Join(workDir, "base", "lb", "c", "page.md"), pageContent)
writeSource(t, fs, filepath.Join(workDir, "base", "lb", "c", "logo.png"), "content")
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")

0 comments on commit 5d03086

Please sign in to comment.