-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_falco.js
69 lines (63 loc) · 1.89 KB
/
dom_falco.js
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
let accum = []
let indent = 0
const writeElement = (h) => {
indent = indent + 2
if (h.tag == 'text') {
accum.push(`Text.raw "${h.data}"\n`)
} else {
accum.push(`Elem.${h.tag}`)
let attrs = []
for (const property in h.attrs) {
let p = property
.trim()
.replace(/class$/, `class'`)
.replace(/default$/, `default'`)
.replace(/for$/, `for'`)
.replace(/open$/, `open'`)
.replace(/type$/, "type'")
const _attr = h.attrs[property]
attrs.push(`Attr.${p} "${_attr}"\n`)
}
if (attrs.length > 0) {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[`)
accum.push(`\n`)
indent = indent + 2
accum.push(' '.repeat(indent))
accum.push(attrs.join(' '.repeat(indent)))
indent = indent - 2
accum.push(' '.repeat(indent))
accum.push(`]\n`)
} else {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[]\n`)
}
if (h.children?.length > 0) {
h.children?.forEach((e) => {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[`)
accum.push(`\n`)
indent = indent + 2
accum.push(' '.repeat(indent))
writeElement(e)
indent = indent - 2
accum.push(' '.repeat(indent))
accum.push(`]\n`)
})
} else {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[]\n`)
}
}
indent = indent - 2
}
export function to_falco(h) {
accum = []
indent = 0
h.forEach((e) => writeElement(e))
return accum.join('')
}