From db17577bb526e6872d9c16a6aa50eb4c67ab1155 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Wed, 8 May 2019 22:09:14 -0400 Subject: [PATCH] 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 --- lib/dsl/html.ex | 22 +++++++++++++++++----- lib/mix/tasks/update_mdn_docs.ex | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 lib/mix/tasks/update_mdn_docs.ex diff --git a/lib/dsl/html.ex b/lib/dsl/html.ex index ea50cd5..2e3c5a9 100644 --- a/lib/dsl/html.ex +++ b/lib/dsl/html.ex @@ -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) diff --git a/lib/mix/tasks/update_mdn_docs.ex b/lib/mix/tasks/update_mdn_docs.ex new file mode 100644 index 0000000..1b5598d --- /dev/null +++ b/lib/mix/tasks/update_mdn_docs.ex @@ -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