From e2a28114d196689337fea8a78f250c7ef706b4be Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 9 Aug 2013 21:35:23 +0100 Subject: [PATCH] Add redirect to page parameters and redirects example --- docs/content/doc/front-matter.md | 11 +++--- docs/content/doc/redirects.md | 37 +++++++++++++++++++ .../content/redirects/my-awesome-blog-post.md | 5 +++ docs/layouts/chrome/menu.html | 1 + docs/layouts/redirects/single.html | 8 ++++ hugolib/page.go | 17 +++++++-- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 docs/content/doc/redirects.md create mode 100644 docs/content/redirects/my-awesome-blog-post.md create mode 100644 docs/layouts/redirects/single.html diff --git a/docs/content/doc/front-matter.md b/docs/content/doc/front-matter.md index 0fb03b443..566fb1fba 100644 --- a/docs/content/doc/front-matter.md +++ b/docs/content/doc/front-matter.md @@ -4,8 +4,8 @@ date = "2013-07-01" +++ The front matter is one of the features that gives Hugo it's strength. It enables -you to include the meta data of the content right with it. Hugo supports a few -different formats each with their own identifying tokens. +you to include the meta data of the content right with it. Hugo supports a few +different formats each with their own identifying tokens. Supported formats:
**YAML**, identified by '\-\-\-'.
@@ -24,7 +24,7 @@ Supported formats:
- "VIM" slug: "spf13-vim-3-0-release-and-new-website" --- - Content of the file goes Here + Content of the file goes Here ### TOML Example @@ -39,7 +39,7 @@ Supported formats:
] slug = "spf13-vim-3-0-release-and-new-website" +++ - Content of the file goes Here + Content of the file goes Here ### JSON Example @@ -54,7 +54,7 @@ Supported formats:
], "slug": "spf13-vim-3-0-release-and-new-website", } - Content of the file goes Here + Content of the file goes Here ### Variables @@ -71,6 +71,7 @@ any variable they want to. These will be placed into the `.Params` variable avai #### Optional +**redirect** Mark the post as a redirect post
**draft** If true the content will not be rendered unless `hugo` is called with -d
**type** The type of the content (will be derived from the directory automatically if unset).
**markup** (Experimental) Specify "rst" for reStructuredText (requires diff --git a/docs/content/doc/redirects.md b/docs/content/doc/redirects.md new file mode 100644 index 000000000..74b742d4b --- /dev/null +++ b/docs/content/doc/redirects.md @@ -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 + + + + + + + + + ``` + +Now when you go to `/my-awesome-blog-post/` it will do a meta redirect to +`/docs/redirects/`. \ No newline at end of file diff --git a/docs/content/redirects/my-awesome-blog-post.md b/docs/content/redirects/my-awesome-blog-post.md new file mode 100644 index 000000000..dedf283d1 --- /dev/null +++ b/docs/content/redirects/my-awesome-blog-post.md @@ -0,0 +1,5 @@ +--- +redirect: true +slug: /my-awesome-blog-post/ +url: /docs/redirects1/ +--- \ No newline at end of file diff --git a/docs/layouts/chrome/menu.html b/docs/layouts/chrome/menu.html index e0eaf7b23..99e9a04a4 100644 --- a/docs/layouts/chrome/menu.html +++ b/docs/layouts/chrome/menu.html @@ -19,6 +19,7 @@
  • ShortCodes
  • Indexes
  • +
  • Redirects
  • Release Notes
  • diff --git a/docs/layouts/redirects/single.html b/docs/layouts/redirects/single.html new file mode 100644 index 000000000..e99e75811 --- /dev/null +++ b/docs/layouts/redirects/single.html @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hugolib/page.go b/hugolib/page.go index ff00493b2..93f2f6dbe 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -42,6 +42,7 @@ type Page struct { RenderedContent *bytes.Buffer contentType string Draft bool + Redirect bool Tmpl *template.Template Markup string PageMeta @@ -85,15 +86,22 @@ func initializePage(filename string) (page Page) { page.Params = make(map[string]interface{}) page.Keywords = make([]string, 10, 30) page.Markup = "md" - page.setSection() return page } func (p *Page) setSection() { 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 } } @@ -102,7 +110,7 @@ func (page *Page) Type() string { if page.contentType != "" { return page.contentType } - + page.setSection() if x := page.GetSection(); x != "" { return x } @@ -130,6 +138,7 @@ func (page *Page) Layout(l ...string) string { // TODO initalize separately... load from reader (file, or []byte) func NewPage(filename string) *Page { p := initializePage(filename) + if err := p.buildPageFromFile(); err != nil { fmt.Println(err) os.Exit(1) @@ -299,6 +308,8 @@ func (page *Page) handleMetaData(f interface{}) error { page.layout = interfaceToString(v) case "markup": page.Markup = interfaceToString(v) + case "redirect": + page.Redirect = interfaceToBool(v) case "status": page.Status = interfaceToString(v) default: