| 
 | 1 | +defmodule ExCSSModules do  | 
 | 2 | +  @moduledoc """  | 
 | 3 | +  CSS Modules helpers  | 
 | 4 | +  """  | 
 | 5 | + | 
 | 6 | +  alias __MODULE__  | 
 | 7 | +  alias Phoenix.HTML  | 
 | 8 | + | 
 | 9 | +  @doc """  | 
 | 10 | +  Reads the stylesheet. Returns a map if the stylesheet is already a map. Reads  | 
 | 11 | +  the file if the stylesheet is a string.  | 
 | 12 | +
  | 
 | 13 | +  ## Examples  | 
 | 14 | +
  | 
 | 15 | +    iex> stylesheet(%{})  | 
 | 16 | +    %{}  | 
 | 17 | +
  | 
 | 18 | +    iex> stylesheet("../stylesheet.css")  | 
 | 19 | +    %{}  | 
 | 20 | +  """  | 
 | 21 | +  def stylesheet(definition) when is_map(definition), do: definition  | 
 | 22 | + | 
 | 23 | +  @doc false  | 
 | 24 | +  def stylesheet(definition), do: definition |> read_stylesheet()  | 
 | 25 | + | 
 | 26 | +  def read_stylesheet(filename) do  | 
 | 27 | +    case File.exists?(filename) do  | 
 | 28 | +      true -> filename <> ".json"  | 
 | 29 | +              |> File.read!  | 
 | 30 | +              |> Poison.decode!  | 
 | 31 | +      false -> %{}  | 
 | 32 | +    end  | 
 | 33 | +  end  | 
 | 34 | + | 
 | 35 | +  @doc """  | 
 | 36 | +  Reads the class definitions from the definition map and maps them to a class  | 
 | 37 | +  attribute. The classes argument takes any argument the class_name for the key.  | 
 | 38 | +
  | 
 | 39 | +  ## Examples  | 
 | 40 | +    iex> class(%{ "hello" => "world"}, "hello")  | 
 | 41 | +    {:safe, ~s(class="world")}  | 
 | 42 | +  """  | 
 | 43 | +  def class(definition, classes) do  | 
 | 44 | +    definition  | 
 | 45 | +    |> class_name(classes)  | 
 | 46 | +    |> class_attribute  | 
 | 47 | +  end  | 
 | 48 | + | 
 | 49 | +  @doc """  | 
 | 50 | +  Returns the class name from the definition map is value is true.  | 
 | 51 | +
  | 
 | 52 | +  ## Examples  | 
 | 53 | +    iex> class_name(%{"hello" => "world"}, "hello", true)  | 
 | 54 | +    "world"  | 
 | 55 | +
  | 
 | 56 | +    iex> class_name(%{"hello" => "world"}, "hello", false)  | 
 | 57 | +    nil  | 
 | 58 | +  """  | 
 | 59 | +  def class_name(definition, key, value) do  | 
 | 60 | +    if value do  | 
 | 61 | +      class_name(definition, key)  | 
 | 62 | +    end  | 
 | 63 | +  end  | 
 | 64 | + | 
 | 65 | +  @doc """  | 
 | 66 | +  Returns the class name from the definition map is the second argument  | 
 | 67 | +  in the tuple is true.  | 
 | 68 | +
  | 
 | 69 | +  ## Examples  | 
 | 70 | +    iex> class_name(%{"hello" => "world"}, {"hello", true})  | 
 | 71 | +    "world"  | 
 | 72 | +
  | 
 | 73 | +    iex> class_name(%{"hello" => "world"}, {"hello", false})  | 
 | 74 | +    nil  | 
 | 75 | +  """  | 
 | 76 | +  def class_name(definition, {key, value}), do:  | 
 | 77 | +    class_name(definition, key, value)  | 
 | 78 | + | 
 | 79 | +  @doc """  | 
 | 80 | +  Returns the class name sfrom the definition map when the argument is a list  | 
 | 81 | +  of values or tuples.  | 
 | 82 | +
  | 
 | 83 | +  ## Examples  | 
 | 84 | +    iex> class_name(%{"hello" => "world", "foo" => "bar"}, ["hello", "foo"])  | 
 | 85 | +    "world bar"  | 
 | 86 | +
  | 
 | 87 | +    iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{"hello", true}, {"foo", true}])  | 
 | 88 | +    "world bar"  | 
 | 89 | +
  | 
 | 90 | +    iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{"hello", true}, {"foo", false}])  | 
 | 91 | +    "world"  | 
 | 92 | +  """  | 
 | 93 | +  def class_name(definition, keys) when is_list(keys) do  | 
 | 94 | +    keys  | 
 | 95 | +    |> Enum.map(&class_name(definition, &1))  | 
 | 96 | +    |> Enum.reject(&is_nil/1)  | 
 | 97 | +    |> Enum.join(" ")  | 
 | 98 | +  end  | 
 | 99 | + | 
 | 100 | +  @doc """  | 
 | 101 | +  Returns the class name sfrom the definition map when the argument is a list  | 
 | 102 | +  of values or tuples.  | 
 | 103 | +
  | 
 | 104 | +  ## Examples  | 
 | 105 | +    iex> class_name(%{"hello" => "world"}, "hello")  | 
 | 106 | +    "world"  | 
 | 107 | +
  | 
 | 108 | +    iex> class_name(%{"hello" => "world"}, "foo")  | 
 | 109 | +    nil  | 
 | 110 | +  """  | 
 | 111 | +  def class_name(stylesheet, key) do  | 
 | 112 | +    stylesheet  | 
 | 113 | +    |> stylesheet()  | 
 | 114 | +    |> Map.get(key, nil)  | 
 | 115 | +  end  | 
 | 116 | + | 
 | 117 | +  @doc """  | 
 | 118 | +  Takes the definition and makes a class selector that can be used in CSS out of  | 
 | 119 | +  the classes given. Takes either a single value or a list of classes.  | 
 | 120 | +
  | 
 | 121 | +  ## Examples  | 
 | 122 | +    iex> class_selector(%{ "hello" => "world"}, "hello")  | 
 | 123 | +    ".world"  | 
 | 124 | +    iex> class_selector(%{ "hello" => "world", "foo" => "bar"}, ["hello", "foo"])  | 
 | 125 | +    ".world.foo"  | 
 | 126 | +  """  | 
 | 127 | +  def class_selector(definition, classes) when is_list(classes) do  | 
 | 128 | +    classes  | 
 | 129 | +    |> Enum.map(&class_selector(definition, &1))  | 
 | 130 | +    |> Enum.reject(&is_nil/1)  | 
 | 131 | +    |> Enum.join("")  | 
 | 132 | +  end  | 
 | 133 | + | 
 | 134 | +  @doc false  | 
 | 135 | +  def class_selector(definition, class) do  | 
 | 136 | +    case class_name(definition, class) do  | 
 | 137 | +      nil -> nil  | 
 | 138 | +      value -> ".#{value}"  | 
 | 139 | +    end  | 
 | 140 | +  end  | 
 | 141 | + | 
 | 142 | +  defp class_attribute(class), do: HTML.raw(~s(class="#{class}"))  | 
 | 143 | +end  | 
0 commit comments