Document void and nonvoid element macros

The mix task will fetch the element documentation from MDN for each
element and include that in the documentation.

The main documentation for these macros will be in the moduledoc
This commit is contained in:
Mitchell Hanberg 2019-05-08 22:09:14 -04:00
parent 83b99d8a8a
commit db17577bb5
2 changed files with 46 additions and 5 deletions

View file

@ -24,6 +24,9 @@ defmodule Dsl.Html do
area br col embed hr img input keygen param source track wbr
]a
def nonvoid_elements, do: @nonvoid_elements
def void_elements, do: @void_elements
@doc """
Creates a markup context.
@ -65,6 +68,9 @@ defmodule Dsl.Html do
end
for el <- @nonvoid_elements do
@doc """
#{File.read!("./tmp/docs/#{el}.txt")}
"""
defmacro unquote(el)() do
el = unquote(el)
@ -74,26 +80,28 @@ defmodule Dsl.Html do
end
end
defmacro unquote(el)([{:do, inner} | attrs]) do
@doc false
defmacro unquote(el)([{:do, inner}] = _attrs_or_content_or_block) do
el = unquote(el)
quote do
put_open_tag(var!(buff, Dsl.Html), unquote(el), unquote(attrs))
put_open_tag(var!(buff, Dsl.Html), unquote(el), [])
_ = unquote(inner)
put_close_tag(var!(buff, Dsl.Html), unquote(el))
end
end
defmacro unquote(el)(attrs_or_content) do
defmacro unquote(el)(attrs_or_content_or_block) do
el = unquote(el)
quote do
put_open_tag(var!(buff, Dsl.Html), unquote(el), unquote(attrs_or_content))
put_open_tag(var!(buff, Dsl.Html), unquote(el), unquote(attrs_or_content_or_block))
put_close_tag(var!(buff, Dsl.Html), unquote(el))
end
end
defmacro unquote(el)(attrs, [{:do, inner}]) do
@doc false
defmacro unquote(el)(attrs, [{:do, inner}] = _block) do
el = unquote(el)
quote do
@ -130,6 +138,10 @@ defmodule Dsl.Html do
end
for el <- @void_elements do
@doc """
#{File.read!("./tmp/docs/#{el}.txt")}
"""
defmacro unquote(el)(attrs \\ []) do
el = unquote(el)

View file

@ -0,0 +1,29 @@
defmodule Mix.Tasks.UpdateMdnDocs do
use Mix.Task
@baseurl "https://developer.mozilla.org/en-US/docs/Web/HTML/Element/"
@params "?summary&raw"
@shortdoc "Update the MDN documentation"
def run(_) do
(Dsl.Html.nonvoid_elements() ++ Dsl.Html.void_elements())
|> Enum.each(fn el ->
el = to_string(el)
page =
if Enum.any?(["h1", "h2", "h3", "h4", "h5", "h6"], & &1 == el) do
"Heading_Elements"
else
el
end
{doc, 0} = System.cmd("curl", [@baseurl <> page <> @params])
File.mkdir_p!("./tmp/docs/")
path = "./tmp/docs/" <> el <> ".txt"
doc = HtmlSanitizeEx.strip_tags(doc)
File.write!(path, doc)
end)
end
end