Add redirect to page parameters and redirects example

This commit is contained in:
Ross Lawley 2013-08-09 21:35:23 +01:00
parent 4f17ad69a7
commit e2a28114d1
6 changed files with 71 additions and 8 deletions

View file

@ -71,6 +71,7 @@ any variable they want to. These will be placed into the `.Params` variable avai
#### Optional #### Optional
**redirect** Mark the post as a redirect post<br>
**draft** If true the content will not be rendered unless `hugo` is called with -d<br> **draft** If true the content will not be rendered unless `hugo` is called with -d<br>
**type** The type of the content (will be derived from the directory automatically if unset).<br> **type** The type of the content (will be derived from the directory automatically if unset).<br>
**markup** (Experimental) Specify "rst" for reStructuredText (requires **markup** (Experimental) Specify "rst" for reStructuredText (requires

View file

@ -0,0 +1,37 @@
---
title: "Redirects"
Pubdate: "2013-07-09"
---
For people migrating existing published content to Hugo theres a good chance
you need a mechanism to handle redirecting old urls.
Luckily, this can be handled easily in a couple of easy steps.
1. Create a special post for the redirect and mark the file as a `redirect`
file in the front matter. Here is an example
`content/redirects/my-awesome-blog-post.md` :
```markdown
---
redirect: true
slug: /my-awesome-blog-post/
url: /docs/redirects/
---
```
2. Set the redirect template `layouts/redirects/single.html`:
```html
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="{{ .Url }}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ .Url }}" />
</head>
</html>
```
Now when you go to `/my-awesome-blog-post/` it will do a meta redirect to
`/docs/redirects/`.

View file

@ -0,0 +1,5 @@
---
redirect: true
slug: /my-awesome-blog-post/
url: /docs/redirects1/
---

View file

@ -19,6 +19,7 @@
<li class="nav-header">Extras</li> <li class="nav-header">Extras</li>
<li> <a href="/doc/shortcodes">ShortCodes</a></li> <li> <a href="/doc/shortcodes">ShortCodes</a></li>
<li> <a href="/doc/indexes">Indexes</a></li> <li> <a href="/doc/indexes">Indexes</a></li>
<li> <a href="/doc/redirects">Redirects</a></li>
<li class="divider"></li> <li class="divider"></li>
<li class="nav-header">Meta</li> <li class="nav-header">Meta</li>
<li> <a href="/doc/release-notes">Release Notes</a></li> <li> <a href="/doc/release-notes">Release Notes</a></li>

View file

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="{{ .Url }}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ .Url }}" />
</head>
</html>

View file

@ -42,6 +42,7 @@ type Page struct {
RenderedContent *bytes.Buffer RenderedContent *bytes.Buffer
contentType string contentType string
Draft bool Draft bool
Redirect bool
Tmpl *template.Template Tmpl *template.Template
Markup string Markup string
PageMeta PageMeta
@ -85,15 +86,22 @@ func initializePage(filename string) (page Page) {
page.Params = make(map[string]interface{}) page.Params = make(map[string]interface{})
page.Keywords = make([]string, 10, 30) page.Keywords = make([]string, 10, 30)
page.Markup = "md" page.Markup = "md"
page.setSection()
return page return page
} }
func (p *Page) setSection() { func (p *Page) setSection() {
x := strings.Split(p.FileName, string(os.PathSeparator)) x := strings.Split(p.FileName, string(os.PathSeparator))
section := x[len(x)-2]
if section := x[len(x)-2]; section != "content" { c := p.Site.Config
systemDirs := map[string] bool {
c.ContentDir: true,
c.StaticDir: true,
c.LayoutDir: true,
}
if !systemDirs[section] && !p.Redirect {
p.Section = section p.Section = section
} }
} }
@ -102,7 +110,7 @@ func (page *Page) Type() string {
if page.contentType != "" { if page.contentType != "" {
return page.contentType return page.contentType
} }
page.setSection()
if x := page.GetSection(); x != "" { if x := page.GetSection(); x != "" {
return x return x
} }
@ -130,6 +138,7 @@ func (page *Page) Layout(l ...string) string {
// TODO initalize separately... load from reader (file, or []byte) // TODO initalize separately... load from reader (file, or []byte)
func NewPage(filename string) *Page { func NewPage(filename string) *Page {
p := initializePage(filename) p := initializePage(filename)
if err := p.buildPageFromFile(); err != nil { if err := p.buildPageFromFile(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -299,6 +308,8 @@ func (page *Page) handleMetaData(f interface{}) error {
page.layout = interfaceToString(v) page.layout = interfaceToString(v)
case "markup": case "markup":
page.Markup = interfaceToString(v) page.Markup = interfaceToString(v)
case "redirect":
page.Redirect = interfaceToBool(v)
case "status": case "status":
page.Status = interfaceToString(v) page.Status = interfaceToString(v)
default: default: