summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNate Finch <nate.finch@gmail.com>2016-10-15 07:35:32 -0400
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-10-15 15:25:05 +0200
commit10a773cde7a7d5860512917ae2aa14b683318000 (patch)
tree2210c46781c6040a8fdc2783eda7b9957f53e167
parent8b43d39ef312e5b9e3549aa8fa75a1464a467ae8 (diff)
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
-rw-r--r--docs/content/extras/aliases.md8
-rw-r--r--docs/content/templates/overview.md2
-rw-r--r--hugolib/alias_test.go60
-rw-r--r--hugolib/site.go22
-rw-r--r--target/htmlredirect.go8
5 files changed, 86 insertions, 14 deletions
diff --git a/docs/content/extras/aliases.md b/docs/content/extras/aliases.md
index 9d20e12ba..da861387c 100644
--- a/docs/content/extras/aliases.md
+++ b/docs/content/extras/aliases.md
@@ -94,3 +94,11 @@ Assuming a baseurl of `mysite.tld`, the contents of the html file will look some
```
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
diff --git a/docs/content/templates/overview.md b/docs/content/templates/overview.md
index 99eb00432..3b41a6641 100644
--- a/docs/content/templates/overview.md
+++ b/docs/content/templates/overview.md
@@ -71,4 +71,6 @@ Used to render the XML sitemap
### [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.
diff --git a/hugolib/alias_test.go b/hugolib/alias_test.go
new file mode 100644
index 000000000..180fb553c
--- /dev/null
+++ b/hugolib/alias_test.go
@@ -0,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")
+}
diff --git a/hugolib/site.go b/hugolib/site.go
index 73a177f77..7b74aa02f 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1563,9 +1563,8 @@ func (s *Site) renderAliases() error {
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 @@ func (s *Site) renderAliases() error {
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 @@ func taxonomyRenderer(prepare bool, s *Site, taxes <-chan taxRenderInfo, results
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 @@ func (s *Site) renderSectionLists(prepare bool) error {
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 @@ func (s *Site) renderHomePage(prepare bool) error {
{
// 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 @@ func (s *Site) initTargetList() {
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 @@ func (s *Site) writeDestPage(path string, publisher target.Publisher, reader io.
}
// 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 @@ func (s *Site) publishDestAlias(aliasPublisher target.AliasPublisher, path strin
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 {
diff --git a/target/htmlredirect.go b/target/htmlredirect.go
index 81051589a..20e064c88 100644
--- a/target/htmlredirect.go
+++ b/target/htmlredirect.go
@@ -39,7 +39,7 @@ func init() {
type AliasPublisher interface {
Translator
- Publish(string, string) error
+ Publish(path string, permalink string, page interface{}) error
}
type HTMLRedirectAlias struct {
@@ -121,9 +121,10 @@ func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error
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 @@ func (h *HTMLRedirectAlias) Publish(path string, permalink string) (err error) {
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
}