-
Notifications
You must be signed in to change notification settings - Fork 0
/
mooncake.fnl
165 lines (146 loc) · 4.51 KB
/
mooncake.fnl
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
;; mutual recursive helpers
(local rec {})
(fn b [str] (str:byte))
(local tag-delims {(b ".") :class (b "#") :id})
;; taken from https://developer.mozilla.org/en-US/docs/Glossary/Void_element
;; fnlfmt: skip
(local void-elements {
:area true
:base true
:br true
:col true
:embed true
:hr true
:img true
:input true
:link true
:meta true
:param true
:source true
:track true
:wbr true
})
(fn string? [v]
(= (type v) "string"))
(fn table? [v]
(= (type v) "table"))
;; TODO: remove
;; fnlfmt: skip
(fn page [title]
[:<>
[:h1 title]
[:p "this is the body"]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:div
[:p.anotherclass {
:class "h-3"
}]
[:p "this is also nested"]]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:p :more]
[:p :more]])
;; fnlfmt: skip
(local escape-chars {
"<" "<"
">" ">"
"&" "&"
"'" "'"
"\"" """
})
(fn escape-string [str]
(let [cur []]
(do
(for [i 1 (# str)]
(let [ch (str:sub i i )]
(table.insert cur (or (. escape-chars ch) ch))))
(table.concat cur ""))))
;; TODO: something more robust?
;; attr table should be length 0, compared to a markup table
(fn attr-table? [tab]
(and tab (= 0 (# tab))))
;; need to reference function in mod because of mutual recursion
(fn render-element [tag-data all start]
(let [cur []
{:tag raw-tag :attr attr} tag-data
frag? (or (= raw-tag "<>") (= raw-tag "*"))
void-el? (. void-elements raw-tag)]
(do
(when (= "html" raw-tag)
(table.insert cur "<!DOCTYPE html"))
(when (not frag?)
(do
(var open-tag (.. "<" raw-tag))
(each [k v (pairs attr)]
(if (table? v)
(set open-tag (.. open-tag " " k "=\"" (table.concat v " ") "\""))
(set open-tag (.. open-tag " " k "=\"" (tostring v) "\""))))
(set open-tag (.. open-tag ">"))
(table.insert cur open-tag)))
(when (not void-el?)
(for [i start (# all)]
(let [el (. all i)
children (if (string? el)
[(escape-string el)]
(table? el)
(rec.render-markup- el)
[(tostring el)])]
(each [_ child (pairs children)]
(table.insert cur (.. "\t" child))))))
;; insert closing tag
(when (and (not frag?) (not void-el?))
(table.insert cur (.. "</" raw-tag ">")))
cur)))
(fn add-class-tag [attr fld]
(let [clname (fld:sub 2)
class-val (. attr :class)]
(do
(if (not class-val)
(tset attr :class [])
(not (= (type class-val) "table"))
(tset attr :class [class-val]))
(table.insert (. attr :class) clname))))
(fn set-id-tag [attr fld]
(let [idname (fld:sub 2)]
(tset attr :id idname)))
(fn add-field [tbl fld]
(let [fld-symbol (fld:byte 1)
fld-type (. tag-delims fld-symbol)
attr (. tbl :attr)]
(case fld-type
:class (add-class-tag attr fld)
:id (set-id-tag attr fld)
_ (tset tbl :tag fld))))
(fn to-tag-data [tag attr]
(do
(let [parts (string.gmatch tag "[\\.#]?[^\\.#]+")
res {: attr}]
;; loop through the iterator, adding all parts to the res table
(while (let [fld (parts)]
(when fld
(add-field res fld)
fld)))
(if (not (. res :tag))
(add-field res "div"))
res)))
(fn render-markup- [{1 tag 2 maybe-attr &as all}]
(let [has-attr-table? (attr-table? maybe-attr)
body-start (if has-attr-table? 3 2)
attr (if has-attr-table? maybe-attr {})
tag-data (to-tag-data tag attr)]
(rec.render-element tag-data all body-start)))
(fn render-markup [body]
(table.concat (render-markup- body) "\n"))
(tset rec :render-markup- render-markup-)
(tset rec :render-element render-element)
;; TODO: remove test page
(local mod {: page : render-markup })
mod