Refactor & Cleanup
This commit is contained in:
@ -7,14 +7,6 @@ defmodule Heroicons.Generator do
|
|||||||
|
|
||||||
require Phoenix.Component
|
require Phoenix.Component
|
||||||
|
|
||||||
if function_exported?(Phoenix.Component, :assigns_to_attributes, 2) do
|
|
||||||
Module.put_attribute(__CALLER__.module, :assign_mod, Phoenix.Component)
|
|
||||||
Module.put_attribute(__CALLER__.module, :assigns_to_attrs_mod, Phoenix.Component)
|
|
||||||
else
|
|
||||||
Module.put_attribute(__CALLER__.module, :assign_mod, Phoenix.LiveView)
|
|
||||||
Module.put_attribute(__CALLER__.module, :assigns_to_attrs_mod, Phoenix.LiveView.Helpers)
|
|
||||||
end
|
|
||||||
|
|
||||||
for path <- icon_paths do
|
for path <- icon_paths do
|
||||||
generate(path)
|
generate(path)
|
||||||
end
|
end
|
||||||
@ -22,12 +14,34 @@ defmodule Heroicons.Generator do
|
|||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def generate(path) do
|
def generate(path) do
|
||||||
name =
|
name = Heroicons.Generator.name(path)
|
||||||
Path.basename(path, ".svg")
|
|
||||||
|> String.replace("-", "_")
|
|
||||||
|> String.to_atom()
|
|
||||||
|
|
||||||
doc = """
|
quote do
|
||||||
|
@doc Heroicons.Generator.doc(unquote(path))
|
||||||
|
def unquote(name)(assigns_or_opts \\ [])
|
||||||
|
|
||||||
|
def unquote(name)(assigns) when is_map(assigns) do
|
||||||
|
Heroicons.Generator.icon_component(unquote(path), assigns)
|
||||||
|
end
|
||||||
|
|
||||||
|
def unquote(name)(opts) when is_list(opts) do
|
||||||
|
Heroicons.Generator.icon_function(unquote(path), opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def name(path) do
|
||||||
|
Path.basename(path, ".svg")
|
||||||
|
|> String.replace("-", "_")
|
||||||
|
|> String.to_atom()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def doc(path) do
|
||||||
|
name = name(path)
|
||||||
|
|
||||||
|
"""
|
||||||
)}) {: width=24px}
|
)}) {: width=24px}
|
||||||
## Examples
|
## Examples
|
||||||
Use as a `Phoenix.Component`
|
Use as a `Phoenix.Component`
|
||||||
@ -37,44 +51,49 @@ defmodule Heroicons.Generator do
|
|||||||
<%= #{name}() %>
|
<%= #{name}() %>
|
||||||
<%= #{name}(class: "h-6 w-6 text-gray-500") %>
|
<%= #{name}(class: "h-6 w-6 text-gray-500") %>
|
||||||
"""
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
quote do
|
if function_exported?(Phoenix.Component, :assigns_to_attributes, 2) do
|
||||||
@doc unquote(doc)
|
@assign_mod Phoenix.Component
|
||||||
def unquote(name)(assigns_or_opts \\ [])
|
@assigns_to_attrs_mod Phoenix.Component
|
||||||
|
else
|
||||||
|
@assign_mod Phoenix.LiveView
|
||||||
|
@assigns_to_attrs_mod Phoenix.LiveView.Helpers
|
||||||
|
end
|
||||||
|
|
||||||
def unquote(name)(var!(assigns)) when is_map(var!(assigns)) do
|
@doc false
|
||||||
var!(attrs) = @assigns_to_attrs_mod.assigns_to_attributes(var!(assigns))
|
def icon_component(path, assigns) when is_map(assigns) do
|
||||||
var!(assigns) = @assign_mod.assign(var!(assigns), :attrs, var!(attrs))
|
attrs = @assigns_to_attrs_mod.assigns_to_attributes(assigns)
|
||||||
|
assigns = @assign_mod.assign(assigns, :attrs, attrs)
|
||||||
|
|
||||||
EEx.compile_string("<svg {@attrs}" <> Heroicons.IconCache.icon_body(unquote(path)),
|
EEx.compile_string("<svg {@attrs}" <> Heroicons.IconCache.icon_body(path),
|
||||||
engine: Phoenix.LiveView.HTMLEngine,
|
engine: Phoenix.LiveView.HTMLEngine,
|
||||||
file: __ENV__.file,
|
file: __ENV__.file,
|
||||||
line: __ENV__.line + 1,
|
line: __ENV__.line + 1,
|
||||||
module: __ENV__.module,
|
module: __ENV__.module,
|
||||||
indentation: 0,
|
indentation: 0,
|
||||||
assigns: var!(assigns)
|
assigns: assigns
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def icon_function(path, opts) when is_list(opts) do
|
||||||
|
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
|
end
|
||||||
|
|
||||||
def unquote(name)(opts) when is_list(opts) do
|
{:safe,
|
||||||
attrs =
|
[
|
||||||
for {k, v} <- opts do
|
"<svg",
|
||||||
safe_k =
|
Phoenix.HTML.Safe.to_iodata(attrs),
|
||||||
k |> Atom.to_string() |> String.replace("_", "-") |> Phoenix.HTML.Safe.to_iodata()
|
" ",
|
||||||
|
Heroicons.IconCache.icon_body(path)
|
||||||
safe_v = v |> Phoenix.HTML.Safe.to_iodata()
|
]}
|
||||||
|
|
||||||
{:safe, [?\s, safe_k, ?=, ?", safe_v, ?"]}
|
|
||||||
end
|
|
||||||
|
|
||||||
{:safe,
|
|
||||||
[
|
|
||||||
"<svg",
|
|
||||||
Phoenix.HTML.Safe.to_iodata(attrs),
|
|
||||||
" ",
|
|
||||||
Heroicons.IconCache.icon_body(unquote(path))
|
|
||||||
]}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,5 +6,5 @@ defmodule Heroicons.Outline do
|
|||||||
designed to be rendered at 24x24.
|
designed to be rendered at 24x24.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
use Heroicons.Generator, icon_dir: "icons/solid/"
|
use Heroicons.Generator, icon_dir: "icons/outline/"
|
||||||
end
|
end
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
defmodule <%= inspect module %> do
|
|
||||||
@moduledoc """
|
|
||||||
<%= moduledoc %>
|
|
||||||
"""
|
|
||||||
|
|
||||||
use Phoenix.Component
|
|
||||||
|
|
||||||
if function_exported?(Phoenix.Component, :assigns_to_attributes, 2) do
|
|
||||||
@assign_mod Phoenix.Component
|
|
||||||
@assigns_to_attrs_mod Phoenix.Component
|
|
||||||
else
|
|
||||||
@assign_mod Phoenix.LiveView
|
|
||||||
@assigns_to_attrs_mod Phoenix.LiveView.Helpers
|
|
||||||
end
|
|
||||||
<%= for path <- icon_paths, name = icon_name(path), body = icon_body(path, svg_opts) do %>
|
|
||||||
@doc """
|
|
||||||
) %>) {: width=24px}
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
Use as a `Phoenix.Component`
|
|
||||||
|
|
||||||
<%= ~s(<.#{name} />) %>
|
|
||||||
|
|
||||||
<%= ~s(<.#{name} class="h-6 w-6 text-gray-500" />) %>
|
|
||||||
|
|
||||||
or as a function
|
|
||||||
|
|
||||||
<%= ~s(<%= #{name}() %>) %>
|
|
||||||
|
|
||||||
<%= ~s(<%= #{name}(class: "h-6 w-6 text-gray-500") %>) %>
|
|
||||||
"""
|
|
||||||
def <%= name %>(assigns_or_opts \\ [])
|
|
||||||
|
|
||||||
def <%= name %>(assigns) when is_map(assigns) do
|
|
||||||
attrs = @assigns_to_attrs_mod.assigns_to_attributes(assigns)
|
|
||||||
assigns = @assign_mod.assign(assigns, :attrs, attrs)
|
|
||||||
|
|
||||||
~H"""
|
|
||||||
<svg {@attrs} <%= String.replace(body, "\n", "\n ") %>
|
|
||||||
"""
|
|
||||||
end
|
|
||||||
|
|
||||||
def <%= name %>(opts) when is_list(opts) do
|
|
||||||
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, ["<svg", Phoenix.HTML.Safe.to_iodata(attrs), <%= inspect " " <> body, printable_limit: :infinity %>]}
|
|
||||||
end
|
|
||||||
<% end %>
|
|
||||||
end
|
|
Reference in New Issue
Block a user