hugolib: Create an adapter from old to new getPage

To make sure we confirm that the existing tests run the correct code path.

Updates #4969
This commit is contained in:
Bjørn Erik Pedersen 2018-07-19 18:26:10 +02:00
parent 501543d4b6
commit 9b4b97a722
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 44 additions and 38 deletions

View file

@ -16,6 +16,7 @@ package hugolib
import ( import (
"fmt" "fmt"
"path" "path"
"path/filepath"
"strings" "strings"
"github.com/gohugoio/hugo/cache" "github.com/gohugoio/hugo/cache"
@ -149,13 +150,49 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections {
return &PageCollections{rawAllPages: pages} return &PageCollections{rawAllPages: pages}
} }
// getPage is the "old style" get page. Deprecated in Hugo 0.45 in favour of // This is an adapter func for the old API with Kind as first argument.
// the "path only" syntax. // This is invoked when you do .Site.GetPage. We drop the Kind and fails
// TODO(bep) remove this an rename below once this is all working. // if there are more than 2 arguments, which would be ambigous.
func (c *PageCollections) getPage(typ string, sections ...string) *Page { func (c *PageCollections) getPageOldVersion(ref ...string) (*Page, error) {
p, _ := c.getPageNew(nil, "/"+path.Join(sections...)) var refs []string
return p for _, r := range ref {
// A common construct in the wild is
// .Site.GetPage "home" "" or
// .Site.GetPage "home" "/"
if r != "" && r != "/" {
refs = append(refs, r)
}
}
var key string
if len(refs) > 2 {
// This was allowed in Hugo <= 0.44, but we cannot support this with the
// new API. This should be the most unusual case.
return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref)
}
if len(refs) == 0 || refs[0] == KindHome {
key = "/"
} else if len(refs) == 1 {
key = refs[0]
} else {
key = refs[1]
}
key = filepath.ToSlash(key)
if !strings.HasPrefix(key, "/") {
key = "/" + key
}
return c.getPageNew(nil, key)
}
// Only used in tests.
func (c *PageCollections) getPage(typ string, sections ...string) *Page {
refs := append([]string{typ}, path.Join(sections...))
p, _ := c.getPageOldVersion(refs...)
return p
} }
// Ref is either unix-style paths (i.e. callers responsible for // Ref is either unix-style paths (i.e. callers responsible for

View file

@ -1612,38 +1612,7 @@ func (s *Site) appendThemeTemplates(in []string) []string {
// as possible for existing sites. Most sites will use {{ .Site.GetPage "section" "my/section" }}, // as possible for existing sites. Most sites will use {{ .Site.GetPage "section" "my/section" }},
// i.e. 2 arguments, so we test for that. // i.e. 2 arguments, so we test for that.
func (s *SiteInfo) GetPage(ref ...string) (*Page, error) { func (s *SiteInfo) GetPage(ref ...string) (*Page, error) {
var refs []string return s.getPageOldVersion(ref...)
for _, r := range ref {
// A common construct in the wild is
// .Site.GetPage "home" "" or
// .Site.GetPage "home" "/"
if r != "" && r != "/" {
refs = append(refs, r)
}
}
var key string
if len(refs) > 2 {
// This was allowed in Hugo <= 0.44, but we cannot support this with the
// new API. This should be the most unusual case.
return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref)
}
if len(refs) == 0 || refs[0] == KindHome {
key = "/"
} else if len(refs) == 1 {
key = refs[0]
} else {
key = refs[1]
}
key = filepath.ToSlash(key)
if !strings.HasPrefix(key, "/") {
key = "/" + key
}
return s.getPageNew(nil, key)
} }
func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) { func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) {