Docs tests & a refactor

This commit is contained in:
Max Veytsman
2021-03-06 16:53:46 -05:00
parent 143b0d78c9
commit 682e9d04f3
5 changed files with 73 additions and 28 deletions

View File

@ -8,42 +8,51 @@ defmodule Heroicons do
@doc false
defmacro __before_compile__(%Macro.Env{module: module}) do
unless Module.has_attribute?(module, :icon_path) do
raise CompileError, description: "@icon_path attrubute is required"
unless Module.has_attribute?(module, :icon_dir) do
raise CompileError, description: "@icon_dir attrubute is required"
end
icon_dir = Module.get_attribute(module, :icon_dir)
icon_paths =
Module.get_attribute(module, :icon_path)
Path.absname(icon_dir, :code.priv_dir(:heroicons))
|> Path.join("*.svg")
|> Path.wildcard()
for path <- icon_paths do
name =
Path.basename(path, ".svg")
|> String.replace("-", "_")
|> String.to_atom()
generate_function(path)
end
end
quote do
@doc """
![](#{unquote(path)}) {: width=24px}
@doc false
def generate_function(path) do
name =
Path.basename(path, ".svg")
|> String.replace("-", "_")
|> String.to_atom()
## Examples
iex> #{unquote(name)}()
iex> #{unquote(name)}(class: "h-6 w-6 text-gray-500")
"""
@spec unquote(name)(keyword(binary)) :: binary
def unquote(name)(opts \\ []) do
icon = File.read!(unquote(path))
{i, _} = :binary.match(icon, ">")
doc = """
![](assets/#{Path.relative_to(path, :code.priv_dir(:heroicons))}) {: width=24px}
{head, tail} = String.split_at(icon, i)
## Examples
iex> #{name}()
iex> #{name}(class: "h-6 w-6 text-gray-500")
"""
attrs =
opts
|> Enum.map_join(fn {k, v} -> ~s( #{k}="#{v}") end)
quote do
@doc unquote(doc)
@spec unquote(name)(keyword(binary)) :: binary
def unquote(name)(opts \\ []) do
icon = File.read!(unquote(path))
{i, _} = :binary.match(icon, ">")
head <> attrs <> tail
end
{head, tail} = String.split_at(icon, i)
attrs =
opts
|> Enum.map_join(fn {k, v} -> ~s( #{k}="#{v}") end)
head <> attrs <> tail
end
end
end

View File

@ -4,6 +4,6 @@ defmodule Heroicons.Outline do
For primary navigation and marketing sections, designed to be rendered at 24x24.
"""
@icon_path "#{:code.priv_dir(:heroicons)}/outline/"
@icon_dir "outline/"
@before_compile Heroicons
end

View File

@ -5,6 +5,6 @@ defmodule Heroicons.Solid do
For buttons, form elements, and to support text, designed to be rendered at 20x20.
"""
@icon_path "#{:code.priv_dir(:heroicons)}/solid/"
@icon_dir "solid/"
@before_compile Heroicons
end