hugolib: Fix ref/relref issue with duplicate base filenames - hugo - [fork] hugo port for 9front
 (HTM) git clone git@git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 612f6e3afe0510c31f70f3621f3dc8ba609dade4
 (DIR) parent 50d11138f3e18b545c15fadf52f7b0b744bf3e7c
 (HTM) Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
       Date:   Fri, 26 May 2017 10:12:19 +0300
       
       hugolib: Fix ref/relref issue with duplicate base filenames
       
       This commit also makes that function 80x faster.
       
       Fixes #2507
       
       Diffstat:
         M hugolib/page_collections.go         |       3 ++-
         M hugolib/page_collections_test.go    |       6 +++++-
         M hugolib/site.go                     |       8 +-------
         M hugolib/site_test.go                |      13 +++++--------
       
       4 files changed, 13 insertions(+), 17 deletions(-)
       ---
 (DIR) diff --git a/hugolib/page_collections.go b/hugolib/page_collections.go
       @@ -61,8 +61,9 @@ func (c *PageCollections) refreshPageCaches() {
                                        // shortcodes. If the user says "sect/doc1.en.md", he/she knows
                                        // what he/she is looking for.
                                        for _, p := range c.AllRegularPages {
       -                                        // TODO(bep) section
                                                cache[filepath.ToSlash(p.Source.Path())] = p
       +                                        // Ref/Relref supports this potentially ambiguous lookup.
       +                                        cache[p.Source.LogicalName()] = p
                                        }
                                default:
                                        for _, p := range c.indexPages {
 (DIR) diff --git a/hugolib/page_collections_test.go b/hugolib/page_collections_test.go
       @@ -110,6 +110,9 @@ func TestGetPage(t *testing.T) {
                        }
                }
        
       +        content := fmt.Sprintf(pageCollectionsPageTemplate, "UniqueBase")
       +        writeSource(t, fs, filepath.Join("content", "sect3", "unique.md"), content)
       +
                s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
        
                tests := []struct {
       @@ -122,7 +125,8 @@ func TestGetPage(t *testing.T) {
                        {KindPage, []string{"sect3", "page1.md"}, "Title3_1"},
                        {KindPage, []string{"sect4/page2.md"}, "Title4_2"},
                        {KindPage, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"},
       -                // TODO(bep) section maybe support sect5/page2, aka relref.
       +                // Ref/Relref supports this potentially ambiguous lookup.
       +                {KindPage, []string{"unique.md"}, "UniqueBase"},
                }
        
                for i, test := range tests {
 (DIR) diff --git a/hugolib/site.go b/hugolib/site.go
       @@ -427,13 +427,7 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool, outputFormat s
                var link string
        
                if refURL.Path != "" {
       -                for _, page := range s.AllRegularPages {
       -                        refPath := filepath.FromSlash(refURL.Path)
       -                        if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
       -                                target = page
       -                                break
       -                        }
       -                }
       +                target := s.getPage(KindPage, refURL.Path)
        
                        if target == nil {
                                return "", fmt.Errorf("No page found with path or logical name \"%s\".\n", refURL.Path)
 (DIR) diff --git a/hugolib/site_test.go b/hugolib/site_test.go
       @@ -907,6 +907,7 @@ func findPage(site *Site, f string) *Page {
        
        func setupLinkingMockSite(t *testing.T) *Site {
                sources := []source.ByteSource{
       +                {Name: filepath.FromSlash("level2/unique.md"), Content: []byte("")},
                        {Name: filepath.FromSlash("index.md"), Content: []byte("")},
                        {Name: filepath.FromSlash("rootfile.md"), Content: []byte("")},
                        {Name: filepath.FromSlash("root-image.png"), Content: []byte("")},
       @@ -957,15 +958,11 @@ func TestRefLinking(t *testing.T) {
                        relative     bool
                        expected     string
                }{
       -                // Note: There are no magic in the index.md name. This was fixed in Hugo 0.20.
       -                // Before that, index.md would wrongly resolve to "/".
       -                // See #3396 -- there is an ambiguity in the examples below, even if they do work.
       -                // TODO(bep) better test cases
       -                {"index.md", "", true, "/"},
       -                {"common.md", "", true, "/level2/common/"},
       +                {"unique.md", "", true, "/level2/unique/"},
       +                {"level2/common.md", "", true, "/level2/common/"},
                        {"3-root.md", "", true, "/level2/level3/3-root/"},
       -                {"index.md", "amp", true, "/amp/"},
       -                {"index.md", "amp", false, "http://auth/amp/"},
       +                {"level2/level3/index.md", "amp", true, "/amp/level2/level3/"},
       +                {"level2/index.md", "amp", false, "http://auth/amp/level2/"},
                } {
                        if out, err := site.Info.refLink(test.link, currentPage, test.relative, test.outputFormat); err != nil || out != test.expected {
                                t.Errorf("[%d] Expected %s to resolve to (%s), got (%s) - error: %s", i, test.link, test.expected, out, err)