-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathgenerator.go
102 lines (85 loc) · 2.08 KB
/
generator.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"text/template"
)
type Entry struct {
URL string
Author string
Year int
Complaints []string
}
func main() {
entries := []*Entry{}
entryBytes, err := ioutil.ReadFile("entries.json")
if err != nil {
fmt.Println("Error reading entries.json:", err)
fmt.Println(string(entryBytes))
os.Exit(1)
}
err = json.Unmarshal(entryBytes, &entries)
if err != nil {
fmt.Println("Error unmarshalling entries:", err)
fmt.Println(string(entryBytes))
os.Exit(1)
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Year < entries[j].Year })
entryTmpl, err := template.New("entry").Parse(`
# The List
{{ range . }}+ {{ .URL }} ({{ .Author }}, {{ .Year }})
{{ range .Complaints }} - {{ . }}
{{ end }}{{ end }}
`)
if err != nil {
fmt.Println("Error parsing entry template:", err)
os.Exit(1)
}
complaintMap := map[string][]*Entry{}
for _, e := range entries {
for _, c := range e.Complaints {
complaintMap[c] = append(complaintMap[c], e)
}
}
complaintTmpl, err := template.New("complaint").Parse(`
# Reverse complaints index
It's a reverse complaints index, generated by https://github.com/ksimka/go-is-not-good/blob/master/generator.go (thanks to @capoferro)
{{ range $complaint, $entries := . }}+ {{ $complaint }}
{{ range $entries }} - {{ .URL }} ({{ .Author }} {{ .Year }})
{{ end }}{{ end }}
`)
if err != nil {
fmt.Println("Error parsing complaint template:", err)
os.Exit(1)
}
f, err := os.Create("README.md")
if err != nil {
fmt.Println("Error opening README.md:", err)
os.Exit(1)
}
defer f.Close()
err = copyContents("HEADER.md", f)
entryTmpl.Execute(f, entries)
complaintTmpl.Execute(f, complaintMap)
err = copyContents("FOOTER.md", f)
}
func copyContents(origin string, target *os.File) error {
f, err := os.Open(origin)
if err != nil {
fmt.Println("Error opening "+origin+":", err)
os.Exit(1)
}
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return err
}
_, err = target.Write(contents)
if err != nil {
return err
}
return nil
}