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

95 lines
2.1 KiB
Go
Raw Normal View History

2017-07-09 01:57:41 +02:00
package plugins
import (
"bytes"
2017-07-09 03:41:35 +02:00
"text/template"
2017-07-09 01:57:41 +02:00
)
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-09-03 18:18:17 +02:00
func (p jekyllRedirectFromPlugin) PostReadSite(s Site) error {
ps := s.Pages()
addRedirects, err := p.processRedirectFrom(s, ps)
2017-07-10 21:28:07 +02:00
if err != nil {
return err
}
2017-09-03 18:18:17 +02:00
if err := p.processRedirectTo(s, ps); err != nil {
2017-07-10 21:39:17 +02:00
return err
}
2017-09-03 18:18:17 +02:00
addRedirects()
2017-07-10 21:28:07 +02:00
return nil
}
2017-09-03 18:18:17 +02:00
func (p jekyllRedirectFromPlugin) processRedirectFrom(s Site, ps []Page) (func(), error) {
2017-07-10 21:28:07 +02:00
var (
2017-09-03 18:18:17 +02:00
cfg = s.Config()
siteurl = cfg.AbsoluteURL
baseurl = cfg.BaseURL
prefix = siteurl + baseurl
redirects = []func(){}
2017-07-10 21:28:07 +02:00
)
2017-09-03 18:18:17 +02:00
addRedirectFrom := func(from string, to Page) {
f := func() {
s.AddHTMLPage(from, createRedirectionHTML(prefix+to.URL()), nil)
}
redirects = append(redirects, f)
2017-07-10 21:28:07 +02:00
}
for _, p := range ps {
for _, from := range p.FrontMatter().StringArray("redirect_from") {
2017-07-10 21:28:07 +02:00
addRedirectFrom(from, p)
2017-07-09 01:57:41 +02:00
}
}
2017-09-03 18:18:17 +02:00
return func() {
for _, f := range redirects {
f()
}
}, nil
2017-07-10 21:28:07 +02:00
}
2017-09-03 18:18:17 +02:00
func (p jekyllRedirectFromPlugin) processRedirectTo(_ Site, ps []Page) error {
2017-07-10 21:28:07 +02:00
for _, p := range ps {
sources := p.FrontMatter().StringArray("redirect_to")
2017-07-10 21:28:07 +02:00
if len(sources) > 0 {
2017-09-03 18:18:17 +02:00
p.SetContent(createRedirectionHTML(sources[0]))
2017-07-10 21:28:07 +02:00
}
2017-07-09 01:57:41 +02:00
}
return nil
}
2017-09-03 18:18:17 +02:00
func createRedirectionHTML(to string) string {
r := redirection{to}
buf := new(bytes.Buffer)
2017-09-03 18:18:17 +02:00
if err := redirectTemplate.Execute(buf, r); err != nil {
panic(err)
}
2017-08-10 15:06:53 +02:00
return buf.String()
}
2017-09-03 18:18:17 +02:00
type redirection struct {
To string
}
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-08-10 15:06:53 +02:00
<link rel="canonical" href="{{.To}}">
<meta http-equiv="refresh" content="0; url={{.To}}">
2017-07-09 01:57:41 +02:00
<meta name="robots" content="noindex">
2017-07-09 03:41:35 +02:00
<h1>Redirecting</h1>
2017-08-10 15:06:53 +02:00
<a href="{{.To}}">Click here if you are not redirected.</a>
<script>location="{{.To}}"</script>
2017-07-09 01:57:41 +02:00
</html>`