node to page: Handle URLs

This includes removing the error return value from Permalink and RelPermalink.

We ignore that error all over the place, so we might as well remove it.

Updates #2297
This commit is contained in:
Bjørn Erik Pedersen 2016-11-15 10:43:49 +01:00
parent c8d3124dde
commit 524eb16686
8 changed files with 105 additions and 63 deletions

View file

@ -136,7 +136,7 @@ func (h *HugoSites) process(config *BuildCfg, events ...fsnotify.Event) error {
} }
func (h *HugoSites) assemble(config *BuildCfg) error { func (h *HugoSites) assemble(config *BuildCfg) error {
// TODO(bep) np we could probably wait and do this in one go later // TODO(bep) we could probably wait and do this in one go later
h.setupTranslations() h.setupTranslations()
if len(h.Sites) > 1 { if len(h.Sites) > 1 {

View file

@ -86,13 +86,13 @@ func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) {
doc1en := enSite.RegularPages[0] doc1en := enSite.RegularPages[0]
doc1fr := frSite.RegularPages[0] doc1fr := frSite.RegularPages[0]
enPerm, _ := doc1en.Permalink() enPerm := doc1en.Permalink()
enRelPerm, _ := doc1en.RelPermalink() enRelPerm := doc1en.RelPermalink()
require.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", enPerm) require.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", enPerm)
require.Equal(t, "/blog/en/sect/doc1-slug/", enRelPerm) require.Equal(t, "/blog/en/sect/doc1-slug/", enRelPerm)
frPerm, _ := doc1fr.Permalink() frPerm := doc1fr.Permalink()
frRelPerm, _ := doc1fr.RelPermalink() frRelPerm := doc1fr.RelPermalink()
// Main language in root // Main language in root
require.Equal(t, replaceDefaultContentLanguageValue("http://example.com/blog/fr/sect/doc1/", defaultInSubDir), frPerm) require.Equal(t, replaceDefaultContentLanguageValue("http://example.com/blog/fr/sect/doc1/", defaultInSubDir), frPerm)
require.Equal(t, replaceDefaultContentLanguageValue("/blog/fr/sect/doc1/", defaultInSubDir), frRelPerm) require.Equal(t, replaceDefaultContentLanguageValue("/blog/fr/sect/doc1/", defaultInSubDir), frRelPerm)
@ -223,18 +223,18 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
assert.Len(t, enSite.AllPages, 28, "should have 28 total pages (including translations and index types)") assert.Len(t, enSite.AllPages, 28, "should have 28 total pages (including translations and index types)")
doc1en := enSite.RegularPages[0] doc1en := enSite.RegularPages[0]
permalink, err := doc1en.Permalink() permalink := doc1en.Permalink()
assert.NoError(t, err, "permalink call failed") assert.NoError(t, err, "permalink call failed")
assert.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", permalink, "invalid doc1.en permalink") assert.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", permalink, "invalid doc1.en permalink")
assert.Len(t, doc1en.Translations(), 1, "doc1-en should have one translation, excluding itself") assert.Len(t, doc1en.Translations(), 1, "doc1-en should have one translation, excluding itself")
doc2 := enSite.RegularPages[1] doc2 := enSite.RegularPages[1]
permalink, err = doc2.Permalink() permalink = doc2.Permalink()
assert.NoError(t, err, "permalink call failed") assert.NoError(t, err, "permalink call failed")
assert.Equal(t, "http://example.com/blog/en/sect/doc2/", permalink, "invalid doc2 permalink") assert.Equal(t, "http://example.com/blog/en/sect/doc2/", permalink, "invalid doc2 permalink")
doc3 := enSite.RegularPages[2] doc3 := enSite.RegularPages[2]
permalink, err = doc3.Permalink() permalink = doc3.Permalink()
assert.NoError(t, err, "permalink call failed") assert.NoError(t, err, "permalink call failed")
// Note that /superbob is a custom URL set in frontmatter. // Note that /superbob is a custom URL set in frontmatter.
// We respect that URL literally (it can be /search.json) // We respect that URL literally (it can be /search.json)
@ -246,7 +246,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
assert.Equal(t, doc2.Next, doc3, "doc3 should follow doc2, in .Next") assert.Equal(t, doc2.Next, doc3, "doc3 should follow doc2, in .Next")
doc1fr := doc1en.Translations()[0] doc1fr := doc1en.Translations()[0]
permalink, err = doc1fr.Permalink() permalink = doc1fr.Permalink()
assert.NoError(t, err, "permalink call failed") assert.NoError(t, err, "permalink call failed")
assert.Equal(t, "http://example.com/blog/fr/sect/doc1/", permalink, "invalid doc1fr permalink") assert.Equal(t, "http://example.com/blog/fr/sect/doc1/", permalink, "invalid doc1fr permalink")
@ -255,16 +255,14 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
assert.Equal(t, "fr", doc1fr.Language().Lang) assert.Equal(t, "fr", doc1fr.Language().Lang)
doc4 := enSite.AllPages[4] doc4 := enSite.AllPages[4]
permalink, err = doc4.Permalink() permalink = doc4.Permalink()
assert.NoError(t, err, "permalink call failed")
assert.Equal(t, "http://example.com/blog/fr/sect/doc4/", permalink, "invalid doc4 permalink") assert.Equal(t, "http://example.com/blog/fr/sect/doc4/", permalink, "invalid doc4 permalink")
assert.Equal(t, "/blog/fr/sect/doc4/", doc4.URL()) assert.Equal(t, "/blog/fr/sect/doc4/", doc4.URL())
assert.Len(t, doc4.Translations(), 0, "found translations for doc4") assert.Len(t, doc4.Translations(), 0, "found translations for doc4")
doc5 := enSite.AllPages[5] doc5 := enSite.AllPages[5]
permalink, err = doc5.Permalink() permalink = doc5.Permalink()
assert.NoError(t, err, "permalink call failed")
assert.Equal(t, "http://example.com/blog/fr/somewhere/else/doc5", permalink, "invalid doc5 permalink") assert.Equal(t, "http://example.com/blog/fr/somewhere/else/doc5", permalink, "invalid doc5 permalink")
// Taxonomies and their URLs // Taxonomies and their URLs

View file

@ -560,7 +560,6 @@ func TestHomeNodeMenu(t *testing.T) {
s := setupMenuTests(t, menuPageSources) s := setupMenuTests(t, menuPageSources)
home := s.getPage(KindHome) home := s.getPage(KindHome)
homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL()} homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL()}
for i, this := range []struct { for i, this := range []struct {
@ -583,7 +582,7 @@ func TestHomeNodeMenu(t *testing.T) {
if isMenuCurrent != this.isMenuCurrent { if isMenuCurrent != this.isMenuCurrent {
fmt.Println("isMenuCurrent", isMenuCurrent) fmt.Println("isMenuCurrent", isMenuCurrent)
fmt.Printf("this: %#v\n", this) fmt.Printf("this: %#v\n", this)
t.Errorf("[%d] Wrong result from IsMenuCurrent: %v for %q", i, isMenuCurrent, this.menu) t.Errorf("[%d] Wrong result from IsMenuCurrent: %v for %q", i, isMenuCurrent, this.menuItem)
} }
if hasMenuCurrent != this.hasMenuCurrent { if hasMenuCurrent != this.hasMenuCurrent {

View file

@ -394,6 +394,7 @@ aliases:
`) `)
viper.Set("paginate", 1) viper.Set("paginate", 1)
viper.Set("baseURL", "http://base/")
viper.Set("title", "Hugo Rocks!") viper.Set("title", "Hugo Rocks!")
s := newSiteDefaultLang() s := newSiteDefaultLang()
@ -403,7 +404,7 @@ aliases:
} }
assertFileContent(t, filepath.Join("public", "index.html"), true, "Home With Alias") assertFileContent(t, filepath.Join("public", "index.html"), true, "Home With Alias")
assertFileContent(t, filepath.Join("public", "my", "new", "home.html"), true, "content=\"0; url=/") assertFileContent(t, filepath.Join("public", "my", "new", "home.html"), true, "content=\"0; url=http://base/")
} }
@ -431,6 +432,47 @@ My Section Content
} }
func TestNodesWithURLs(t *testing.T) {
testCommonResetState()
writeLayoutsForNodeAsPageTests(t)
writeRegularPagesForNodeAsPageTests(t)
writeSource(t, filepath.Join("content", "sect", "_index.md"), `---
title: MySection
url: foo.html
---
My Section Content
`)
viper.Set("paginate", 1)
viper.Set("title", "Hugo Rocks!")
viper.Set("baseURL", "http://bep.is/base/")
s := newSiteDefaultLang()
if err := buildAndRenderSite(s); err != nil {
t.Fatalf("Failed to build site: %s", err)
}
assertFileContent(t, filepath.Join("public", "sect", "index.html"), true, "My Section")
p := s.RegularPages[0]
require.Equal(t, "/base/sect1/regular1/", p.URL())
// Section with front matter and url set (which should not be used)
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())
// Home page without front matter
require.Equal(t, "/base/", s.getPage(KindHome).URL())
}
func writeRegularPagesForNodeAsPageTests(t *testing.T) { func writeRegularPagesForNodeAsPageTests(t *testing.T) {
writeRegularPagesForNodeAsPageTestsWithLang(t, "") writeRegularPagesForNodeAsPageTestsWithLang(t, "")
} }

View file

@ -63,6 +63,7 @@ const (
// The following are (currently) temporary nodes, // The following are (currently) temporary nodes,
// i.e. nodes we create just to render in isolation. // i.e. nodes we create just to render in isolation.
kindRSS = "RSS"
kindSitemap = "sitemap" kindSitemap = "sitemap"
kindRobotsTXT = "robotsTXT" kindRobotsTXT = "robotsTXT"
kind404 = "404" kind404 = "404"
@ -698,7 +699,18 @@ func (p *Page) analyzePage() {
} }
func (p *Page) permalink() (*url.URL, error) { func (p *Page) permalink() (*url.URL, error) {
// TODO(bep) this should probably be set once during build. Maybe.
// And simplified.
baseURL := string(p.Site.BaseURL) baseURL := string(p.Site.BaseURL)
if p.IsNode() {
// No permalink config for nodes (currently)
pURL := strings.TrimSpace(p.Site.pathSpec.URLize(p.URLPath.URL))
pURL = p.addLangPathPrefix(pURL)
url := helpers.MakePermalink(baseURL, pURL)
return url, nil
}
dir := strings.TrimSpace(p.Site.pathSpec.MakePath(filepath.ToSlash(strings.ToLower(p.Source.Dir())))) dir := strings.TrimSpace(p.Site.pathSpec.MakePath(filepath.ToSlash(strings.ToLower(p.Source.Dir()))))
pSlug := strings.TrimSpace(p.Site.pathSpec.URLize(p.Slug)) pSlug := strings.TrimSpace(p.Site.pathSpec.URLize(p.Slug))
pURL := strings.TrimSpace(p.Site.pathSpec.URLize(p.URLPath.URL)) pURL := strings.TrimSpace(p.Site.pathSpec.URLize(p.URLPath.URL))
@ -802,36 +814,30 @@ func (p *Page) IsExpired() bool {
return p.ExpiryDate.Before(time.Now()) return p.ExpiryDate.Before(time.Now())
} }
func (p *Page) Permalink() (string, error) { func (p *Page) Permalink() string {
// TODO(bep) np permalink
if p.IsNode() {
return p.Site.permalink(p.URL()), nil
}
link, err := p.permalink() link, err := p.permalink()
if err != nil { if err != nil {
return "", err return ""
} }
return link.String(), nil
return link.String()
} }
func (p *Page) URL() string { func (p *Page) URL() string {
// TODO(bep np URL
if p.IsNode() { if p.IsPage() && p.URLPath.URL != "" {
return p.addLangPathPrefix(p.URLPath.URL)
}
if p.URLPath.URL != "" {
// This is the url set in front matter // This is the url set in front matter
return p.URLPath.URL return p.URLPath.URL
} }
// Fall back to the relative permalink. // Fall back to the relative permalink.
u, _ := p.RelPermalink() u := p.RelPermalink()
return u return u
} }
func (p *Page) RelPermalink() (string, error) { func (p *Page) RelPermalink() string {
link, err := p.permalink() link, err := p.permalink()
if err != nil { if err != nil {
return "", err return ""
} }
if viper.GetBool("canonifyURLs") { if viper.GetBool("canonifyURLs") {
@ -839,16 +845,27 @@ func (p *Page) RelPermalink() (string, error) {
// have to return the URL relative from baseURL // have to return the URL relative from baseURL
relpath, err := helpers.GetRelativePath(link.String(), string(p.Site.BaseURL)) relpath, err := helpers.GetRelativePath(link.String(), string(p.Site.BaseURL))
if err != nil { if err != nil {
return "", err return ""
} }
return "/" + filepath.ToSlash(relpath), nil
relpath = filepath.ToSlash(relpath)
if relpath[0] == '.' {
relpath = relpath[1:]
}
if !strings.HasPrefix(relpath, "/") {
relpath = "/" + relpath
}
return relpath
} }
link.Scheme = "" link.Scheme = ""
link.Host = "" link.Host = ""
link.User = nil link.User = nil
link.Opaque = "" link.Opaque = ""
return link.String(), nil return link.String()
} }
var ErrHasDraftAndPublished = errors.New("both draft and published parameters were found in page's frontmatter") var ErrHasDraftAndPublished = errors.New("both draft and published parameters were found in page's frontmatter")
@ -1109,7 +1126,7 @@ func (p *Page) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
// The following logic is kept from back when Hugo had both Page and Node types. // The following logic is kept from back when Hugo had both Page and Node types.
// TODO(bep) consolidate / clean // TODO(bep) consolidate / clean
me := MenuEntry{Name: p.Title, URL: p.Site.createNodeMenuEntryURL(p.URL())} me := MenuEntry{Name: p.Title, URL: p.URL()}
if !me.IsSameResource(inme) { if !me.IsSameResource(inme) {
return false return false
@ -1154,7 +1171,7 @@ func (p *Page) Menus() PageMenus {
p.pageMenus = PageMenus{} p.pageMenus = PageMenus{}
if ms, ok := p.Params["menu"]; ok { if ms, ok := p.Params["menu"]; ok {
link, _ := p.RelPermalink() link := p.RelPermalink()
me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, URL: link} me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, URL: link}
@ -1719,13 +1736,13 @@ func (p *Page) setNodeTypeVars(s *Site) {
// Set Node URL // Set Node URL
switch p.Kind { switch p.Kind {
case KindHome: case KindHome:
p.URLPath.URL = "" p.URLPath.URL = "/"
case KindSection: case KindSection:
p.URLPath.URL = p.sections[0] p.URLPath.URL = "/" + p.sections[0] + "/"
case KindTaxonomy: case KindTaxonomy:
p.URLPath.URL = path.Join(p.sections...) p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
case KindTaxonomyTerm: case KindTaxonomyTerm:
p.URLPath.URL = path.Join(p.sections...) p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
} }
p.site = s p.site = s

View file

@ -81,20 +81,14 @@ func TestPermalink(t *testing.T) {
}) })
} }
u, err := p.Permalink() u := p.Permalink()
if err != nil {
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
}
expected := test.expectedAbs expected := test.expectedAbs
if u != expected { if u != expected {
t.Errorf("Test %d: Expected abs url: %s, got: %s", i, expected, u) t.Errorf("Test %d: Expected abs url: %s, got: %s", i, expected, u)
} }
u, err = p.RelPermalink() u = p.RelPermalink()
if err != nil {
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
}
expected = test.expectedRel expected = test.expectedRel
if u != expected { if u != expected {

View file

@ -299,13 +299,9 @@ func (s *SiteInfo) refLink(ref string, page *Page, relative bool) (string, error
} }
if relative { if relative {
link, err = target.RelPermalink() link = target.RelPermalink()
} else { } else {
link, err = target.Permalink() link = target.Permalink()
}
if err != nil {
return "", err
} }
} }
@ -389,11 +385,8 @@ func (s *SiteInfo) SourceRelativeLink(ref string, currentPage *Page) (string, er
return "", fmt.Errorf("No page found for \"%s\" on page \"%s\".\n", ref, currentPage.Source.Path()) return "", fmt.Errorf("No page found for \"%s\" on page \"%s\".\n", ref, currentPage.Source.Path())
} }
link, err = target.RelPermalink() link = target.RelPermalink()
if err != nil {
return "", err
}
} }
if refURL.Fragment != "" { if refURL.Fragment != "" {

View file

@ -99,7 +99,7 @@ func (s *Site) renderPaginator(p *Page) error {
aliasPath := p.addLangPathPrefix(helpers.PaginateAliasPath(path.Join(p.sections...), 1)) aliasPath := p.addLangPathPrefix(helpers.PaginateAliasPath(path.Join(p.sections...), 1))
//TODO(bep) np node.permalink //TODO(bep) np node.permalink
link, _ := p.Permalink() link := p.Permalink()
s.writeDestAlias(aliasPath, link, nil) s.writeDestAlias(aliasPath, link, nil)
pagers := p.paginator.Pagers() pagers := p.paginator.Pagers()
@ -144,6 +144,7 @@ func (s *Site) renderRSS(p *Page) error {
// TODO(bep) np check RSS titles // TODO(bep) np check RSS titles
// TODO(bep) np check RSS page limit, 50? // TODO(bep) np check RSS page limit, 50?
rssNode := p.copy() rssNode := p.copy()
rssNode.Kind = kindRSS
// TODO(bep) np todelido URL // TODO(bep) np todelido URL
rssURI := s.Language.GetString("rssURI") rssURI := s.Language.GetString("rssURI")
@ -248,10 +249,8 @@ func (s *Site) renderAliases() error {
continue continue
} }
plink, err := p.Permalink() plink := p.Permalink()
if err != nil {
return err
}
for _, a := range p.Aliases { for _, a := range p.Aliases {
if err := s.writeDestAlias(a, plink, p); err != nil { if err := s.writeDestAlias(a, plink, p); err != nil {
return err return err