1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-04 17:37:48 +01:00
gojekyll/plugins/redirect_from.go

95 lines
2.6 KiB
Go
Raw Normal View History

2017-07-09 01:57:41 +02:00
package plugins
import (
"bytes"
"fmt"
"io"
2017-07-09 03:41:35 +02:00
"strings"
"text/template"
2017-07-09 01:57:41 +02:00
"github.com/osteele/gojekyll/pages"
)
2017-07-09 04:47:50 +02:00
type jekyllRedirectFromPlugin struct{ plugin }
2017-07-09 01:57:41 +02:00
var redirectTemplate *template.Template
func init() {
2017-07-09 04:47:50 +02:00
register("jekyll-redirect-from", jekyllRedirectFromPlugin{})
tmpl, err := template.New("redirect_from").Parse(redirectFromTemplateSource)
2017-07-09 01:57:41 +02:00
if err != nil {
panic(err)
}
redirectTemplate = tmpl
}
2017-07-09 04:47:50 +02:00
func (p jekyllRedirectFromPlugin) PostRead(site Site) error {
2017-07-09 01:57:41 +02:00
redirections := []pages.Document{}
for _, p := range site.Pages() {
rd, ok := p.FrontMatter()["redirect_from"]
if ok {
switch rd := rd.(type) {
case string:
2017-07-09 03:41:35 +02:00
siteurl := site.Config().AbsoluteURL
baseurl := site.Config().BaseURL
var p = redirectionDoc{From: rd, To: strings.Join([]string{siteurl, baseurl, p.Permalink()}, "")}
2017-07-09 01:57:41 +02:00
redirections = append(redirections, &p)
default:
fmt.Printf("unimplemented redirect_from type: %T\n", rd)
}
}
rd, ok = p.FrontMatter()["redirect_to"]
if ok {
switch rd := rd.(type) {
case string:
r := redirectionDoc{From: rd, To: p.Permalink()}
2017-07-09 01:57:41 +02:00
p.SetContent(r.Content())
default:
fmt.Printf("unimplemented redirect_from type: %T\n", rd)
}
}
}
for _, p := range redirections {
site.AddDocument(p, true)
}
return nil
}
type redirectionDoc struct {
From string
To string
}
func (d *redirectionDoc) Permalink() string { return d.From }
func (d *redirectionDoc) SourcePath() string { return "" } // FIXME bad design
func (d *redirectionDoc) OutputExt() string { return ".html" }
func (d *redirectionDoc) Published() bool { return true }
func (d *redirectionDoc) Static() bool { return false } // FIXME means different things to different callers
func (d *redirectionDoc) Categories() []string { return []string{} }
func (d *redirectionDoc) Tags() []string { return []string{} }
func (d *redirectionDoc) Content() []byte {
buf := new(bytes.Buffer)
if err := redirectTemplate.Execute(buf, d); err != nil {
panic(err)
}
return buf.Bytes()
}
func (d *redirectionDoc) Write(w io.Writer, c pages.RenderingContext) error {
return redirectTemplate.Execute(w, d)
}
2017-07-09 01:57:41 +02:00
// Adapted from https://github.com/jekyll/jekyll-redirect-from
const redirectFromTemplateSource = `<!DOCTYPE html>
2017-07-09 01:57:41 +02:00
<html lang="en-US">
<meta charset="utf-8">
2017-07-09 03:41:35 +02:00
<title>Redirecting</title>
2017-07-09 01:57:41 +02:00
<link rel="canonical" href="{{ .To }}">
<meta http-equiv="refresh" content="0; url={{ .To }}">
<meta name="robots" content="noindex">
2017-07-09 03:41:35 +02:00
<h1>Redirecting</h1>
2017-07-09 01:57:41 +02:00
<a href="{{ .To }}">Click here if you are not redirected.</a>
<script>location="{{ .To }}"</script>
</html>`