defmodule Feathericons.Compiler do @default_attrs [xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", stroke_width: "2", stroke_linecap: "round", stroke_linejoin: "round"] @doc false defmacro __before_compile__(%Macro.Env{}) do icon_paths = Path.absname("icons", :code.priv_dir(:feathericons)) |> Path.join("*.svg") |> Path.wildcard() for path <- icon_paths do generate_function(path, @default_attrs) end end @doc false def generate_function(path, default_attrs) do name = Path.basename(path, ".svg") |> String.replace("-", "_") |> String.to_atom() icon = File.read!(path) {i, _} = :binary.match(icon, ">") {_, body} = String.split_at(icon, i) doc = """ ![](assets/#{Path.relative_to(path, :code.priv_dir(:feathericons))}) {: width=24px} ## Examples iex> #{name}() iex> #{name}(class: "h-6 w-6 text-gray-500") """ quote do @doc unquote(doc) @spec unquote(name)(keyword()) :: {:safe, list()} def unquote(name)(opts \\ []) do opts = Keyword.merge(unquote(default_attrs), opts) attrs = for {k, v} <- opts do safe_k = k |> Atom.to_string() |> String.replace("_", "-") |> Phoenix.HTML.Safe.to_iodata() safe_v = v |> Phoenix.HTML.Safe.to_iodata() {:safe, [?\s, safe_k, ?=, ?", safe_v, ?"]} end {:safe, [" Feathericons.loader(class: "w-4 h-4") {:safe, [ "" ]} """ @before_compile Feathericons.Compiler end