shithub: hugo

Download patch

ref: 5d03086981b4a7d4bc450269a6a2e0fd22dbeed7
parent: 07700aab0d816b8af1d49465fe3a76331ccb6b77
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Jan 20 12:11:03 EST 2018

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

Fixes #4295

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -234,6 +234,8 @@
 	// 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 @@
 }
 
 // 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
 }
--- a/hugolib/page_bundler_capture_test.go
+++ b/hugolib/page_bundler_capture_test.go
@@ -186,7 +186,7 @@
 __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
--- a/hugolib/page_bundler_handlers.go
+++ b/hugolib/page_bundler_handlers.go
@@ -16,6 +16,7 @@
 import (
 	"errors"
 	"fmt"
+	"path/filepath"
 	"sort"
 
 	"strings"
@@ -232,6 +233,9 @@
 					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)
 				}
 			}
--- a/hugolib/page_bundler_test.go
+++ b/hugolib/page_bundler_test.go
@@ -168,6 +168,42 @@
 
 }
 
+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 @@
 	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")