Skip to content

Commit e98da1b

Browse files
committed
Html module with macros that expand to virtual-dom functions
1 parent ad2ec91 commit e98da1b

File tree

10 files changed

+3524
-10
lines changed

10 files changed

+3524
-10
lines changed

lib/elixir_script.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ defmodule ElixirScript do
8080

8181
defp custom_env() do
8282
require Logger
83-
require ElixirScript.Lib.JS, as: JS
83+
require ElixirScript.JS, as: JS
84+
require ElixirScript.Html, as: Html
8485
__ENV__
8586
end
8687

lib/elixir_script/lib/html.ex

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule ElixirScript.Html do
2+
3+
@external_resource tags_path = Path.join([__DIR__, "tags.txt"])
4+
@tags (for line <- File.stream!(tags_path, [], :line) do
5+
line |> String.strip |> String.to_atom
6+
end)
7+
8+
for tag <- @tags do
9+
defmacro unquote(tag)(config \\ [], block \\ [do: nil]) do
10+
tag = Atom.to_string(unquote(tag))
11+
12+
inner = case Keyword.get(block, :do) do
13+
{:__block__, [], params} ->
14+
params
15+
nil ->
16+
[]
17+
x ->
18+
[x]
19+
end
20+
21+
config = config_to_map(config)
22+
23+
quote do
24+
Elixir.VirtualDOM.h(unquote(tag), unquote(config), unquote_splicing(inner))
25+
end
26+
end
27+
end
28+
29+
defp config_to_map(config) do
30+
config = Enum.map(config, fn({key, value}) ->
31+
if is_atom(key) do
32+
{Atom.to_string(key), value}
33+
else
34+
{key, value}
35+
end
36+
end)
37+
38+
{:%{}, [], config}
39+
end
40+
41+
defmacro create(element) do
42+
quote do
43+
Elixir.VirtualDOM.create(unquote(element))
44+
end
45+
end
46+
47+
defmacro diff(tree, newTree) do
48+
quote do
49+
Elixir.VirtualDOM.diff(unquote(tree), unquote(newTree))
50+
end
51+
end
52+
53+
defmacro patch(root, patches) do
54+
quote do
55+
Elixir.VirtualDOM.patch(unquote(root), unquote(patches))
56+
end
57+
end
58+
59+
end

lib/elixir_script/lib/js.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule ElixirScript.Lib.JS do
1+
defmodule ElixirScript.JS do
22

33
@doc """
44
Creates new JavaScript objects.
@@ -25,8 +25,8 @@ defmodule ElixirScript.Lib.JS do
2525
but JavaScript modules work differently and have to be imported
2626
using this.
2727
28-
If module is not a list, then it is treated as a default import,
29-
otherwise it is not.
28+
If module is not a list, then it is treated as a default import,
29+
otherwise it is not.
3030
3131
ex:
3232
JS.import A, "a" #translates to "import {default as A} from 'a'"
@@ -47,4 +47,4 @@ defmodule ElixirScript.Lib.JS do
4747
"""
4848
defmacro to_json(value)
4949

50-
end
50+
end

lib/elixir_script/lib/tags.txt

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
doctype!
2+
a
3+
abbr
4+
acronym
5+
address
6+
applet
7+
area
8+
article
9+
aside
10+
audio
11+
b
12+
base
13+
basefont
14+
bdi
15+
bdo
16+
big
17+
blockquote
18+
body
19+
br
20+
button
21+
canvas
22+
caption
23+
center
24+
cite
25+
code
26+
col
27+
colgroup
28+
command
29+
datalist
30+
dd
31+
del
32+
details
33+
dfn
34+
dir
35+
div
36+
dl
37+
dt
38+
em
39+
embed
40+
fieldset
41+
figcaption
42+
figure
43+
font
44+
footer
45+
form
46+
frame
47+
frameset
48+
h1
49+
head
50+
header
51+
hgroup
52+
hr
53+
html
54+
i
55+
iframe
56+
img
57+
input
58+
ins
59+
kbd
60+
keygen
61+
label
62+
legend
63+
li
64+
link
65+
map
66+
mark
67+
menu
68+
meta
69+
meter
70+
nav
71+
noframes
72+
noscript
73+
object
74+
ol
75+
optgroup
76+
option
77+
output
78+
p
79+
param
80+
pre
81+
progress
82+
q
83+
rp
84+
rt
85+
ruby
86+
s
87+
samp
88+
script
89+
section
90+
select
91+
small
92+
source
93+
span
94+
strike
95+
strong
96+
style
97+
sub
98+
summary
99+
sup
100+
table
101+
tbody
102+
td
103+
textarea
104+
tfoot
105+
th
106+
thead
107+
time
108+
title
109+
tr
110+
track
111+
tt
112+
u
113+
ul
114+
var
115+
video
116+
wbr

lib/elixir_script/preprocess/modules.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ defmodule ElixirScript.Preprocess.Modules do
5858
end
5959

6060
def do_get_info({:defmodule, _, [{:__aliases__, _, [:ElixirScript, :Temp]}, [do: body]]} = ast) do
61+
body = case body do
62+
{:__block__, _, _ } ->
63+
Macro.expand(body, State.get().env)
64+
_ ->
65+
body
66+
end
67+
6168
mod = %ElixirScript.Module{ name: [:ElixirScript, :Temp] , body: body }
6269
State.add_module(mod)
6370

0 commit comments

Comments
 (0)