-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractReadme.go
71 lines (59 loc) · 1.09 KB
/
extractReadme.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
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
)
const tpl = `Snippets of Go code for Sublime Text
====================================
{{ range . }}
{{ .TabTrigger }}:
` + "```go" + `
{{ .Content }}
` + "```" + `
{{ end }}
`
type snippet struct {
XMLName xml.Name `xml:"snippet"`
Content string `xml:"content"`
TabTrigger string `xml:"tabTrigger"`
}
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
var snippets []*snippet
for _, f := range files {
if strings.HasSuffix(f.Name(), ".sublime-snippet") {
snippets = append(snippets, extractCode(f.Name()))
}
}
w, err := os.Create("README.md")
if err != nil {
log.Fatal(err)
}
tmpl, err := template.New("doc").Parse(tpl)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(w, snippets)
if err != nil {
log.Fatal(err)
}
}
func extractCode(fn string) *snippet {
r, err := os.Open(fn)
if err != nil {
log.Fatal(err)
}
defer r.Close()
var data *snippet
if err := xml.NewDecoder(r).Decode(&data); err != nil {
log.Fatal(err)
}
return data
}