Extract helper module for creating elements
Also removes illegal `html` macros, which allows use to only define one clause and remove some tests.
This commit is contained in:
parent
f3aabf8f59
commit
b0a7f9da11
4 changed files with 69 additions and 143 deletions
|
@ -8,7 +8,7 @@ defmodule Mix.Tasks.UpdateMdnDocs do
|
|||
def run(_) do
|
||||
IO.puts("Downloading MDN documentation")
|
||||
|
||||
(Temple.Tags.nonvoid_elements() ++ Temple.Tags.void_elements())
|
||||
(Temple.Tags.nonvoid_elements() ++ Temple.Tags.void_elements() ++ ["html"])
|
||||
|> Enum.map(fn el ->
|
||||
Task.async(fn ->
|
||||
el = to_string(el)
|
||||
|
|
54
lib/temple/elements.ex
Normal file
54
lib/temple/elements.ex
Normal file
|
@ -0,0 +1,54 @@
|
|||
defmodule Temple.Elements do
|
||||
def nonvoid_element(el) do
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), [])
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
end
|
||||
|
||||
def nonvoid_element(el, attrs_or_content_or_block)
|
||||
|
||||
def nonvoid_element(el, [{:do, inner}]) do
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), [])
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
end
|
||||
|
||||
def nonvoid_element(el, attrs_or_content) do
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), unquote(attrs_or_content))
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
end
|
||||
|
||||
def nonvoid_element(el, attrs_or_content, block_or_attrs)
|
||||
|
||||
def nonvoid_element(el, attrs, [{:do, inner}] = _block) do
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([el, attrs]))
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
end
|
||||
|
||||
def nonvoid_element(el, content, attrs) do
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([el, attrs]))
|
||||
text unquote(content)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
end
|
||||
|
||||
def void_element(el, attrs \\ []) do
|
||||
quote do
|
||||
attrs = unquote(attrs)
|
||||
|
||||
Temple.Utils.put_buffer(
|
||||
var!(buff, Temple.Tags),
|
||||
"<#{unquote(el)}#{Temple.Utils.compile_attrs(attrs)}>"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -88,125 +88,44 @@ defmodule Temple.Tags do
|
|||
for el <- @nonvoid_elements do
|
||||
@doc if File.exists?("./tmp/docs/#{el}.txt"), do: File.read!("./tmp/docs/#{el}.txt")
|
||||
defmacro unquote(el)() do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), [])
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
Temple.Elements.nonvoid_element(unquote(el))
|
||||
end
|
||||
|
||||
defmacro unquote(el)(attrs_or_content_or_block)
|
||||
|
||||
defmacro unquote(el)([{:do, inner}]) do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), [])
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
defmacro unquote(el)([{:do, _inner}] = block) do
|
||||
Temple.Elements.nonvoid_element(unquote(el), block)
|
||||
end
|
||||
|
||||
defmacro unquote(el)(attrs_or_content) do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(el), unquote(attrs_or_content))
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
Temple.Elements.nonvoid_element(unquote(el), attrs_or_content)
|
||||
end
|
||||
|
||||
defmacro unquote(el)(attrs_or_content, block_or_attrs)
|
||||
|
||||
defmacro unquote(el)(attrs, [{:do, inner}] = _block) do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([el, attrs]))
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
defmacro unquote(el)(attrs, [{:do, _inner}] = block) do
|
||||
Temple.Elements.nonvoid_element(unquote(el), attrs, block)
|
||||
end
|
||||
|
||||
defmacro unquote(el)(content, attrs) do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([el, attrs]))
|
||||
text unquote(content)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(el))
|
||||
end
|
||||
Temple.Elements.nonvoid_element(unquote(el), content, attrs)
|
||||
end
|
||||
end
|
||||
|
||||
for el <- @void_elements do
|
||||
@doc if File.exists?("./tmp/docs/#{el}.txt"), do: File.read!("./tmp/docs/#{el}.txt")
|
||||
defmacro unquote(el)(attrs \\ []) do
|
||||
el = unquote(el)
|
||||
|
||||
quote do
|
||||
attrs = unquote(attrs)
|
||||
|
||||
Temple.Utils.put_buffer(
|
||||
var!(buff, Temple.Tags),
|
||||
"<#{unquote(el)}#{Temple.Utils.compile_attrs(attrs)}>"
|
||||
)
|
||||
end
|
||||
Temple.Elements.void_element(unquote(el), attrs)
|
||||
end
|
||||
end
|
||||
|
||||
@doc if File.exists?("./tmp/docs/html.txt"), do: File.read!("./tmp/docs/html.txt")
|
||||
defmacro unquote(:html)() do
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(:html), [])
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(:html))
|
||||
end
|
||||
end
|
||||
defmacro html(attrs \\ [], [{:do, _inner}] = block) do
|
||||
doc_type =
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
end
|
||||
|
||||
defmacro unquote(:html)(attrs_or_content_or_block)
|
||||
|
||||
defmacro unquote(:html)([{:do, inner}]) do
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote(:html), [])
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(:html))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro unquote(:html)(attrs_or_content) do
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
|
||||
Temple.Utils.put_open_tag(
|
||||
var!(buff, Temple.Tags),
|
||||
unquote(:html),
|
||||
unquote(attrs_or_content)
|
||||
)
|
||||
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(:html))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro unquote(:html)(attrs_or_content, block_or_attrs)
|
||||
|
||||
defmacro unquote(:html)(attrs, [{:do, inner}] = _block) do
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([:html, attrs]))
|
||||
_ = unquote(inner)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(:html))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro unquote(:html)(content, attrs) do
|
||||
quote do
|
||||
Temple.Utils.put_buffer(var!(buff, Temple.Tags), "<!DOCTYPE html>")
|
||||
Temple.Utils.put_open_tag(var!(buff, Temple.Tags), unquote_splicing([:html, attrs]))
|
||||
text unquote(content)
|
||||
Temple.Utils.put_close_tag(var!(buff, Temple.Tags), unquote(:html))
|
||||
end
|
||||
[doc_type, Temple.Elements.nonvoid_element(:html, attrs, block)]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,53 +2,6 @@ defmodule Temple.TagsTest do
|
|||
use ExUnit.Case, async: true
|
||||
use Temple
|
||||
|
||||
# Seperate tests for the html tag
|
||||
|
||||
test "renders a html" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
html()
|
||||
end
|
||||
|
||||
assert result == ~s{<!DOCTYPE html><html></html>}
|
||||
end
|
||||
|
||||
test "renders a html with attrs" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
html(class: "hello")
|
||||
end
|
||||
|
||||
assert result == ~s{<!DOCTYPE html><html class="hello"></html>}
|
||||
end
|
||||
|
||||
test "renders a html with content" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
html "Hi"
|
||||
end
|
||||
|
||||
assert result == "<!DOCTYPE html><html>Hi</html>"
|
||||
end
|
||||
|
||||
test "renders a html with escaped content" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
html("<div>1</div>")
|
||||
end
|
||||
|
||||
assert result == "<!DOCTYPE html><html><div>1</div></html>"
|
||||
end
|
||||
|
||||
test "renders a html with attrs and content" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
html("Hi", class: "hello")
|
||||
end
|
||||
|
||||
assert result == ~s{<!DOCTYPE html><html class="hello">Hi</html>}
|
||||
end
|
||||
|
||||
test "renders a html with a block" do
|
||||
{:safe, result} =
|
||||
temple do
|
||||
|
|
Reference in a new issue