Packages

Ezmodex is a lightweight experimental Elixir microframework built on top of Plug

Current section

5 Versions

Jump to

Compare versions

6 files changed
+30 additions
-7 deletions
  @@ -8,7 +8,7 @@ Highly experimental lightweight microframework built on top of Plug
8 8
9 9 ```elixir
10 10 def deps do
11 - [{:ezmodex, "~> 0.3.0"}]
11 + [{:ezmodex, "~> 0.3.1"}]
12 12 end
13 13 ```
  @@ -21,4 +21,4 @@
21 21 {<<"name">>,<<"plug">>},
22 22 {<<"optional">>,false},
23 23 {<<"requirement">>,<<"~> 1.0">>}]]}.
24 - {<<"version">>,<<"0.3.0">>}.
24 + {<<"version">>,<<"0.3.1">>}.
  @@ -73,6 +73,11 @@ defmodule Ezmodex.Elements do
73 73
74 74 """
75 75
76 + @typedoc """
77 + Element tree is a list that contains either strings or Element trees
78 + """
79 + @type element_tree :: [String.t | element_tree]
80 +
76 81 defmacro __using__(options) do
77 82 quote do
78 83 import Kernel, except: [div: 2]
  @@ -89,13 +94,18 @@ defmodule Ezmodex.Elements do
89 94 "<!DOCTYPE html><html><head></head><body</body></html>"
90 95
91 96 """
97 + @spec html5(element_tree) :: element_tree
92 98 def html5(children) do
93 99 ["<!DOCTYPE html>", html([ children ])]
94 100 end
95 101
96 102 Enum.each(@all_elements, fn name ->
97 - @doc false
98 - def unquote(name)(), do: unquote(name)(%{}, [])
103 +
104 + if Enum.member?(@empty_elements, name) do
105 + @doc false
106 + def unquote(name)(), do: unquote(name)(%{}, [])
107 + end
108 +
99 109 @doc false
100 110 def unquote(name)(attributes) when is_map(attributes), do: unquote(name)(attributes, [])
101 111 @doc false
  @@ -118,7 +128,7 @@ defmodule Ezmodex.Elements do
118 128 "&lt;script&gt;"
119 129
120 130 """
121 - @spec text(String.t) :: String.t
131 + @spec text(String.t) :: [String.t]
122 132 def text(string) do
123 133 [Ezmodex.HTML.Sanitizer.clean(string)]
124 134 end
  @@ -136,7 +146,7 @@ defmodule Ezmodex.Elements do
136 146 "<bunny awesomeness="high"></bunny>"
137 147
138 148 """
139 - @spec build_element(String.t, map, list) :: List.t
149 + @spec build_element(String.t, map, element_tree) :: element_tree
140 150 def build_element(element, attributes, children) do
141 151 element_name = Atom.to_string(element)
  @@ -17,6 +17,7 @@ defmodule Ezmodex.HTML.Sanitizer do
17 17 "Tom &amp; Jerry"
18 18
19 19 """
20 + @spec clean(String.t) :: String.t
20 21 def clean(""), do: ""
21 22 def clean(string) do
22 23 string
  @@ -7,6 +7,9 @@ defmodule Ezmodex.Page do
7 7 import unquote(__MODULE__)
8 8
9 9 def init(opts), do: opts
10 +
11 + def set_data(conn), do: nil
12 + defoverridable [set_data: 1]
10 13 end
11 14 end
12 15
  @@ -18,9 +21,18 @@ defmodule Ezmodex.Page do
18 21 end
19 22 end
20 23
24 + defmacro data(do: content) do
25 + quote do
26 + def set_data(conn), do: unquote(content)
27 + def data, do: nil
28 + end
29 + end
30 +
21 31 defmacro view(do: content) do
22 32 quote do
23 33 def view(conn) do
34 + set_data(conn)
35 +
24 36 conn
25 37 |> put_resp_content_type("text/html")
26 38 |> send_resp(@status_code, Enum.join(unquote(content), ""))
Loading more files…