-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
gen.go
50 lines (41 loc) · 1.01 KB
/
gen.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
//go:build go_run_only
// +build go_run_only
package main
import (
"bytes"
"os"
"os/exec"
"strings"
"text/template"
"zgo.at/zli"
)
func main() {
os.Setenv("README", "yes")
tpl := template.New("").Funcs(template.FuncMap{
"trim": func(n int, lines string) string {
s := strings.SplitN(lines, "\n", n+2)
return strings.Join(s[:len(s)-1], "\n") + "\n …"
},
"example": func(args ...string) (string, error) {
o, err := exec.Command("uni", args...).CombinedOutput()
if err != nil {
return "", err
}
for i := range args {
if strings.Index(args[i], " ") > -1 {
args[i] = "'" + args[i] + "'"
}
}
out := " % uni " + strings.Join(args, " ") + "\n"
for _, line := range bytes.Split(bytes.TrimRight(o, "\n"), []byte{'\n'}) {
out += " " + string(line) + "\n"
}
return out[:len(out)-1], nil
},
})
tpl, err := tpl.ParseFiles(".readme.gotxt")
zli.F(err)
fp, err := os.Create("README.md")
zli.F(err)
zli.F(tpl.ExecuteTemplate(fp, ".readme.gotxt", nil))
}