Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-07-19 19:26:10 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-07-19 19:26:10 +0300
commit9b4b97a722c2af3c4ae1df24634c22209b9a575a (patch)
treeadf7fe6150983424f82deb79448a8c5d59f9eda9 /hugolib
parent501543d4b6d381a1de496baf2870993a03afcfdb (diff)
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
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/page_collections.go47
-rw-r--r--hugolib/site.go33
2 files changed, 43 insertions, 37 deletions
diff --git a/hugolib/page_collections.go b/hugolib/page_collections.go
index 60f7dd83c..3d7c86280 100644
--- a/hugolib/page_collections.go
+++ b/hugolib/page_collections.go
@@ -16,6 +16,7 @@ package hugolib
import (
"fmt"
"path"
+ "path/filepath"
"strings"
"github.com/gohugoio/hugo/cache"
@@ -149,13 +150,49 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections {
return &PageCollections{rawAllPages: pages}
}
-// getPage is the "old style" get page. Deprecated in Hugo 0.45 in favour of
-// the "path only" syntax.
-// TODO(bep) remove this an rename below once this is all working.
+// This is an adapter func for the old API with Kind as first argument.
+// This is invoked when you do .Site.GetPage. We drop the Kind and fails
+// if there are more than 2 arguments, which would be ambigous.
+func (c *PageCollections) getPageOldVersion(ref ...string) (*Page, error) {
+ var refs []string
+ 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 {
- p, _ := c.getPageNew(nil, "/"+path.Join(sections...))
+ refs := append([]string{typ}, path.Join(sections...))
+ p, _ := c.getPageOldVersion(refs...)
return p
-
}
// Ref is either unix-style paths (i.e. callers responsible for
diff --git a/hugolib/site.go b/hugolib/site.go
index dcb9b1d8c..4cca648f3 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -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" }},
// i.e. 2 arguments, so we test for that.
func (s *SiteInfo) GetPage(ref ...string) (*Page, error) {
- var refs []string
- 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)
+ return s.getPageOldVersion(ref...)
}
func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) {