-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-readme.go
75 lines (61 loc) · 1.51 KB
/
gen-readme.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
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"text/template"
)
const (
templateFile = "_README.template"
htmlNewline = "</br>"
)
// Generate table in README from profiles in .md and a template
// Usage:
// go run gen-readme.go *.md > README.md
type person struct {
Name string
File string
Period string
}
var (
reName = regexp.MustCompile("name:([^\n]+)")
rePeriod = regexp.MustCompile("period:([^\n]+)")
)
func main() {
var people []*person
fmt.Fprintf(os.Stderr, "processing %d files\n", len(os.Args)-1)
for _, file := range os.Args[1:] {
fmt.Fprintf(os.Stderr, "\tfile %s\n", file)
person, err := readFrom(file)
check(err, "failed to read person file "+file)
people = append(people, person)
}
sort.SliceStable(people, func(i, j int) bool {
return people[i].Period < people[j].Period
})
t, err := template.ParseFiles(templateFile)
check(err, "failed to read template file")
t.Execute(os.Stdout, people)
}
// readFrom reads .period and .name from a given file.
func readFrom(file string) (*person, error) {
p := &person{File: file}
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
name := string(reName.FindSubmatch(data)[1])
period := string(rePeriod.FindSubmatch(data)[1])
p.Name = strings.TrimSpace(strings.TrimSuffix(name, htmlNewline))
p.Period = strings.TrimSpace(strings.TrimSuffix(period, htmlNewline))
return p, nil
}
func check(err error, msg string) {
if err != nil {
fmt.Fprintf(os.Stderr, msg, err)
os.Exit(1)
}
}