-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
97 lines (79 loc) · 1.79 KB
/
build.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
package main
import (
"fmt"
"html/template"
"log"
"os"
"path"
"strings"
)
type vanityTemplateData struct {
Root string
VCS string
RepoURL string
}
// Build the pages
func build(config *Config) error {
t, err := template.New("").Parse(`{{define "T"}}` + config.RepoTemplate + "{{end}}")
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
for root, repo := range config.Repos {
err := os.MkdirAll(path.Join(wd, config.Output, root), os.ModePerm)
if err != nil {
return err
}
dest := path.Join(wd, config.Output, root, "index.html")
log.Printf("%s %s\n", info("Building ["+root+"]"), dest)
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
data := vanityTemplateData{
Root: path.Join(config.Domain, root),
VCS: repo.VCS,
RepoURL: repo.URL,
}
err = t.ExecuteTemplate(f, "T", data)
if err != nil {
return err
}
}
return nil
}
// Build the index page
func buildIndex(config *Config) error {
funcMap := template.FuncMap{
"unescape": func(s string) template.HTML {
return template.HTML(s)
},
"replace": func(input, from, to string) string {
return strings.Replace(input, from, to, -1)
}}
t, err := template.New("").Funcs(funcMap).Parse(`{{define "T"}}` + config.IndexTemplate + "{{end}}")
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
dest := path.Join(wd, config.Output, "index.html")
log.Printf("%s %s\n", info("Building [index]"), dest)
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
err = t.ExecuteTemplate(f, "T", config)
if err != nil {
return err
}
return nil
}
func info(txt string) string {
return fmt.Sprintf("\033[0;34m%s\033[m", txt)
}