Pregenerate the icons with a mix task

This commit is contained in:
Max Veytsman
2022-08-31 16:41:05 -04:00
parent 9c6dafbd81
commit 093383a73a
12 changed files with 35681 additions and 197 deletions

View File

@ -1,91 +0,0 @@
defmodule Heroicons.Generator do
alias Heroicons.SvgProcessor
defmacro __using__(icon_dir: icon_dir, svg_opts: svg_opts) do
icon_paths =
Path.absname(icon_dir, :code.priv_dir(:heroicons))
|> Path.join("*.svg")
|> Path.wildcard()
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
generate(path, svg_opts)
end
end
@doc false
def generate(path, svg_opts) do
name =
Path.basename(path, ".svg")
|> String.replace("-", "_")
|> String.to_atom()
icon =
File.read!(path)
|> SvgProcessor.process(svg_opts)
<<"<svg", body::binary>> = icon
head = "<svg "
doc = """
![](assets/#{Path.relative_to(path, :code.priv_dir(:heroicons))}) {: width=24px}
## Examples
Use as a `Phoenix.Component`
<.#{name} />
<.#{name} class="h-6 w-6 text-gray-500" />
or as a function
<%= #{name}() %>
<%= #{name}(class: "h-6 w-6 text-gray-500") %>
"""
quote do
@doc unquote(doc)
def unquote(name)(assigns_or_opts \\ [])
def unquote(name)(var!(assigns)) when is_map(var!(assigns)) do
var!(attrs) = @assigns_to_attrs_mod.assigns_to_attributes(var!(assigns))
var!(assigns) = @assign_mod.assign(var!(assigns), :attrs, var!(attrs))
unquote(
EEx.compile_string(head <> "{@attrs}" <> body,
engine: Phoenix.LiveView.HTMLEngine,
file: __ENV__.file,
line: __ENV__.line + 1,
module: __ENV__.module,
indentation: 0
)
)
end
def unquote(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, [unquote(head), Phoenix.HTML.Safe.to_iodata(attrs), unquote(body)]}
end
end
end
end

11871
lib/heroicons/mini.ex Normal file

File diff suppressed because it is too large Load Diff

11716
lib/heroicons/outline.ex Normal file

File diff suppressed because it is too large Load Diff

11874
lib/heroicons/solid.ex Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,18 +0,0 @@
defmodule Heroicons.SvgProcessor do
@moduledoc """
An SVG parser loosly based on https://github.com/svg/svgo
## Options
Currently supports the following options:
* `:remove_dimensions` - remove the `width` and `height` attributes. Defaults to false.
* `:sort_attributes` - sort the svg attributes by name. Default to false.
* `:remove_attributes` - list of attributes to remove
* `:add_attributes` - list of `{"name", "value"}` pairs of attributes to add
"""
def process(svg, opts \\ []) do
{:ok, stack} = Saxy.parse_string(svg, Heroicons.SvgProcessor.Handler, {[], opts})
Saxy.encode!(stack)
end
end

View File

@ -1,86 +0,0 @@
defmodule Heroicons.SvgProcessor.Handler do
@moduledoc false
@behaviour Saxy.Handler
@impl Saxy.Handler
def handle_event(:start_document, _prolog, {stack, opts}) do
{:ok, {stack, opts}}
end
@impl Saxy.Handler
def handle_event(:start_element, {"svg", attributes}, {stack, opts}) do
attributes =
filter_attributes(attributes, opts)
|> add_attributes(opts)
|> sort_attributes(opts)
tag = {"svg", attributes, []}
{:ok, {[tag | stack], opts}}
end
def handle_event(:start_element, {tag_name, attributes}, {stack, opts}) do
tag = {tag_name, attributes, []}
{:ok, {[tag | stack], opts}}
end
@impl Saxy.Handler
def handle_event(:characters, chars, {stack, opts}) do
[{tag_name, attributes, content} | stack] = stack
current = {tag_name, attributes, [chars | content]}
{:ok, {[current | stack], opts}}
end
@impl Saxy.Handler
def handle_event(:cdata, chars, {stack, opts}) do
[{tag_name, attributes, content} | stack] = stack
current = {tag_name, attributes, [{:cdata, chars} | content]}
{:ok, {[current | stack], opts}}
end
@impl Saxy.Handler
def handle_event(:end_element, tag_name, {[{tag_name, attributes, content} | stack], opts}) do
current = {tag_name, attributes, Enum.reverse(content)}
case stack do
[] ->
{:ok, {current, opts}}
[parent | rest] ->
{parent_tag_name, parent_attributes, parent_content} = parent
parent = {parent_tag_name, parent_attributes, [current | parent_content]}
{:ok, {[parent | rest], opts}}
end
end
@impl Saxy.Handler
def handle_event(:end_document, _, {stack, _opts}) do
{:ok, stack}
end
defp filter_attributes(attributes, opts) do
remove_dimensions = Keyword.get(opts, :remove_dimensions)
remove_attrs = Keyword.get(opts, :remove_attributes, [])
Enum.reject(attributes, fn {attr, _value} ->
(remove_dimensions && (attr == "width" || attr == "height")) ||
attr in remove_attrs
end)
end
defp add_attributes(attributes, opts) do
attributes ++ Keyword.get(opts, :add_attributes, [])
end
defp sort_attributes(attributes, opts) do
if Keyword.get(opts, :sort_attributes) do
Enum.sort_by(attributes, fn {attr, _value} -> attr end)
else
attributes
end
end
end