Skip to content

Commit bfc31c0

Browse files
committed
improve some api and examples
1 parent db8508f commit bfc31c0

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.vscode/
1+
.vscode/
2+
main.ua

examples/todo.ua

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
# Experimental!
2-
~ "../lib.ua"
3-
~ ! A B Body Br Button Div Em Form Head Html H₁ H₂ H₃ H₄ H₅ H₆ I Li Meta Ol P Script Span Strong Style Table Title Ul
4-
~ HXGet HXSwap HXTarget NotFound ResHTML Response Serve!
2+
Html ~ "../lib.ua" ~ HtmlResp NotFound Serve!
53

6-
Item ← Div {
7-
⊓(
8-
| Button {HXSwap "outerHTML" HXTarget "closest div" ⊃(HXGet $"/item-tog/_"|$"Item: _")}
9-
| Br {}
10-
Br {}
11-
)
12-
}
4+
Item ← Html!(
5+
Div{⊓(
6+
Button {HXSwap "outerHTML" HXTarget "closest div" ⊃(HXGet $"/item-tog/_"|$"Item: _")}
7+
| Br {}
8+
)}
9+
)
1310

14-
Index ← (
11+
Index ← Html!(
1512
Html {
1613
Head {
1714
Title "Webua"
15+
Meta {!charset "utf-8"}
1816
Script {!src "https://unpkg.com/[email protected]"}
1917
}
2018
Body {
2119
H₁ "Webua Example"
22-
P "This is a simple example of Webua templating."
20+
P "This is a simple example of Webua templating with HTMX!"
2321
Item "a"
2422
Item "b"
2523
Item "c"
2624
}
2725
}
28-
ResHTML
29-
)
30-
31-
RespondItem ← (
32-
Item
33-
ResHTML
3426
)
3527

3628
Route ← ⍣(
37-
⍩Index°"/"
38-
| ⍩RespondItem¯ ?°$"/item-tog/_"
29+
(HtmlResp Index) °"/"
30+
| ⍩(HtmlResp Item) ¯ °$"/item-tog/_"
3931
| NotFound
4032
)
4133

42-
Serve!(Route)
34+
Serve!Route 8080

lib.ua

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
Xml ~ "xml" ~ ! Tags!
22
~ "xml" ~ ! Tags!
33

4-
Listener ← &tcpl "0.0.0.0:8080"
5-
64
HXGet ← $$ hx-get="_"
75
HXPost ← $$ hx-post="_"
86
HXSwap ← $$ hx-swap="_"
97
HXTarget ← $$ hx-target="_"
108

11-
# ? Status Mime Bytes
9+
# Construct an HTTP response
10+
# ResponseBytes ? Status Mime Bytes
1211
Response ← (
13-
⊙⊙(⧻.)
12+
⊙⊙⊸⧻
1413
$$ HTTP/1.1 _
1514
$$ Content-Type: _
1615
$$ Content-Length: _
1716
$$
1817
$$
1918
⊂ utf₈
2019
)
20+
# Construct a simple text HTTP response
21+
TextResp ← Response "200 OK" "text/html" utf₈
22+
# A simple not found HTTP response
23+
NotFound ← (Response "404 Not Found" "text/plain" utf₈"Not Found")
24+
# Construct a simple HTML HTTP response
25+
HtmlResp ← Response "200 OK" "text/html" utf₈
2126

22-
ResHTML ← (Response "200 OK" "text/html" utf₈)
23-
24-
Respond! ← ◌pool(
25-
&p $"Request from _" &tcpaddr.
26-
# Extract path from request
27-
°□⊡1 ⊜□≠@\s . &ru "\r\n\r\n".
28-
&p $"Request:\n_".
29-
30-
^0
31-
&p⊸($"Response: _ bytes" ⧻)
32-
33-
# Send response
34-
⊃⋅&cl&w
27+
Respond! ← (
28+
&w⊸(
29+
⟜(&p $"Request from _" &tcpaddr)
30+
°□⊡1 ⊜□⊸≠@\s &ru "\r\n\r\n"
31+
⟜(&p $"Request:\n_")
32+
^0
33+
⟜(&p $"Response: _ bytes" ⧻)
34+
)
3535
)
3636

37-
Serve! ← ⍢(⍣Respond!(^0)⋅&p &tcpa Listener)1
37+
Sig! ↚^ ⋅⊢
3838

39-
NotFound ← (
40-
Response "404 Not Found" "text/plain" utf₈"Not Found"
39+
# Start a server on the given port.
40+
# The function will be called on the TCP socket for each incoming connection.
41+
# ? Port
42+
Serve! ← (
43+
&tcpl $"0.0.0.0:_"
44+
⟜(&p $"Server started on _" &tcpaddr)
45+
⍢(⍜&tcpa(⍣Respond!^0⋅&p))1
4146
)
4247

4348
Tags!(

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Templating is done with tag functions. A tag function is passed a list of attrib
99
The `!` macro makes it easy to define attributes.
1010

1111
```uiua
12-
~ "git: github.com/uiua-lang/webua" ~ ! Html Head Body P Br H₁ Title Meta Style
12+
Html ~ "git: github.com/uiua-lang/webua"
1313
14-
Html {
14+
Html!(Html {
1515
Head {
1616
Title "Example"
1717
Meta {!charset "utf-8"}
@@ -22,19 +22,19 @@ Html {
2222
P "This is a simple example of Webua templating."
2323
Br {}
2424
}
25-
}
25+
})
2626
```
2727

2828
You can use normal Uiua primitives to fill out data.
2929

3030
```uiua
31-
~ "git: github.com/uiua-lang/webua" ~ Div H₁ Ul Li
31+
~ "git: github.com/uiua-lang/webua"
3232
3333
$Users {"Alice" "Bob" "Carol"}
34-
Div {
34+
Html!(Div {
3535
H₁ "Users"
3636
Ul ⍚Li
37-
}
37+
})
3838
## "<div><h1>Users</h1><ul><li>Alice</li><li>Bob</li><li>Carol</li></ul></div>"
3939
```
4040

0 commit comments

Comments
 (0)