-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (130 loc) · 3.24 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
htmltemplate "text/template"
"time"
"github.com/abdheshnayak/gohtmlx/pkg/element"
"github.com/abdheshnayak/gohtmlx/pkg/gocode"
"github.com/abdheshnayak/gohtmlx/pkg/utils"
"sigs.k8s.io/yaml"
)
func main() {
src := flag.String("src", "", "source directory")
dist := flag.String("dist", "", "destination directory")
flag.Parse()
if src == nil || dist == nil {
flag.PrintDefaults()
return
}
if *src == "" || *dist == "" {
flag.PrintDefaults()
return
}
if err := Run(*src, *dist); err != nil {
utils.Log.Error("transpiling failed", "err", err)
}
}
func Run(src, dist string) error {
utils.Log.Info("transpiling...")
t := time.Now()
defer func(t time.Time) {
utils.Log.Info(fmt.Sprintf("transpiled in %s", time.Since(t)))
}(t)
input, err := utils.WalkAndConcatenateHTML(src)
if err != nil {
return err
}
tmpl, err := template.New("global").Delims("<!-- *", " -->").Parse(string(input))
if err != nil {
return err
}
gsections, err := utils.ParseSections(tmpl)
if err != nil {
return err
}
imports := []string{}
if imps, ok := gsections["imports"]; ok {
imps := strings.Split(strings.TrimSpace(imps), "\n")
for _, v := range imps {
imports = append(imports, strings.TrimSpace(v))
}
}
// Parse the template
tmpl, err = template.New("sections").Delims("<!-- +", " -->").Parse(string(input))
if err != nil {
return err
}
sections, err := utils.ParseSections(tmpl)
if err != nil {
return err
}
goCodes := map[string]string{}
components := map[string]element.CompInfo{}
for k := range sections {
components[strings.ToLower(k)] = element.CompInfo{
Name: k,
Props: map[string]string{},
}
}
structs := []string{}
for name, content := range sections {
hparser := htmltemplate.New("sections").Delims("<!-- |", " -->")
tpl, err := hparser.Parse(string(content))
m, err := utils.ParseSections(tpl)
if err != nil {
return err
}
var propsMap map[string]string
if props, ok := m["props"]; ok {
if err := yaml.Unmarshal([]byte(props), &propsMap); err != nil {
return err
}
if _, ok := components[name]; !ok {
for k := range propsMap {
components[strings.ToLower(name)].Props[strings.ToLower(k)] = k
}
}
structs = append(structs, gocode.ConstructStruct(propsMap, name))
} else {
structs = append(structs, gocode.ConstructStruct(map[string]string{}, name))
}
}
// Output the parsed map
for name, content := range sections {
hparser := htmltemplate.New("section-data").Delims("<!-- |", "-->")
tpl, err := hparser.Parse(string(content))
m, err := utils.ParseSections(tpl)
if err != nil {
return err
}
if html, ok := m["html"]; ok {
h, err := element.NewHtml([]byte(html))
if err != nil {
return err
}
out, err := h.RenderGolangCode(components)
if err != nil {
return err
}
goCodes[name] = out
}
}
b, err := gocode.ConstructSource(goCodes, structs, imports)
if err != nil {
return err
}
outPath := path.Join(dist, "gohtmlxc", "comp_generated.go")
if err := os.MkdirAll(filepath.Dir(outPath), 0755); err != nil {
return err
}
if err := os.WriteFile(outPath, []byte(b), 0644); err != nil {
return err
}
return nil
}