defmodule Nitroux.Utils do @doc """ Generates dynamic open and closing tags around content iex> Nitroux.Utils.tag("div", ["hello", " ", "world"]) "
hello world
" Nitroux.Utils.tag("div", []) "
hello world
" Nitroux.Utils.tag("div", [html: "hello world"]) "
hello world
" Nitroux.Utils.tag("div", [class: "test", html: "hello world"]) "
hello world
" """ def tag(name, attrs, container \\ true) def tag(name, _, false), do: "<#{name}/>" def tag(name, [{_, _} | _t] = keywordlist, _container), do: "<#{name}#{add_attributes(keywordlist)}>#{Keyword.get(keywordlist, :html, "")}" def tag(name, [_h | _t] = list, _container), do: name |> tag(html: Enum.join(list)) def tag(name, [], _container), do: name |> tag([], true) def tag(name, text, _) when is_binary(text), do: "<#{name}>#{text}" defp add_attributes(attrs) do attrs |> Keyword.filter(fn {key, _} -> key !== :html end) |> Enum.map(fn {key, val} -> "#{key}=\"#{val}\"" end) |> Enum.join(" ") |> case do "" -> "" res -> " " <> res end end end