Files
systemuicons/livebook.livemd
2022-06-27 15:47:03 +02:00

1.8 KiB

Function components from systemuicons

Mix.install([
  {:jason, "~> 1.3"}
])

Clone git repo

System.cmd("git", ["clone", "--depth=1", "https://github.com/CoreyGinnivan/system-uicons.git"],
  cd: "./priv"
)

{output, _} = System.cmd("ls", ["./priv/system-uicons/src/images/icons"])
IO.puts(output)

Get icon data

path = "./priv/system-uicons/src/js/data.js"
source_data = File.read!(path)
cl = String.to_charlist(source_data)

<<"var sourceData = "::utf8, rest::binary>> = source_data
# remove trailing semicolon
sslice = String.slice(rest, 0..-3//1)
# quote object keys
quote_keys = Regex.replace(~r/([\w_]+):/, sslice, "\"\\1\":")
# remove trailing commas
rm_trailing_commas = Regex.replace(~r/,\s+(}|])/, quote_keys, "\\1")

icon_data = Jason.decode!(rm_trailing_commas)

fetch svg contents

icon_map =
  Enum.map(icon_data, fn %{"icon_path" => path} = icon ->
    svg = File.read!("./priv/system-uicons/src/images/icons/#{path}.svg")

    Map.put_new(icon, "icon_svg", svg)
    |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
  end)

Component template

defmodule Templates do
  def svg(name, contents, path, keywords) do
    """
    @doc "Name: #{name} Keywords: #{keywords}"
    def icon_#{path}(assigns) do
      \"\"\"<i class="svg">
    """ <>
      contents <>
      """
      
      </i>
        \"\"\"
      end
      
      """
  end
end
icon = Enum.at(icon_map, 0)
Templates.svg(icon.icon_name, icon.icon_svg, icon.icon_path, icon.icon_keywords)
icon

Create module file

funs =
  for %{icon_name: name, icon_path: path, icon_svg: svg, icon_keywords: tags} <- icon_map do
    Templates.svg(name, svg, path, tags)
  end
  |> Enum.join()

content = "defmodule do\n" <> funs <> "\nend\n"
File.write!("./icons.ex", content)