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
This commit is contained in:
Nate Finch 2016-10-15 07:35:32 -04:00 committed by Bjørn Erik Pedersen
parent 8b43d39ef3
commit 10a773cde7
5 changed files with 86 additions and 14 deletions

View file

@ -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

View file

@ -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.

60
hugolib/alias_test.go Normal file
View file

@ -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")
}

View file

@ -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 {

View file

@ -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
}