/
icon.ex
54 lines (41 loc) · 1.33 KB
/
icon.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule FontAwesome.Icon do
@moduledoc """
This module defines the data structure and functions for working with icons stored as SVG files.
"""
alias __MODULE__
@doc """
Defines the FontAwesome.Icon struct.
Its fields are:
* `:type` - the type of the icon
* `:name` - the name of the icon
* `:file` - the binary of the icon
"""
defstruct [:type, :name, :file]
@type t :: %Icon{type: String.t(), name: String.t(), file: binary}
@doc "Parses a SVG file and returns structured data"
@spec parse!(String.t()) :: Icon.t()
def parse!(filename) do
[type, name] = filename |> Path.split() |> Enum.take(-2)
name = Path.rootname(name)
file = File.read!(filename)
struct!(__MODULE__, type: type, name: name, file: file)
end
@doc "Converts opts to HTML attributes"
@spec opts_to_attrs(keyword) :: list
def opts_to_attrs(opts) do
for {key, value} <- opts do
key =
key
|> Atom.to_string()
|> String.replace("_", "-")
|> Phoenix.HTML.Safe.to_iodata()
value = Phoenix.HTML.Safe.to_iodata(value)
[?\s, key, ?=, ?", value, ?"]
end
end
@doc "Inserts HTML attributes into an SVG icon"
@spec insert_attrs(binary, keyword) :: Phoenix.HTML.safe()
def insert_attrs("<svg" <> rest, attrs) do
Phoenix.HTML.raw(["<svg", attrs, rest])
end
end