Use SAX parser to process SVGs

This commit is contained in:
Max Veytsman
2022-08-31 10:37:29 -04:00
parent 11fcb4183e
commit 05ef2f9a8a
6 changed files with 157 additions and 11 deletions

View File

@ -25,7 +25,19 @@ defmodule Heroicons do
designed to be rendered at 24x24.
"""
use Heroicons.Generator, icon_dir: "outline/"
use Heroicons.Generator,
icon_dir: "outline/",
# Following https://github.com/tailwindlabs/heroicons/blob/b933d51df1f27c35414389fea185e9bac0097481/svgo.24.outline.yaml
svg_opts: [
remove_dimensions: true,
sort_attributes: true,
remove_attributes: ["stroke", "path:stroke-width"],
add_attributes: [
{"stroke-widt", "1.5"},
{"stroke", "currentColor"},
{"aria-hidden", "true"}
]
]
end
defmodule Solid do
@ -36,7 +48,18 @@ defmodule Heroicons do
designed to be rendered at 24x24.
"""
use Heroicons.Generator, icon_dir: "solid/"
use Heroicons.Generator,
icon_dir: "solid/",
# Following https://github.com/tailwindlabs/heroicons/blob/b933d51df1f27c35414389fea185e9bac0097481/svgo.24.solid.yaml
svg_opts: [
remove_dimensions: true,
sort_attributes: true,
remove_attributes: ["fill"],
add_attributes: [
{"fill", "currentColor"},
{"aria-hidden", "true"}
]
]
end
defmodule Mini do
@ -47,6 +70,17 @@ defmodule Heroicons do
designed to be rendered at 20x20.
"""
use Heroicons.Generator, icon_dir: "mini/"
use Heroicons.Generator,
icon_dir: "mini/",
# Following https://github.com/tailwindlabs/heroicons/blob/b933d51df1f27c35414389fea185e9bac0097481/svgo.20.solid.yaml
svg_opts: [
remove_dimensions: true,
sort_attributes: true,
remove_attributes: ["fill"],
add_attributes: [
{"fill", "currentColor"},
{"aria-hidden", "true"}
]
]
end
end

View File

@ -1,11 +1,14 @@
defmodule Heroicons.Generator do
defmacro __using__(icon_dir: icon_dir) 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)
@ -15,20 +18,23 @@ defmodule Heroicons.Generator do
end
for path <- icon_paths do
generate(path)
generate(path, svg_opts)
end
end
@doc false
def generate(path) do
def generate(path, svg_opts) do
name =
Path.basename(path, ".svg")
|> String.replace("-", "_")
|> String.to_atom()
icon = File.read!(path)
{i, _} = :binary.match(icon, ">")
{head, body} = String.split_at(icon, i)
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}

View File

@ -0,0 +1,18 @@
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

@ -0,0 +1,86 @@
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