-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_ml.go
402 lines (328 loc) · 8.66 KB
/
go_ml.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package go_ml
import (
"errors"
"fmt"
"io"
"log/slog"
"strings"
)
var (
ErrWriterNotFound = errors.New("writer not found!")
)
/* HTML element definitions */
type ElementType string
const (
NonVoid ElementType = "non-void"
Void ElementType = "void"
)
type ContentType string
const (
Raw ContentType = "raw-text"
Node ContentType = "node"
)
type HTMLRawContent struct {
text string
}
type HTMLElement struct {
tagName string
attrs []HTMLAttribute
contents []HTMLContent
elType ElementType
}
type HTMLContent struct {
ctType ContentType
child HTMLElement
raw HTMLRawContent
}
// ref: https://github.com/golang/go/issues/62005#issuecomment-1747630201
type NopWritter struct{}
func (NopWritter) Write([]byte) (int, error) { return 0, nil }
func NopLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(new(NopWritter), nil))
}
type buildConfig struct {
stdWriter io.Writer
debug struct {
logger *slog.Logger
}
indentitation struct {
isEnable bool
indentationLevel uint8
}
}
type buildOpt func(config *buildConfig)
func WithWriter(w io.Writer) buildOpt {
return func(config *buildConfig) {
config.stdWriter = w
}
}
// TODO: create logger interface
func WithLogger(logger *slog.Logger) buildOpt {
return func(config *buildConfig) {
config.debug.logger = logger
}
}
func WithDefaultIndentation() buildOpt {
return func(config *buildConfig) {
config.indentitation.indentationLevel = 4
config.indentitation.isEnable = true
}
}
// Choose a indentation level between 0 and 255.
func WithIndentation(level int) buildOpt {
return func(config *buildConfig) {
config.indentitation.indentationLevel = uint8(level)
config.indentitation.isEnable = true
}
}
func (ele HTMLElement) BuildDOM(opts ...buildOpt) error {
return buildDOM(ele, opts...)
}
func (ct HTMLContent) BuildDOM(opts ...buildOpt) error {
return buildDOM(ct.child, opts...)
}
func buildDOM(ele HTMLElement, opts ...buildOpt) error {
var totalWritten int
defaultCfg := new(buildConfig)
defaultCfg.debug.logger = NopLogger()
for _, op := range opts {
op(defaultCfg)
}
if defaultCfg.stdWriter == nil {
return ErrWriterNotFound
}
// hardcoded html document compliance
if ele.tagName == "html" {
totalWritten, _ = defaultCfg.stdWriter.Write([]byte("<!DOCTYPE html>\n"))
}
n, err := defaultCfg.parseElement(ele, 1)
if err != nil {
return err
}
totalWritten += n
defaultCfg.debug.logger.Debug("Element was written with: ",
"tag_name", ele.tagName, "bytes", totalWritten)
return nil
}
func (cfg *buildConfig) parseElement(ele HTMLElement, tagDepth int) (int, error) {
var attrStr string
var rIndentStr, lIndentStr string
var attrKeys []string
var totalWritten int
writeOrErr := func(s string) error {
n, err := cfg.stdWriter.Write([]byte(s))
totalWritten += n
return err
}
putNChar := func(s string, ch string, n int) string {
for i := 0; i < n; i++ {
s += ch
}
return s
}
// TODO: fix indentatin bug!
if cfg.indentitation.isEnable {
rIndentStr = putNChar("\n", " ", tagDepth*int(cfg.indentitation.indentationLevel))
lIndentStr = putNChar("\n", " ", (tagDepth-1)*int(cfg.indentitation.indentationLevel))
}
// rules:
// 1. we need to merge all attributes with the same name
// 2. if element has no attrs, the space become a suffix and will be removed
attrMap := make(map[string]HTMLAttribute)
for _, attr := range ele.attrs {
if curAttr, ok := attrMap[attr.name]; ok {
curAttr.values = append(curAttr.values, attr.values...)
attrMap[attr.name] = curAttr
} else {
attrKeys = append(attrKeys, attr.name)
attrMap[attr.name] = attr
}
}
// TODO: find another aproach to have all the parsed attributes in O(n)
for _, k := range attrKeys {
attrStr += " " + attrMap[k].String()
}
attrStr = strings.TrimSuffix(attrStr, " ")
switch ele.elType {
// void -> <[tag][?attrs]/>
case Void:
if err := writeOrErr(fmt.Sprintf(`<%s%s/>`, ele.tagName, attrStr)); err != nil {
return totalWritten, err
}
return totalWritten, nil
// non-void -> <[tag][?attrs]>[content]</[tag]>
default:
if err := writeOrErr("<" + ele.tagName + attrStr + ">"); err != nil {
return totalWritten, err
}
if len(ele.contents) > 0 {
_ = writeOrErr(rIndentStr)
}
// threat as element node or just an raw text
for _, ct := range ele.contents {
switch ct.ctType {
case Node:
n, err := cfg.parseElement(ct.child, tagDepth+1)
if err != nil {
return totalWritten, err
}
totalWritten += n
case Raw:
if err := writeOrErr(ct.raw.text); err != nil {
return totalWritten, err
}
default:
return totalWritten, fmt.Errorf("not recognized content type: [%s]", ct.ctType)
}
}
if len(ele.contents) > 0 {
_ = writeOrErr(lIndentStr)
}
if err := writeOrErr("</" + ele.tagName + ">"); err != nil {
return totalWritten, err
}
return totalWritten, nil
}
}
/*
HTML attribute definitions
Ref: https://www.w3.org/TR/2012/WD-html-markup-20120329/syntax.html#syntax-attributes
*/
type AttributeType string
const (
None AttributeType = "none"
Single AttributeType = "single"
DoubleQuoted AttributeType = "double-quoted"
)
type HTMLAttribute struct {
name string
// space delimited values
values []string
attrType AttributeType
}
func (attr HTMLAttribute) String() string {
switch attr.attrType {
case DoubleQuoted:
var st string
for _, v := range attr.values {
st += v + " "
}
return fmt.Sprintf(`%s="%s"`, attr.name, strings.TrimSuffix(st, " "))
case Single:
return attr.name
case None:
return ""
default:
return ""
}
}
/* Attributes functions declarations */
func Attr(name string, attrType AttributeType, values ...string) HTMLAttribute {
return HTMLAttribute{name: name, values: values, attrType: attrType}
}
func ClassNames(values ...string) HTMLAttribute {
return Attr("class", DoubleQuoted, values...)
}
func PlaceHolder(value string) HTMLAttribute {
return Attr("placeholder", DoubleQuoted, value)
}
func Id(values ...string) HTMLAttribute {
return Attr("id", DoubleQuoted, values...)
}
func Name(values ...string) HTMLAttribute {
return Attr("name", DoubleQuoted, values...)
}
func Lang(values ...string) HTMLAttribute {
return Attr("lang", DoubleQuoted, values...)
}
func Type(values ...string) HTMLAttribute {
return Attr("type", DoubleQuoted, values...)
}
func Value(values ...string) HTMLAttribute {
return Attr("value", DoubleQuoted, values...)
}
func Src(values ...string) HTMLAttribute {
return Attr("src", DoubleQuoted, values...)
}
func Defer() HTMLAttribute {
return Attr("defer", Single)
}
func Checked() HTMLAttribute {
return Attr("checked", Single)
}
func Required() HTMLAttribute {
return Attr("required", Single)
}
func Action(values ...string) HTMLAttribute {
return Attr("action", Single, values...)
}
func Method(values ...string) HTMLAttribute {
return Attr("method", Single, values...)
}
/* Attributes utils */
func IsChecked(check bool) (attr HTMLAttribute) {
if check {
return Checked()
}
return HTMLAttribute{attrType: None}
}
/* Tags functions declarations */
type tagClosure func(contents ...HTMLContent) HTMLContent
func Tag(tagName string, elType ElementType, attrs ...HTMLAttribute) tagClosure {
return func(contents ...HTMLContent) HTMLContent {
return HTMLContent{
child: HTMLElement{
tagName: tagName,
contents: contents,
attrs: attrs,
elType: elType,
},
ctType: Node,
}
}
}
func RawText(text string) HTMLContent {
return HTMLContent{raw: HTMLRawContent{text: text}, ctType: Raw}
}
func Input(attrs ...HTMLAttribute) HTMLContent {
return Tag("input", Void, attrs...)()
}
func Button(attrs ...HTMLAttribute) tagClosure {
return Tag("button", NonVoid, attrs...)
}
func Script(attrs ...HTMLAttribute) tagClosure {
return Tag("script", NonVoid, attrs...)
}
func Title(attrs ...HTMLAttribute) tagClosure {
return Tag("title", NonVoid, attrs...)
}
func Head(attrs ...HTMLAttribute) tagClosure {
return Tag("head", NonVoid, attrs...)
}
func Div(attrs ...HTMLAttribute) tagClosure {
return Tag("div", NonVoid, attrs...)
}
func Body(attrs ...HTMLAttribute) tagClosure {
return Tag("body", NonVoid, attrs...)
}
func Html(attrs ...HTMLAttribute) tagClosure {
return Tag("html", NonVoid, attrs...)
}
func Form(attrs ...HTMLAttribute) tagClosure {
return Tag("form", NonVoid, attrs...)
}
func Label(attrs ...HTMLAttribute) tagClosure {
return Tag("label", NonVoid, attrs...)
}
func Table(attrs ...HTMLAttribute) tagClosure {
return Tag("table", NonVoid, attrs...)
}
func Th(attrs ...HTMLAttribute) tagClosure {
return Tag("th", NonVoid, attrs...)
}
func Tr(attrs ...HTMLAttribute) tagClosure {
return Tag("tr", NonVoid, attrs...)
}
func Td(attrs ...HTMLAttribute) tagClosure {
return Tag("td", NonVoid, attrs...)
}