shithub: hugo

Download patch

ref: 8a409894bdb0972e152a2eccc47a2738568e1cfc
parent: 46db900dab9c0e6fcd9d227f10a32fb24f5c8bd9
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Jan 12 12:17:10 EST 2018

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

--- a/hugolib/node_as_page_test.go
+++ b/hugolib/node_as_page_test.go
@@ -591,7 +591,7 @@
 
 	writeSource(t, fs, filepath.Join("content", "sect", "_index.md"), `---
 title: MySection
-url: foo.html
+url: /my-section/
 ---
 My Section Content
 `)
@@ -602,7 +602,8 @@
 
 	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 @@
 
 	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())
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -223,7 +223,6 @@
 	Lastmod time.Time
 
 	Sitemap Sitemap
-
 	URLPath
 	permalink    string
 	relPermalink string
@@ -1111,6 +1110,7 @@
 				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 @@
 }
 
 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 @@
 		p.URLPath.URL = "/"
 	case KindPage:
 	default:
-		p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
+		if p.URLPath.URL == "" {
+			p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
+		}
 	}
 }
 
--- a/hugolib/page_paths.go
+++ b/hugolib/page_paths.go
@@ -56,7 +56,7 @@
 	// 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 @@
 		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 @@
 		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 @@
 		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.Addends != "" {
-				pagePath = filepath.Join(pagePath, d.Addends)
+			if d.BaseName != "" {
+				pagePath = filepath.Join(pagePath, d.BaseName)
 			}
+		}
 
-			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.Addends != "" {
+			pagePath = filepath.Join(pagePath, d.Addends)
+		}
 
-			if d.LangPrefix != "" {
-				pagePath = filepath.Join(d.LangPrefix, pagePath)
-			}
+		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)
 		}
 	} else {
 		if d.Addends != "" {