-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHTML.tla
208 lines (174 loc) · 6.28 KB
/
HTML.tla
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
-------------------------------- MODULE HTML --------------------------------
LOCAL INSTANCE Sequences
LOCAL INSTANCE SequencesExt
\***** Helpers
HTMLFill(string, args) ==
(***********************************************************************)
(* Do the sequence of replaces in args to the string. *)
(* The args is a sequence of tuples: <<substr,newsubstr>>. *)
(* Example: *)
(* HTMLFill("test %num%", << <<"%num%", "12">> >>) = "test 12" *)
(***********************************************************************)
LET f(str, x) == ReplaceFirstSubSeq(x[2], x[1], str)
IN FoldLeft(f, string, args)
LOCAL StringSeqToString(string_seq, separator) ==
(***********************************************************************)
(* Concatenates sequence of strings into one string. *)
(* The `seperator` is put between the strings in the sequence. *)
(* Example: *)
(* StringSeqToString(<<"Test","string!">>, "-") = "Test-string!" *)
(***********************************************************************)
LET f(str1,str2) == IF str1 = "" THEN str2 ELSE str1 \o separator \o str2
IN FoldLeft(f, "", string_seq)
\***** Main Document
HTMLDocument(header, body, list_of_scripts) ==
StringSeqToString(<<
"<!DOCTYPE html>",
"<html>",
header,
body,
StringSeqToString(list_of_scripts,"\n"),
"</html>"
>>, "\n")
\***** Style Sheet
HTMLClass(classname, attribute_list) ==
StringSeqToString(<<
ReplaceFirstSubSeq(classname, "%classname%", "%classname% {"),
StringSeqToString(attribute_list,"\n"),
"}"
>>, "\n")
HTMLAttribute(name, value) ==
HTMLFill("%name%: %value%;", <<
<<"%name%", name>>,
<<"%value%", value>>
>>)
HTMLDefaultStyle ==
StringSeqToString(<<
HTMLClass(".default_grid",<<
HTMLAttribute("display", "grid")
>>),
HTMLClass(".default_box",<<
HTMLAttribute("border", "1px solid black")
>>),
HTMLClass(".default_circle",<<
HTMLAttribute("border-radius", "50%")
>>)
>>, "\n")
HTMLStyleSheet(class_list) ==
StringSeqToString(<<
"<style type=\"text/css\">",
HTMLDefaultStyle,
StringSeqToString(class_list,"\n"),
"</style>"
>>, "\n")
\***** Script
HTMLScriptFile(name) ==
HTMLFill("<script src=\"%name%\"></script>", << <<"%name%", name>>>> )
HTMLScript(children) ==
StringSeqToString(<<
"<script>",
StringSeqToString(children, "\n"),
"</script>"
>>, "\n")
\***** Header
HTMLLink(name, type) ==
HTMLFill("<link href=\"%name%\" rel=\"%type%\"/>",
<< <<"%name%", name>>, <<"%type%", type>> >> )
HTMLHeader(title, list_of_links, class_list) ==
StringSeqToString(<<
"<head>",
"<meta charset=\"UTF-8\">",
ReplaceFirstSubSeq(title, "%title%", "<title>%title%</title>"),
StringSeqToString(list_of_links,"\n"),
HTMLStyleSheet(class_list),
"</head>"
>>, "\n")
\***** Body
HTMLBody(body) ==
StringSeqToString(<<
"<body>",
body,
"</body>"
>>, "\n")
HTMLFrame(id, children) ==
StringSeqToString(<<
ReplaceFirstSubSeq(id, "%id%", "<div id=\"%id%\">"),
StringSeqToString(children, "\n"),
"</div>"
>>, "\n")
HTMLGridContainer(id, class_list, children) ==
StringSeqToString(<<
HTMLFill("<div id=\"%id%\" class=\"default_grid %class_list%\">",<<
<<"%id%", id>>,
<<"%class_list%", StringSeqToString(class_list, " ")>>
>>),
StringSeqToString(children, "\n"),
"</div>"
>>, "\n")
HTMLBox(id, class_list, size, children) ==
StringSeqToString(<<
HTMLFill("<div id=\"%id%\" class=\"default_box %class_list%\" style=\"%size%\">",<<
<<"%id%", id>>,
<<"%class_list%", StringSeqToString(class_list, " ")>>,
<<"%size%", size>>
>>),
StringSeqToString(children, "\n"),
"</div>"
>>, "\n")
HTMLCircle(id, class_list, size, children) ==
HTMLBox(id, <<"default_circle">> \o class_list, size, children)
HTMLText(id, text) ==
StringSeqToString(<<
ReplaceFirstSubSeq(id, "%id%", "<span id=\"%id%\">"),
text,
"</span>"
>>, "\n")
HTMLSVG(id, viewBox, size, svgString) ==
StringSeqToString(<<
HTMLFill("<svg id=\"%id%\" viewBox=\"%viewBox%\" style=\"%size%\">",<<
<<"%id%", id>>,
<<"%viewBox%", viewBox>>,
<<"%size%", size>>
>>),
svgString,
"</svg>"
>>, "\n")
\***** Pre built
HTMLSize(width, height) ==
StringSeqToString(<<
HTMLAttribute("width", width),
HTMLAttribute("height", height)
>>, " ")
HTMLNewLine ==
"<br>"
HTMLFlexCenterAttributes ==
StringSeqToString(<<
HTMLAttribute("display", "flex"),
HTMLAttribute("align-items", "center"),
HTMLAttribute("justify-content", "center")
>>,"\n")
HTMLJSLineElems(fromID, destID) ==
HTMLFill("document.getElementById(\"%id1%\"), document.getElementById(\"%id2%\")",
<< <<"%id1%", fromID>>, <<"%id2%", destID>> >> )
HTMLJSLine(fromID, destID, color, size) ==
HTMLFill("new LeaderLine(%elems%, {color: '%color%', size: %size%})",
<< <<"%elems%", HTMLJSLineElems(fromID, destID)>>,
<<"%color%", color>>, <<"%size%", size>> >> )
HTMLJSLineDash(fromID, destID, color, size) ==
HTMLFill("new LeaderLine(%elems%, {color: '%color%', size: %size%, dash: true})",
<< <<"%elems%", HTMLJSLineElems(fromID, destID)>>,
<<"%color%", color>>, <<"%size%", size>> >> )
HTMLJSKeyListener(events) ==
StringSeqToString(<<
"window.onkeyup = function(event) {",
"let key = event.key.toUpperCase();",
StringSeqToString(events,"\n"),
"}"
>>, "\n")
HTMLJSNavigationKey(key, file) ==
StringSeqToString(<<
ReplaceFirstSubSeq(key, "%key%", "if(key == '%key%'){"),
ReplaceFirstSubSeq(file, "%file%", "window.location.replace(\"%file%\");"),
"}"
>>,"\n")
=============================================================================