-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
104 lines (87 loc) · 2.59 KB
/
main.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
98
99
100
101
102
103
104
package main
import (
"fmt"
"os"
"text/template"
"path/filepath"
"strings"
)
type ElementFunc struct {
FuncName string
TagName string
}
type VoidElementFunc ElementFunc
type AttributeFunc struct {
FuncName string
AttrName string
}
type Params struct {
ElementFuncs []ElementFunc
VoidElementFuncs []VoidElementFunc
AttributeFuncs []AttributeFunc
}
const templDir = "htmlgogen/templates"
// special-cases data-*, doctype
func main() {
templPaths, err := filepath.Glob(filepath.Join(templDir, "*.go"))
check(err)
moreTemplPaths, err := filepath.Glob(filepath.Join(templDir, "/*/*.go"))
check(err)
templPaths = append(templPaths, moreTemplPaths...)
params := NewParams()
for _, templPath := range templPaths {
saveAs, err := filepath.Rel(templDir, templPath)
check(err)
fmt.Printf("Generating %s...\n", saveAs)
templ, err := template.New(filepath.Base(templPath)).
Delims("[[", "]]").
ParseFiles(templPath)
check(err)
err = os.MkdirAll(filepath.Dir(saveAs), 0744)
check(err)
f, err := os.Create(saveAs)
check(err)
defer f.Close()
err = templ.Execute(f, params)
check(err)
}
}
func NewParams() Params {
ps := Params{
[]ElementFunc{},
[]VoidElementFunc{},
[]AttributeFunc{},
}
for _, tag := range tags {
if _, ok := selfClosingTags[tag]; ok {
ps.VoidElementFuncs = append(ps.VoidElementFuncs, VoidElementFunc{
FuncName: GetFuncName(tag),
TagName: tag,
})
} else {
ps.ElementFuncs = append(ps.ElementFuncs, ElementFunc{
FuncName: GetFuncName(tag),
TagName: tag,
})
}
}
for _, attr := range attributes {
ps.AttributeFuncs = append(ps.AttributeFuncs, AttributeFunc{
FuncName: GetFuncName(attr),
AttrName: attr,
})
}
return ps
}
func GetFuncName(s string) string {
parts := strings.Split(s, "-")
for i, p := range parts {
parts[i] = strings.Title(p)
}
return strings.Join(parts, "")
}
func check(err error) {
if err != nil {
panic(err)
}
}