2017-07-09 01:57:41 +02:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-07-09 03:41:35 +02:00
|
|
|
"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{})
|
2017-07-09 07:22:32 +02:00
|
|
|
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-02 19:57:28 +02:00
|
|
|
func (p jekyllRedirectFromPlugin) PostReadSite(site Site) error {
|
2017-07-10 21:28:07 +02:00
|
|
|
ps := site.Pages()
|
|
|
|
newPages, err := p.processRedirectFrom(site, ps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-10 21:39:17 +02:00
|
|
|
if err := p.processRedirectTo(site, ps); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-10 21:28:07 +02:00
|
|
|
for _, r := range newPages {
|
|
|
|
site.AddDocument(r, true)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p jekyllRedirectFromPlugin) processRedirectFrom(site Site, ps []pages.Page) ([]pages.Document, error) {
|
|
|
|
var (
|
|
|
|
cfg = site.Config()
|
|
|
|
siteurl = cfg.AbsoluteURL
|
|
|
|
baseurl = cfg.BaseURL
|
|
|
|
prefix = siteurl + baseurl
|
|
|
|
redirections = []pages.Document{}
|
|
|
|
)
|
|
|
|
addRedirectFrom := func(from string, to pages.Page) {
|
2017-09-02 19:53:50 +02:00
|
|
|
r := redirectionDoc{pages.PageEmbed{Path: from}, prefix + to.URL()}
|
2017-07-10 21:28:07 +02:00
|
|
|
redirections = append(redirections, &r)
|
|
|
|
}
|
|
|
|
for _, p := range ps {
|
|
|
|
sources, err := getStringArray(p, "redirect_from")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-09 01:57:41 +02:00
|
|
|
}
|
2017-07-10 21:28:07 +02:00
|
|
|
for _, from := range sources {
|
|
|
|
addRedirectFrom(from, p)
|
2017-07-09 01:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-10 21:28:07 +02:00
|
|
|
return redirections, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p jekyllRedirectFromPlugin) processRedirectTo(site Site, ps []pages.Page) error {
|
|
|
|
for _, p := range ps {
|
|
|
|
sources, err := getStringArray(p, "redirect_to")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(sources) > 0 {
|
2017-09-02 19:53:50 +02:00
|
|
|
r := redirectionDoc{pages.PageEmbed{Path: p.URL()}, sources[0]}
|
2017-07-10 21:28:07 +02:00
|
|
|
p.SetContent(r.Content())
|
|
|
|
}
|
2017-07-09 01:57:41 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-10 21:28:07 +02:00
|
|
|
func getStringArray(p pages.Page, fieldName string) (out []string, err error) {
|
|
|
|
if value, ok := p.FrontMatter()[fieldName]; ok {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case []string:
|
|
|
|
out = value
|
|
|
|
case []interface{}:
|
|
|
|
out = make([]string, len(value))
|
|
|
|
for i, item := range value {
|
|
|
|
out[i] = fmt.Sprintf("%s", item)
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
out = []string{value}
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unimplemented redirect_from type %T", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:22:32 +02:00
|
|
|
type redirectionDoc struct {
|
2017-07-25 17:24:37 +02:00
|
|
|
pages.PageEmbed
|
|
|
|
To string
|
2017-07-09 07:22:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:06:53 +02:00
|
|
|
func (d *redirectionDoc) Content() string {
|
2017-07-09 07:22:32 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := redirectTemplate.Execute(buf, d); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-08-10 15:06:53 +02:00
|
|
|
return buf.String()
|
2017-07-09 07:22:32 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:23:51 +02:00
|
|
|
func (d *redirectionDoc) Write(w io.Writer) error {
|
2017-07-09 07:22:32 +02:00
|
|
|
return redirectTemplate.Execute(w, d)
|
|
|
|
}
|
|
|
|
|
2017-07-09 01:57:41 +02:00
|
|
|
// Adapted from https://github.com/jekyll/jekyll-redirect-from
|
2017-07-09 07:22:32 +02:00
|
|
|
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>`
|