-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlc.go
181 lines (158 loc) · 4.58 KB
/
htmlc.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package htmlc
import (
"errors"
"os"
"path/filepath"
"strings"
_ "embed"
regex "github.com/tkdeng/goregex"
"github.com/tkdeng/goutil"
)
//todo: allow dynamic compiling and listening for file changes
//go:embed template.exs
var template []byte
func Compile(src string, out string) error {
if stat, err := os.Stat(src); err != nil || !stat.IsDir() {
return errors.Join(err, os.ErrNotExist, errors.New("src \""+src+"\": directory not found"))
}
if err := os.MkdirAll(filepath.Dir(out), 0755); err != nil {
return err
}
outfile, err := os.OpenFile(out, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer outfile.Close()
outfile.Write(template)
usedRandID := [][]byte{}
if err = compileDir(outfile, src, "", 'r', &usedRandID); err != nil {
return err
}
return nil
}
func compileDir(out *os.File, dir string, name string, dirType byte, usedRandID *[][]byte) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
if path, err := goutil.JoinPath(dir, file.Name()); err == nil {
if dirType == 'r' {
switch file.Name() {
case "layouts", "layout":
if err = compileDir(out, path, "", 'l', usedRandID); err != nil {
return err
}
case "widgets", "widget":
if err = compileDir(out, path, "", 'w', usedRandID); err != nil {
return err
}
case "pages", "page":
if err = compileDir(out, path, "", 'p', usedRandID); err != nil {
return err
}
case "errors", "error":
if err = compileDir(out, path, "", 'e', usedRandID); err != nil {
return err
}
default:
if err = compileDir(out, path, file.Name(), 'd', usedRandID); err != nil {
return err
}
}
} else {
if err = compileDir(out, path, name+"/"+file.Name(), dirType, usedRandID); err != nil {
return err
}
}
}
} else if strings.HasSuffix(file.Name(), ".html") {
if path, err := goutil.JoinPath(dir, file.Name()); err == nil {
if buf, err := os.ReadFile(path); err == nil {
n := name
if n != "" {
n += "/"
}
n += strings.TrimSuffix(file.Name(), ".html")
if dirType == 'l' {
comp := compileExs{buf: &buf}
if err := comp.compile(); err != nil {
return err
}
loadLayout(out, n, &buf, usedRandID)
} else if dirType == 'w' {
comp := compileExs{buf: &buf}
if err := comp.compile(); err != nil {
return err
}
loadWidget(out, n, &buf, usedRandID)
} else if dirType == 'p' || dirType == 'e' {
buf_page := map[string][]byte{}
regex.Comp(`(?s)<(_?@)([\w_-]+)>(.*)</\1\2>\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
name := string(data(2))
if _, ok := buf_page[name]; !ok {
buf_page[name] = data(3)
} else {
buf_page[name] = append(buf_page[name], data(3)...)
}
return []byte{}
})
if len(buf_page) == 0 {
buf_page["body"] = buf
}
for k, b := range buf_page {
comp := compileExs{buf: &b}
if err := comp.compile(); err != nil {
return err
}
buf_page[k] = b
}
loadPage(out, n, &buf_page, usedRandID)
} else {
var buf_layout []byte
buf_page := map[string][]byte{}
buf = regex.Comp(`(?s)(<!DOCTYPE(?:\s+[^>]*|)>|<html(?:\s+[^>]*|)>(.*)</html>)\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
buf_layout = append(buf_layout, data(0)...)
return []byte{}
})
buf = regex.Comp(`(?s)<(_?@)([\w_-]+)>(.*)</\1\2>\r?\n?`).RepFunc(buf, func(data func(int) []byte) []byte {
id := string(data(2))
if _, ok := buf_page[id]; !ok {
buf_page[id] = data(3)
} else {
buf_page[id] = append(buf_page[id], data(3)...)
}
return []byte{}
})
if len(buf_layout) != 0 {
comp := compileExs{buf: &buf_layout}
if err := comp.compile(); err != nil {
return err
}
loadLayout(out, n, &buf_layout, usedRandID)
}
if len(buf_page) != 0 {
for k, b := range buf_page {
comp := compileExs{buf: &b}
if err := comp.compile(); err != nil {
return err
}
buf_page[k] = b
}
loadPage(out, n, &buf_page, usedRandID)
}
if len(buf) != 0 && len(regex.Comp(`(?s)[\s\r\n\t ]+`).RepStrLit(buf, []byte{})) != 0 {
comp := compileExs{buf: &buf}
if err := comp.compile(); err != nil {
return err
}
loadWidget(out, n, &buf, usedRandID)
}
}
}
}
}
}
return nil
}