-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
62 lines (54 loc) · 1.18 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
package main
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"os"
"strings"
)
func main() {
ret := 0
err := run()
if err != nil {
fmt.Println(err.Error())
ret = 1
}
os.Exit(ret)
}
func run() error {
sectionData, err := ioutil.ReadFile("sections/_index")
if err != nil {
return fmt.Errorf("Cannot read sections: %s", err)
}
lines := strings.Split(string(sectionData), "\n")
secs := make([]string, 0)
for _, l := range lines {
if l != "" {
secs = append(secs, l)
}
}
layout, err := ioutil.ReadFile("layout")
if err != nil {
return fmt.Errorf("Cannot read layouts: %s", err)
}
t, err := template.New("cheatsheet").Parse(string(layout))
if err != nil {
return fmt.Errorf("Cannot parse layout template: %s", err)
}
buff := bytes.NewBuffer([]byte{})
err = t.Execute(buff, secs)
if err != nil {
return fmt.Errorf("Cannot execute layout template: %s", err)
}
html := buff.String()
for _, s := range secs {
content, err := ioutil.ReadFile("sections/" + s)
if err != nil {
return fmt.Errorf("Cannot replace text content for %s: %s", s, err)
}
html = strings.Replace(html, "[[ "+s+" ]]", string(content), 1)
}
fmt.Println(html)
return nil
}