This repository was archived by the owner on Jun 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.ua
More file actions
234 lines (199 loc) · 7.29 KB
/
lib.ua
File metadata and controls
234 lines (199 loc) · 7.29 KB
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
# Experimental!
# NOTICE: uiua-html is experimental until `case` gets stabilized.
# Please excuse any inconveniencies this might cause.
# I've a feeling I'll use this more often than I think
IsMap ↚ ⍣(1⋅◌°map|0)
┌─╴Element
CheckTag ↚ ⍤⊃($"expected string for tag, got _"repr|↧⊃(=1type|=1⧻△))
CheckAttrs ↚ (
⍤⊃($"expected map for attributes, got _"repr|IsMap).◴
¬has "style"
⍤ $ The 'style' attribute is prohibited from use.
$ Please make a new file and use `Style` to import it.
)
MagicValue ↚ "\u{2024}"
# This module holds the unsafe versions of
# some functions and other unsafe parts of `Element`.
┌─╴Unsafe
# `Element~WithAttrs`, but it does not check its arguments at runtime.
# Only use if you're sure that your values match the expected types.
# element ? tag content attributes
WithAttrs ← $Element {MagicValue ⊓($Tag|$Attributes|$Content)⊙:}
# `Element~New`, but it does not check its arguments at runtime.
# Only use if you're sure that your values match the expected types.
# element ? tag content
New ← WithAttrs ⊙⊙(map{}[])
# `Element~NoEndTag`, but it does not check its arguments at runtime.
# Only use if you're sure that your values match the expected types.
# element ? tag attributes
NoEndTag ← $Element {MagicValue ⊓($Tag|$Attributes)}
└─╴
# Checks if the value is a valid element.
# - `0` - invalid element
# - `1` - element with end tag
# - `2` - element without end tag
# elementType ? value
Check ← ⍣(
1 ⋅⋅◌°Unsafe~WithAttrs
| 2 ⋅◌°Unsafe~NoEndTag
| 0)
# Wraps the arguments into a new element with attributes attached.
# Throws an error if `tag` isn't a string or `attributes` isn't a map.
# element ? tag content attributes
WithAttrs ← Unsafe~WithAttrs ⊓(CheckTag.|∘|CheckAttrs.)
# Wraps the arguments into a new element.
# Throws an error if `tag` isn't a string.
# element ? tag content
New ← Unsafe~New CheckTag.
# Wraps its arguments into a new element with attributes and no end tag.
# element ? tag attributes
ExNoEndTag ← Unsafe~NoEndTag ⊓(CheckTag|CheckAttrs),,
# Wraps the single argument into a new element with no end tag.
# element ? tag
NoEndTag ← ExNoEndTag ⊙(map{}[])
└─╴
# This module stores the raw element definitions of various elements,
# as well as their versions with attribute parameters (`Ex*`).
# Please note this module is **really unsafe and unstable**.
#
# For anything that should be exposed or abstracted,
# please [file an issue](https://github.com/thacuber2a03/uiua-html)
# in the code's repository, and in the meantime, refer to this module.
#
# For rare tags, or tags simply missing from this module,
# prefer the creation functions in the `Element` module.
┌─╴Raw
# thanks to kai himself for this snippet
Replace ↚ +⬚0⊏⊸⊃(⊗:⊙⋅∘)-
Stylize ↚ Replace "123456" "₁₂₃₄₅₆"
Elements! ↚^ (
/$"_\n_"⍚(
,,⊸(⍜⊢¯Stylize)
$$ _ ← Element "_"
$$ Ex_ ← Element~WithAttrs "_"
))
NETElements! ↚^ (
/$"_\n_"⍚(
,,⊸(⍜⊢¯Stylize)
$$ _ = Element~NoEndTag "_"
$$ Ex_ = Element~ExNoEndTag "_"
))
Elements!(html|head|body|title)
Elements!(ul|ol|li)
Elements!(q|blockquote)
Elements!(p|a|h1|h2|h3|h4|h5|h6)
Elements!(b|i|strong|em)
Elements!(script|style)
NETElements!(br)
NETElements!(link)
└─╴
# Generates a standard HTML document from the provided arguments.
# document ? head body
Document‼ ← $Document Raw~Html [$Head Raw~Head ^0 $Body Raw~Body ^1]
# Returns a new basic document from the provided arguments.
# A "basic document" is a page with only a title and some content.
# document ? title content
BasicDoc‼ ← Document‼[Raw~Title ^0] ^1
H₁ ← Raw~H₁ # h1 ? content
ExH₁ ← Raw~ExH₁ # h1 ? content attributes
H₂ ← Raw~H₂ # h2 ? content
ExH₂ ← Raw~ExH₂ # h2 ? content attributes
H₃ ← Raw~H₃ # h3 ? content
ExH₃ ← Raw~ExH₃ # h3 ? content attributes
H₄ ← Raw~H₄ # h4 ? content
ExH₄ ← Raw~ExH₄ # h4 ? content attributes
H₅ ← Raw~H₅ # h5 ? content
ExH₅ ← Raw~ExH₅ # h5 ? content attributes
H₆ ← Raw~H₆ # h6 ? content
ExH₆ ← Raw~ExH₆ # h6 ? content attributes
P ← Raw~P # p ? content
ExP ← Raw~ExP # p ? content attributes
# Includes a JavaScript file in classic mode.
# script ? filepath
ClassicScript ← Raw~ExScript "" map{"src"}{∘}
# Include a JavaScript module.
# module ? filepath
Script ← Raw~ExScript "" map{"type" "src"}{"module" ∘}
# Includes a CSS file.
# style ? filepath
Style ← Raw~ExLink map{"rel" "src"}["stylesheet" ∘]
# An ordered list.
# list ? values
List ← $List Raw~Ol ≡Raw~Li
# An unordered list.
# list ? values
UList ← $UList Raw~Ul ≡Raw~Li
# A hyperlink.
# hyperlink ? value link
Link ← Raw~ExA ⊙(map{"href"}{∘})
# A hyperlink with extra attributes.
# a ? value link attributes
ExLink ← Raw~ExA ⊙(insert "href")
Break ← Raw~Br # br ?
ExBreak ← Raw~ExBr # br ? attributes
# A quote with extra attributes.
# The underlying element depends on the argument:
# - multiline string or array of elements -- a <blockquote> element
# - anything else -- a <q> element
ExQuote ← (
=1type.
$Quote ⨬(
Raw~ExBlockquote
| ≠0/↥⌕@\n.
⨬(Raw~ExQ|Raw~ExBlockquote)
)
)
# A quote. The underlying element depends on the argument:
# - multiline string or array of elements -- a <blockquote> element
# - anything else -- a <q> element
Quote ← ExQuote ⊙(map{}{})
Title ← Raw~Title # title ? value
ExTitle ← Raw~ExTitle # title ? value attributes
# The main rendering module. You're free to call this as a function,
# as its `Call` function is the main rendering function.
┌─╴Render
# The map of characters that get replaced when you call `Escape`.
EscapeMap ← comptime(map"&<>'\""{"&" "<" ">" "'" """})
# Escapes `value` so it renders safely.
# escaped ? value
Escape ← /(⊂∩°□)≡⍣(get:EscapeMap)
# Renders a map into a single string of HTML element attributes.
# Throws an error if any of the keys isn't a string.
# string ? attributes
Attributes ← (
°map≡□
⟜⍚(⍤⊃($"all the keys in the map must be strings, found a _"repr|=1type))
≡(□$"_=\"_\"" ⊙(Escape$"_")°⊟)⍉⊟
/$"_ _"
)
# Renders the `element` into HTML.
# Unlike all safe `Element` calls, this function/module
# accepts any value with no issues, since it gets escaped and
# inserted into the resulting file. Useful for debugging.
# rendered ? element
Call ← |1 (
=0⧻△.
⨬(type.
⨬(Escape repr
| Escape
| ⍣(≠0⧻.:⊙: °Element~Unsafe~WithAttrs
⨬($"<_>_</_>" ⟜(Call:)◌
| $"<_ _>_</_>" :⊙⟜(Call:) caseAttributes
)
| ≠0⧻.: °Element~Unsafe~NoEndTag
⨬($"<_/>" ◌
| $"<_ _/>" :caseAttributes
)
| /⊂ ≡(Call °□)
| Escape repr)
| Escape repr)
| Escape repr)
)
# Render a document. This function differs from `Render`/`Render~Call` in that
# it appends specific things that apply to full pages (<!DOCTYPE HTML> and the like).
Doc ← (
Call
$$ <!DOCTYPE HTML>
$$ _
)
└─╴