-
Notifications
You must be signed in to change notification settings - Fork 14
/
redirect_from.go
133 lines (119 loc) · 3.42 KB
/
redirect_from.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package plugins
import (
"bytes"
"fmt"
"io"
"text/template"
"github.com/osteele/gojekyll/pages"
)
type jekyllRedirectFromPlugin struct{ plugin }
var redirectTemplate *template.Template
func init() {
register("jekyll-redirect-from", jekyllRedirectFromPlugin{})
tmpl, err := template.New("redirect_from").Parse(redirectFromTemplateSource)
if err != nil {
panic(err)
}
redirectTemplate = tmpl
}
func (p jekyllRedirectFromPlugin) PostRead(site Site) error {
ps := site.Pages()
newPages, err := p.processRedirectFrom(site, ps)
if err != nil {
return err
}
if err := p.processRedirectTo(site, ps); err != nil {
return err
}
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) {
r := redirectionDoc{From: from, To: prefix + to.Permalink()}
redirections = append(redirections, &r)
}
for _, p := range ps {
sources, err := getStringArray(p, "redirect_from")
if err != nil {
return nil, err
}
for _, from := range sources {
addRedirectFrom(from, p)
}
}
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 {
r := redirectionDoc{From: p.Permalink(), To: sources[0]}
p.SetContent(r.Content())
}
}
return nil
}
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
}
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) error {
return redirectTemplate.Execute(w, d)
}
// Adapted from https://github.com/jekyll/jekyll-redirect-from
const redirectFromTemplateSource = `<!DOCTYPE html>
<html lang="en-US">
<meta charset="utf-8">
<title>Redirecting…</title>
<link rel="canonical" href="{{ .To }}">
<meta http-equiv="refresh" content="0; url={{ .To }}">
<meta name="robots" content="noindex">
<h1>Redirecting…</h1>
<a href="{{ .To }}">Click here if you are not redirected.</a>
<script>location="{{ .To }}"</script>
</html>`