ref: 10a773cde7a7d5860512917ae2aa14b683318000
parent: 8b43d39ef312e5b9e3549aa8fa75a1464a467ae8
author: Nate Finch <nate.finch@gmail.com>
date: Sat Oct 15 03:35:32 EDT 2016
Implement support for alias templates This change adds a canonical alias.html template that is used for page redirects, and passes the page as data to the template under .Page Fixes #2533 Closes #2576
--- a/docs/content/extras/aliases.md
+++ b/docs/content/extras/aliases.md
@@ -94,3 +94,11 @@
```
The `http-equiv="refresh"` line is what performs the redirect, in 0 seconds in this case.
+
+## Customizing
+
+You may customize this alias page by creating an alias.html template in the
+layouts folder of your site. In this case, the data passed to the template is
+
+* Permalink - the link to the page being aliased
+* Page - the Page data for the page being aliased
\ No newline at end of file
--- a/docs/content/templates/overview.md
+++ b/docs/content/templates/overview.md
@@ -71,4 +71,6 @@
### [404](/templates/404/)
This template will create a 404.html page used when hosting on GitHub Pages
+### [Alias](/extras/aliases/#customizing)
+This template will override the default page used to create aliases of pages.
--- /dev/null
+++ b/hugolib/alias_test.go
@@ -1,0 +1,60 @@
+// Copyright 2015 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import (
+ "path/filepath"
+ "testing"
+)
+
+const pageWithAlias = `---
+title: Has Alias
+aliases: ["foo/bar/"]
+---
+For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
+`
+
+const basicTemplate = "<html><body>{{.Content}}</body></html>"+const aliasTemplate = "<html><body>ALIASTEMPLATE</body></html>"
+
+func TestAlias(t *testing.T) {+ testCommonResetState()
+ writeSource(t, filepath.Join("content", "page.md"), pageWithAlias)+ writeSource(t, filepath.Join("layouts", "_default", "single.html"), basicTemplate)+
+ if err := buildAndRenderSite(newSiteDefaultLang()); err != nil {+ t.Fatalf("Failed to build site: %s", err)+ }
+
+ // the real page
+ assertFileContent(t, filepath.Join("public", "page", "index.html"), false, "For some moments the old man")+ // the alias redirector
+ assertFileContent(t, filepath.Join("public", "foo", "bar", "index.html"), false, "<meta http-equiv=\"refresh\" content=\"0; ")+}
+
+func TestAliasTemplate(t *testing.T) {+ testCommonResetState()
+ writeSource(t, filepath.Join("content", "page.md"), pageWithAlias)+ writeSource(t, filepath.Join("layouts", "_default", "single.html"), basicTemplate)+ writeSource(t, filepath.Join("layouts", "alias.html"), aliasTemplate)+
+ if err := buildAndRenderSite(newSiteDefaultLang()); err != nil {+ t.Fatalf("Failed to build site: %s", err)+ }
+
+ // the real page
+ assertFileContent(t, filepath.Join("public", "page", "index.html"), false, "For some moments the old man")+ // the alias redirector
+ assertFileContent(t, filepath.Join("public", "foo", "bar", "index.html"), false, "ALIASTEMPLATE")+}
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1563,9 +1563,8 @@
if err != nil {return err
}
-
for _, a := range p.Aliases {- if err := s.writeDestAlias(a, plink); err != nil {+ if err := s.writeDestAlias(a, plink, p); err != nil {return err
}
}
@@ -1576,13 +1575,13 @@
if s.Info.defaultContentLanguageInSubdir {mainLangURL := helpers.AbsURL(mainLang, false)
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)- if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL); err != nil {+ if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL, nil); err != nil {return err
}
} else { mainLangURL := helpers.AbsURL("", false) jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)- if err := s.publishDestAlias(s.languageAliasTarget(), mainLang, mainLangURL); err != nil {+ if err := s.publishDestAlias(s.languageAliasTarget(), mainLang, mainLangURL, nil); err != nil {return err
}
}
@@ -1819,7 +1818,7 @@
paginatePath = helpers.Config().GetString("paginatePath")// write alias for page 1
- s.writeDestAlias(helpers.PaginateAliasPath(baseWithLanguagePrefix, 1), n.Permalink())
+ s.writeDestAlias(helpers.PaginateAliasPath(baseWithLanguagePrefix, 1), n.Permalink(), nil)
pagers := n.paginator.Pagers()
@@ -1954,7 +1953,7 @@
paginatePath := helpers.Config().GetString("paginatePath")// write alias for page 1
- s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base))
+ s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base), nil)
pagers := n.paginator.Pagers()
@@ -2016,7 +2015,7 @@
{// write alias for page 1
// TODO(bep) ml all of these n.addLang ... fix.
- s.writeDestAlias(n.addLangPathPrefix(helpers.PaginateAliasPath("", 1)), n.Permalink())+ s.writeDestAlias(n.addLangPathPrefix(helpers.PaginateAliasPath("", 1)), n.Permalink(), nil)}
pagers := n.paginator.Pagers()
@@ -2479,6 +2478,7 @@
if s.targets.alias == nil { s.targets.alias = &target.HTMLRedirectAlias{PublishDir: s.absPublishDir(),
+ Templates: s.owner.tmpl.Lookup("alias.html"),}
}
if s.targets.languageAlias == nil {@@ -2501,11 +2501,11 @@
}
// AliasPublisher
-func (s *Site) writeDestAlias(path string, permalink string) (err error) {- return s.publishDestAlias(s.aliasTarget(), path, permalink)
+func (s *Site) writeDestAlias(path, permalink string, p *Page) (err error) {+ return s.publishDestAlias(s.aliasTarget(), path, permalink, p)
}
-func (s *Site) publishDestAlias(aliasPublisher target.AliasPublisher, path string, permalink string) (err error) {+func (s *Site) publishDestAlias(aliasPublisher target.AliasPublisher, path, permalink string, p *Page) (err error) { if viper.GetBool("RelativeURLs") {// convert `permalink` into URI relative to location of `path`
baseURL := helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))@@ -2519,7 +2519,7 @@
permalink = filepath.ToSlash(permalink)
}
jww.DEBUG.Println("creating alias:", path, "redirecting to", permalink)- return aliasPublisher.Publish(path, permalink)
+ return aliasPublisher.Publish(path, permalink, p)
}
func (s *Site) draftStats() string {--- a/target/htmlredirect.go
+++ b/target/htmlredirect.go
@@ -39,7 +39,7 @@
type AliasPublisher interface {Translator
- Publish(string, string) error
+ Publish(path string, permalink string, page interface{}) error}
type HTMLRedirectAlias struct {@@ -121,9 +121,10 @@
type AliasNode struct {Permalink string
+ Page interface{}}
-func (h *HTMLRedirectAlias) Publish(path string, permalink string) (err error) {+func (h *HTMLRedirectAlias) Publish(path string, permalink string, page interface{}) (err error) { if path, err = h.Translate(path); err != nil { jww.ERROR.Printf("%s, skipping.", err)return nil
@@ -137,10 +138,11 @@
template := defaultAliasTemplates
if h.Templates != nil {template = h.Templates
+ t = "alias.html"
}
buffer := new(bytes.Buffer)
- err = template.ExecuteTemplate(buffer, t, &AliasNode{permalink})+ err = template.ExecuteTemplate(buffer, t, &AliasNode{permalink, page}) if err != nil {return
}
--
⑨