diff --git a/livebook.livemd b/livebook.livemd index 95a50d1..212e792 100644 --- a/livebook.livemd +++ b/livebook.livemd @@ -41,6 +41,49 @@ icon_data = Jason.decode!(rm_trailing_commas) 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 + +```elixir +defmodule Templates do + def svg(name, contents, path, keywords) do + """ + @doc "Name: #{name} Keywords: #{keywords}" + def icon_#{path}(assigns) do + \"\"\" + """ <> + contents <> + """ + + + \"\"\" + end + + """ + end +end +``` + +```elixir +icon = Enum.at(icon_map, 0) +Templates.svg(icon.icon_name, icon.icon_svg, icon.icon_path, icon.icon_keywords) +icon +``` + +## Create module file + +```elixir +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) +``` diff --git a/systemuicons_ex.exs b/systemuicons_ex.exs index fc9e8be..724feff 100644 --- a/systemuicons_ex.exs +++ b/systemuicons_ex.exs @@ -1,3 +1,52 @@ Mix.install([ - + {:jason, "~> 1.3"} ]) + +repo_url = "https://github.com/CoreyGinnivan/system-uicons.git" +System.shell("rm -rf ./*", cd: "./priv") +System.cmd("git", ["clone", "--depth=1", repo_url], cd: "./priv") + +path = "./priv/system-uicons/src/js/data.js" +source_data = File.read!(path) + +<<"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) + +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) + +defmodule Templates do + def svg(name, contents, path, keywords) do + """ + @doc "Name: #{name} Keywords: #{keywords}" + def icon_#{path}(assigns) do + \"\"\" + + """ + <> contents <> + """ + + + \"\"\" + end + + """ + end +end + +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)