mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-15 20:44:01 +02:00
Simplify .Site.GetPage etc.
This commit is a follow up to a recent overhaul of the GetPage/ref/relref implemenation. The most important change in this commit is the update to `.Site.GetPage`: * To reduce the amount of breakage in the wild to its minimum, I have reworked .Site.GetPage with some rules: * We cannot support more than 2 arguments, i.e. .Site.GetPage "page" "posts" "mypage.md" will now throw an error. I think this is the most uncommon syntax and should be OK. It is an easy fix to change the above to .Site.GetPage "/posts/mypage.md" or similar. * .Site.GetPage "home", .Site.GetPage "home" "" and .Site.GetPage "home" "/" will give you the home page. This means that if you have page in root with the name home.md you need to do .Site.GetPage "/home.md" or similar This commit also fixes some multilingual issues, most notable it is now possible to do cross-language ref/relref lookups by prepending the language code to the path, e.g. `/jp/posts/mypage.md`. This commit also reverts the site building tests related to this to "Hugo 0.44 state", to get better control of the changes made. Closes #4147 Closes #4727 Closes #4728 Closes #4728 Closes #4726 Closes #4652
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"mime"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -504,7 +503,7 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool, outputFormat s
|
||||
var link string
|
||||
|
||||
if refURL.Path != "" {
|
||||
target, err := s.getPage(page, refURL.Path)
|
||||
target, err := s.getPageNew(page, refURL.Path)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -1603,19 +1602,45 @@ func (s *Site) appendThemeTemplates(in []string) []string {
|
||||
}
|
||||
|
||||
// GetPage looks up a page of a given type for the given ref.
|
||||
// {{ with .Site.GetPage "section" "blog" }}{{ .Title }}{{ end }}
|
||||
//
|
||||
// This will return nil when no page could be found, and will return an
|
||||
// error if the key is ambiguous.
|
||||
func (s *SiteInfo) GetPage(typ string, ref ...string) (*Page, error) {
|
||||
var key string
|
||||
if len(ref) == 1 {
|
||||
key = filepath.ToSlash(ref[0])
|
||||
} else {
|
||||
key = path.Join(ref...)
|
||||
// In Hugo <= 0.44 you had to add Page Kind (section, home) etc. as the first
|
||||
// argument and then either a unix styled path (with or without a leading slash))
|
||||
// or path elements separated.
|
||||
// When we now remove the Kind from this API, we need to make the transition as painless
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
return s.getPage(nil, key)
|
||||
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) {
|
||||
|
Reference in New Issue
Block a user