Skip to content

Commit 23fbb1f

Browse files
authored
Add HTML module (#61)
Add HTML module. [Feature]
1 parent d4a5f45 commit 23fbb1f

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

modules/HTML.tla

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
-------------------------------- MODULE HTML --------------------------------
2+
3+
LOCAL INSTANCE Sequences
4+
LOCAL INSTANCE SequencesExt
5+
6+
\***** Helpers
7+
8+
HTMLFill(string, args) ==
9+
(***********************************************************************)
10+
(* Do the sequence of replaces in args to the string. *)
11+
(* The args is a sequence of tuples: <<substr,newsubstr>>. *)
12+
(* Example: *)
13+
(* HTMLFill("test %num%", << <<"%num%", "12">> >>) = "test 12" *)
14+
(***********************************************************************)
15+
LET f(str, x) == ReplaceFirstSubSeq(x[2], x[1], str)
16+
IN FoldLeft(f, string, args)
17+
18+
LOCAL StringSeqToString(string_seq, separator) ==
19+
(***********************************************************************)
20+
(* Concatenates sequence of strings into one string. *)
21+
(* The `seperator` is put between the strings in the sequence. *)
22+
(* Example: *)
23+
(* StringSeqToString(<<"Test","string!">>, "-") = "Test-string!" *)
24+
(***********************************************************************)
25+
LET f(str1,str2) == IF str1 = "" THEN str2 ELSE str1 \o separator \o str2
26+
IN FoldLeft(f, "", string_seq)
27+
28+
\***** Main Document
29+
30+
HTMLDocument(header, body, list_of_scripts) ==
31+
StringSeqToString(<<
32+
"<!DOCTYPE html>",
33+
"<html>",
34+
header,
35+
body,
36+
StringSeqToString(list_of_scripts,"\n"),
37+
"</html>"
38+
>>, "\n")
39+
40+
\***** Style Sheet
41+
42+
HTMLClass(classname, attribute_list) ==
43+
StringSeqToString(<<
44+
ReplaceFirstSubSeq(classname, "%classname%", "%classname% {"),
45+
StringSeqToString(attribute_list,"\n"),
46+
"}"
47+
>>, "\n")
48+
49+
HTMLAttribute(name, value) ==
50+
HTMLFill("%name%: %value%;", <<
51+
<<"%name%", name>>,
52+
<<"%value%", value>>
53+
>>)
54+
55+
HTMLDefaultStyle ==
56+
StringSeqToString(<<
57+
HTMLClass(".default_grid",<<
58+
HTMLAttribute("display", "grid")
59+
>>),
60+
HTMLClass(".default_box",<<
61+
HTMLAttribute("border", "1px solid black")
62+
>>),
63+
HTMLClass(".default_circle",<<
64+
HTMLAttribute("border-radius", "50%")
65+
>>)
66+
>>, "\n")
67+
68+
HTMLStyleSheet(class_list) ==
69+
StringSeqToString(<<
70+
"<style type=\"text/css\">",
71+
HTMLDefaultStyle,
72+
StringSeqToString(class_list,"\n"),
73+
"</style>"
74+
>>, "\n")
75+
76+
\***** Script
77+
78+
HTMLScriptFile(name) ==
79+
HTMLFill("<script src=\"%name%\"></script>", << <<"%name%", name>>>> )
80+
81+
HTMLScript(children) ==
82+
StringSeqToString(<<
83+
"<script>",
84+
StringSeqToString(children, "\n"),
85+
"</script>"
86+
>>, "\n")
87+
88+
\***** Header
89+
90+
HTMLLink(name, type) ==
91+
HTMLFill("<link href=\"%name%\" rel=\"%type%\"/>",
92+
<< <<"%name%", name>>, <<"%type%", type>> >> )
93+
94+
HTMLHeader(title, list_of_links, class_list) ==
95+
StringSeqToString(<<
96+
"<head>",
97+
"<meta charset=\"UTF-8\">",
98+
ReplaceFirstSubSeq(title, "%title%", "<title>%title%</title>"),
99+
StringSeqToString(list_of_links,"\n"),
100+
HTMLStyleSheet(class_list),
101+
"</head>"
102+
>>, "\n")
103+
104+
\***** Body
105+
106+
HTMLBody(body) ==
107+
StringSeqToString(<<
108+
"<body>",
109+
body,
110+
"</body>"
111+
>>, "\n")
112+
113+
HTMLFrame(id, children) ==
114+
StringSeqToString(<<
115+
ReplaceFirstSubSeq(id, "%id%", "<div id=\"%id%\">"),
116+
StringSeqToString(children, "\n"),
117+
"</div>"
118+
>>, "\n")
119+
120+
HTMLGridContainer(id, class_list, children) ==
121+
StringSeqToString(<<
122+
HTMLFill("<div id=\"%id%\" class=\"default_grid %class_list%\">",<<
123+
<<"%id%", id>>,
124+
<<"%class_list%", StringSeqToString(class_list, " ")>>
125+
>>),
126+
StringSeqToString(children, "\n"),
127+
"</div>"
128+
>>, "\n")
129+
130+
HTMLBox(id, class_list, size, children) ==
131+
StringSeqToString(<<
132+
HTMLFill("<div id=\"%id%\" class=\"default_box %class_list%\" style=\"%size%\">",<<
133+
<<"%id%", id>>,
134+
<<"%class_list%", StringSeqToString(class_list, " ")>>,
135+
<<"%size%", size>>
136+
>>),
137+
StringSeqToString(children, "\n"),
138+
"</div>"
139+
>>, "\n")
140+
141+
HTMLCircle(id, class_list, size, children) ==
142+
HTMLBox(id, <<"default_circle">> \o class_list, size, children)
143+
144+
HTMLText(id, text) ==
145+
StringSeqToString(<<
146+
ReplaceFirstSubSeq(id, "%id%", "<span id=\"%id%\">"),
147+
text,
148+
"</span>"
149+
>>, "\n")
150+
151+
HTMLSVG(id, viewBox, size, svgString) ==
152+
StringSeqToString(<<
153+
HTMLFill("<svg id=\"%id%\" viewBox=\"%viewBox%\" style=\"%size%\">",<<
154+
<<"%id%", id>>,
155+
<<"%viewBox%", viewBox>>,
156+
<<"%size%", size>>
157+
>>),
158+
svgString,
159+
"</svg>"
160+
>>, "\n")
161+
162+
\***** Pre built
163+
164+
HTMLSize(width, height) ==
165+
StringSeqToString(<<
166+
HTMLAttribute("width", width),
167+
HTMLAttribute("height", height)
168+
>>, " ")
169+
170+
HTMLNewLine ==
171+
"<br>"
172+
173+
HTMLFlexCenterAttributes ==
174+
StringSeqToString(<<
175+
HTMLAttribute("display", "flex"),
176+
HTMLAttribute("align-items", "center"),
177+
HTMLAttribute("justify-content", "center")
178+
>>,"\n")
179+
180+
HTMLJSLineElems(fromID, destID) ==
181+
HTMLFill("document.getElementById(\"%id1%\"), document.getElementById(\"%id2%\")",
182+
<< <<"%id1%", fromID>>, <<"%id2%", destID>> >> )
183+
184+
HTMLJSLine(fromID, destID, color, size) ==
185+
HTMLFill("new LeaderLine(%elems%, {color: '%color%', size: %size%})",
186+
<< <<"%elems%", HTMLJSLineElems(fromID, destID)>>,
187+
<<"%color%", color>>, <<"%size%", size>> >> )
188+
189+
HTMLJSLineDash(fromID, destID, color, size) ==
190+
HTMLFill("new LeaderLine(%elems%, {color: '%color%', size: %size%, dash: true})",
191+
<< <<"%elems%", HTMLJSLineElems(fromID, destID)>>,
192+
<<"%color%", color>>, <<"%size%", size>> >> )
193+
194+
HTMLJSKeyListener(events) ==
195+
StringSeqToString(<<
196+
"window.onkeyup = function(event) {",
197+
"let key = event.key.toUpperCase();",
198+
StringSeqToString(events,"\n"),
199+
"}"
200+
>>, "\n")
201+
202+
HTMLJSNavigationKey(key, file) ==
203+
StringSeqToString(<<
204+
ReplaceFirstSubSeq(key, "%key%", "if(key == '%key%'){"),
205+
ReplaceFirstSubSeq(file, "%file%", "window.location.replace(\"%file%\");"),
206+
"}"
207+
>>,"\n")
208+
=============================================================================

0 commit comments

Comments
 (0)