-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
285 lines (237 loc) · 6.67 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"
"golang.org/x/tools/present"
)
//go:generate go run main.go
const (
// talksPrefix is the base URL for the talks.godoc.org website with this
// repository's name prefixed.
talksPrefix = "https://go-talks.appspot.com/github.com/mdlayher/talks/"
// repoPrefix is the base URL for resources whose links are hosted in
// this repository.
repoPrefix = "https://github.com/mdlayher/talks/blob/master/"
// talksJSON is the name of the JSON metadata file produced by this script.
talksJSON = "talks.json"
)
func main() {
base, err := url.Parse(talksPrefix)
if err != nil {
log.Fatalf("failed to parse base URL: %v", err)
}
// Look for all presentations in '.slide' format.
var ps []*presentation
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
var fn func(path string, base *url.URL) (*presentation, error)
switch filepath.Ext(path) {
case ".json":
// Skip special metadata file.
if path == talksJSON {
return nil
}
fn = parseJSON
case ".slide":
// Don't parse slides if a JSON file exists instead.
noExt := strings.TrimSuffix(path, filepath.Ext(path))
if _, err := os.Stat(noExt + ".json"); err == nil {
return nil
}
fn = parsePresentation
default:
return nil
}
p, err := fn(path, base)
if err != nil {
return err
}
// Ensure valid resources.
for i, r := range p.Resources {
switch r.Kind {
case audio, blog, slides:
default:
log.Fatalf("unexpected resource kind for %q: %q", p.Time, r.Kind)
}
if r.Link == "" {
log.Fatalf("empty resource link for %q, kind %q", p.Title, r.Kind)
}
// Post-processing for URLs relative to this repository.
link, err := resolveLink(repoPrefix, r.Link)
if err != nil {
log.Fatalf("failed to resolve resource link for %q: %v", p.Title, err)
}
p.Resources[i].Link = link
}
// Create a README for individual presentations, placing it at the same
// directory level as the presentation metadata.
readme, err := os.Create(filepath.Join(filepath.Dir(path), "README.md"))
if err != nil {
log.Fatalf("failed to create directory README: %v", err)
}
defer readme.Close()
if err := directory.Execute(readme, buildInput(p)); err != nil {
log.Fatalf("failed to execute directory template: %v", err)
}
ps = append(ps, p)
return nil
})
if err != nil {
log.Fatalf("unexpected error during filesystem walk: %v", err)
}
// Order all presentations by latest date and time, and then output the template.
sort.Slice(ps, func(i int, j int) bool {
return ps[i].Time.After(ps[j].Time)
})
// Generate top-level README.md.
readme, err := os.Create("README.md")
if err != nil {
log.Fatalf("failed to create index README: %v", err)
}
defer readme.Close()
// Render the presentations in an input format suitable for the README
// markdown template.
inputs := make([]input, 0, len(ps))
for _, p := range ps {
inputs = append(inputs, buildInput(p))
}
if err := index.Execute(readme, inputs); err != nil {
log.Fatalf("failed to execute index template: %v", err)
}
// Generate talks.json metadata.
talks, err := os.Create(talksJSON)
if err != nil {
log.Fatalf("failed to create talks.json: %v", err)
}
defer talks.Close()
if err := json.NewEncoder(talks).Encode(ps); err != nil {
log.Fatalf("failed to encode JSON: %v", err)
}
}
func parseJSON(path string, base *url.URL) (*presentation, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var p presentation
if err := json.NewDecoder(f).Decode(&p); err != nil {
return nil, err
}
return &p, nil
}
func parsePresentation(path string, base *url.URL) (*presentation, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
// Parse a presentation to retrieve its metadata.
doc, err := present.Parse(f, path, 0)
if err != nil {
return nil, err
}
u, err := url.Parse(path)
if err != nil {
return nil, err
}
// Build a presentation to be output as part of the template.
return &presentation{
Title: doc.Title,
Description: doc.Subtitle,
Time: doc.Time,
Resources: []resource{{
Kind: slides,
Link: base.ResolveReference(u).String(),
}},
}, nil
}
// A presentation is a presentation's metadata.
type presentation struct {
Title string
Venue string
Description string
Time time.Time
VideoLink string
Resources []resource
}
// A resource is a type of external content resource.
type resource struct {
Kind kind
Link string
}
// A kind is a resource type.
//
// Video is explicitly not a kind because it is formatted differently in output.
type kind string
// Known kind types. Any unrecognized type will result in an error.
const (
audio kind = "audio"
blog kind = "blog"
slides kind = "slides"
)
// An input is an input for the README templates.
type input struct {
Title string
Venue string
Description string
VideoLink string
ResourcesList string
}
// buildInput turns a presentation into template input.
func buildInput(p *presentation) input {
// Generate a markdown-formatted string of resource links.
ss := make([]string, 0, len(p.Resources))
for _, r := range p.Resources {
ss = append(ss, fmt.Sprintf("[%s](%s)", r.Kind, r.Link))
}
return input{
Title: p.Title,
Venue: p.Venue,
Description: p.Description,
VideoLink: p.VideoLink,
ResourcesList: strings.Join(ss, ", "),
}
}
func resolveLink(prefix, rel string) (string, error) {
pu, err := url.Parse(prefix)
if err != nil {
return "", err
}
relu, err := url.Parse(rel)
if err != nil {
return "", err
}
return pu.ResolveReference(relu).String(), nil
}
// index is the markdown template for the top-level README.md.
var index = template.Must(template.New("index.md").Parse(strings.TrimSpace(`
# talks [![Build Status](https://travis-ci.org/mdlayher/talks.svg?branch=master)](https://travis-ci.org/mdlayher/talks)
Talks by Matt Layher. MIT Licensed.
## Talks
{{range .}}
- {{if .VideoLink}}[{{.Title}}]({{.VideoLink}}){{else}}{{.Title}}{{end}}{{if .Venue}} ({{.Venue}}){{end}}{{if .Description}}
- {{.Description}}{{end}}{{if .ResourcesList}}
- {{.ResourcesList}}{{end}}{{end}}
`)))
// directory is the markdown template for individual directory README.md files.
var directory = template.Must(template.New("directory.md").Parse(strings.TrimSpace(`
# {{if .VideoLink}}[{{.Title}}]({{.VideoLink}}){{else}}{{.Title}}{{end}}{{if .Venue}} ({{.Venue}}){{end}}
{{.Description}}
{{if .ResourcesList}}
- {{.ResourcesList}}
{{end}}
`)))