From 8a409894bdb0972e152a2eccc47a2738568e1cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 12 Jan 2018 17:17:10 +0100 Subject: [PATCH] hugolib: Allow url in front matter for list type pages This enables some potential foot-shooting, but is needed for some special URL requirements. Fixes #4263 --- hugolib/node_as_page_test.go | 13 +++---- hugolib/page.go | 15 ++++---- hugolib/page_paths.go | 67 ++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/hugolib/node_as_page_test.go b/hugolib/node_as_page_test.go index 6cadafc0d..30408ed40 100644 --- a/hugolib/node_as_page_test.go +++ b/hugolib/node_as_page_test.go @@ -591,7 +591,7 @@ func TestNodesWithURLs(t *testing.T) { writeSource(t, fs, filepath.Join("content", "sect", "_index.md"), `--- title: MySection -url: foo.html +url: /my-section/ --- My Section Content `) @@ -602,7 +602,8 @@ My Section Content require.NoError(t, h.Build(BuildCfg{})) - th.assertFileContent(filepath.Join("public", "sect", "index.html"), "My Section") + th.assertFileContent(filepath.Join("public", "my-section", "index.html"), "My Section") + th.assertFileContent(filepath.Join("public", "my-section", "page", "1", "index.html"), `content="0; url=http://bep.is/base/my-section/"`) s := h.Sites[0] @@ -610,11 +611,11 @@ My Section Content require.Equal(t, "/base/sect1/regular1/", p.URL()) - // Section with front matter and url set (which should not be used) + // Section with front matter and url set sect := s.getPage(KindSection, "sect") - require.Equal(t, "/base/sect/", sect.URL()) - require.Equal(t, "http://bep.is/base/sect/", sect.Permalink()) - require.Equal(t, "/base/sect/", sect.RelPermalink()) + require.Equal(t, "/base/my-section/", sect.URL()) + require.Equal(t, "http://bep.is/base/my-section/", sect.Permalink()) + require.Equal(t, "/base/my-section/", sect.RelPermalink()) // Home page without front matter require.Equal(t, "/base/", s.getPage(KindHome).URL()) diff --git a/hugolib/page.go b/hugolib/page.go index 8181de953..1e0d1ac83 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -223,7 +223,6 @@ type Page struct { Lastmod time.Time Sitemap Sitemap - URLPath permalink string relPermalink string @@ -1111,6 +1110,7 @@ func (p *Page) update(f interface{}) error { return fmt.Errorf("Only relative URLs are supported, %v provided", url) } p.URLPath.URL = cast.ToString(v) + p.URLPath.frontMatterURL = p.URLPath.URL p.Params[loki] = p.URLPath.URL case "type": p.contentType = cast.ToString(v) @@ -1809,10 +1809,11 @@ func (p *Page) String() string { } type URLPath struct { - URL string - Permalink string - Slug string - Section string + URL string + frontMatterURL string + Permalink string + Slug string + Section string } // Scratch returns the writable context associated with this Page. @@ -1991,7 +1992,9 @@ func (p *Page) setValuesForKind(s *Site) { p.URLPath.URL = "/" case KindPage: default: - p.URLPath.URL = "/" + path.Join(p.sections...) + "/" + if p.URLPath.URL == "" { + p.URLPath.URL = "/" + path.Join(p.sections...) + "/" + } } } diff --git a/hugolib/page_paths.go b/hugolib/page_paths.go index 55c770ac8..4b523bd47 100644 --- a/hugolib/page_paths.go +++ b/hugolib/page_paths.go @@ -56,7 +56,7 @@ type targetPathDescriptor struct { // Whether this is a multihost multilingual setup. IsMultihost bool - // Page.URLPath.URL. Will override any Slug etc. for regular pages. + // URL from front matter if set. Will override any Slug etc. URL string // Used to create paginator links. @@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error { Sections: p.sections, UglyURLs: p.s.Info.uglyURLs(p), Dir: filepath.ToSlash(p.Source.Dir()), - URL: p.URLPath.URL, + URL: p.URLPath.frontMatterURL, IsMultihost: p.s.owner.IsMultihost(), } @@ -189,7 +189,7 @@ func createTargetPath(d targetPathDescriptor) string { isUgly = true } - if d.Kind != KindPage && len(d.Sections) > 0 { + if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 { if d.ExpandedPermalink != "" { pagePath = filepath.Join(pagePath, d.ExpandedPermalink) } else { @@ -202,43 +202,42 @@ func createTargetPath(d targetPathDescriptor) string { pagePath = filepath.Join(pagePath, d.Type.Path) } - if d.Kind == KindPage { - // Always use URL if it's specified - if d.URL != "" { - if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) { - pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL) - } else { - pagePath = filepath.Join(pagePath, d.URL) - } - if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") { - pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) - } + if d.Kind != KindHome && d.URL != "" { + if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) { + pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL) } else { - if d.ExpandedPermalink != "" { - pagePath = filepath.Join(pagePath, d.ExpandedPermalink) + pagePath = filepath.Join(pagePath, d.URL) + } + if d.Addends != "" { + pagePath = filepath.Join(pagePath, d.Addends) + } else if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") { + pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) + } + } else if d.Kind == KindPage { + if d.ExpandedPermalink != "" { + pagePath = filepath.Join(pagePath, d.ExpandedPermalink) - } else { - if d.Dir != "" { - pagePath = filepath.Join(pagePath, d.Dir) - } - if d.BaseName != "" { - pagePath = filepath.Join(pagePath, d.BaseName) - } + } else { + if d.Dir != "" { + pagePath = filepath.Join(pagePath, d.Dir) } + if d.BaseName != "" { + pagePath = filepath.Join(pagePath, d.BaseName) + } + } - if d.Addends != "" { - pagePath = filepath.Join(pagePath, d.Addends) - } + if d.Addends != "" { + pagePath = filepath.Join(pagePath, d.Addends) + } - if isUgly { - pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix - } else { - pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) - } + if isUgly { + pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix + } else { + pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) + } - if d.LangPrefix != "" { - pagePath = filepath.Join(d.LangPrefix, pagePath) - } + if d.LangPrefix != "" { + pagePath = filepath.Join(d.LangPrefix, pagePath) } } else { if d.Addends != "" {