Rename Temple.EEx to Temple.Generator

This commit is contained in:
Mitchell Hanberg 2021-04-11 17:27:02 -04:00
parent 07a1d5d451
commit 074241be4e
26 changed files with 49 additions and 43 deletions

View file

@ -30,6 +30,12 @@ def deps do
end
```
## Goals
Temple has a few things on which it won't compromise.
- On
## Usage
Using Temple is as simple as using the DSL inside of an `temple/1` block. This returns an EEx string at compile time.

View file

@ -121,7 +121,7 @@ defmodule Temple do
markup =
block
|> Parser.parse()
|> Enum.map(&Temple.EEx.to_eex/1)
|> Enum.map(&Temple.Generator.to_eex/1)
|> :erlang.iolist_to_binary()
quote location: :keep do
@ -133,7 +133,7 @@ defmodule Temple do
quote location: :keep do
unquote(block)
|> Parser.parse()
|> Enum.map(&Temple.EEx.to_eex/1)
|> Enum.map(&Temple.Generator.to_eex/1)
|> :erlang.iolist_to_binary()
end
end
@ -159,7 +159,7 @@ defmodule Temple do
markup =
block
|> Parser.parse()
|> Enum.map(&Temple.EEx.to_eex/1)
|> Enum.map(&Temple.Generator.to_eex/1)
|> :erlang.iolist_to_binary()
EEx.compile_string(markup, engine: engine, line: __CALLER__.line, file: __CALLER__.file)

View file

@ -1,3 +0,0 @@
defprotocol Temple.EEx do
def to_eex(ast)
end

3
lib/temple/generator.ex Normal file
View file

@ -0,0 +1,3 @@
defprotocol Temple.Generator do
def to_eex(ast)
end

View file

@ -31,7 +31,7 @@ defmodule Temple.Parser.AnonymousFunctions do
Temple.Ast.new(__MODULE__, elixir_ast: expression, children: children)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{elixir_ast: {name, _, args}, children: children}) do
{_do_and_else, args} = Temple.Parser.Utils.split_args(args)
@ -51,7 +51,7 @@ defmodule Temple.Parser.AnonymousFunctions do
to_string(arrow),
" %>",
"\n",
for(child <- children, do: Temple.EEx.to_eex(child)),
for(child <- children, do: Temple.Generator.to_eex(child)),
if Enum.any?(args2) do
[
"<% end, ",

View file

@ -33,7 +33,7 @@ defmodule Temple.Parser.Components do
)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{module: module, assigns: assigns, children: []}) do
[
"<%= Phoenix.View.render",
@ -59,7 +59,7 @@ defmodule Temple.Parser.Components do
" ",
"do %>",
"\n",
for(child <- children, do: Temple.EEx.to_eex(child)),
for(child <- children, do: Temple.Generator.to_eex(child)),
"\n",
"<% end %>"
]

View file

@ -14,7 +14,7 @@ defmodule Temple.Parser.Default do
Temple.Ast.new(__MODULE__, elixir_ast: ast)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{elixir_ast: expression}) do
["<%= ", Macro.to_string(expression), " %>\n"]
end

View file

@ -29,16 +29,16 @@ defmodule Temple.Parser.DoExpressions do
Temple.Ast.new(__MODULE__, elixir_ast: {name, meta, args}, children: [do_body, else_body])
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{elixir_ast: expression, children: [do_body, else_body]}) do
[
"<%= ",
Macro.to_string(expression),
" do %>",
"\n",
for(child <- do_body, do: Temple.EEx.to_eex(child)),
for(child <- do_body, do: Temple.Generator.to_eex(child)),
if(else_body != nil,
do: ["<% else %>\n", for(child <- else_body, do: Temple.EEx.to_eex(child))],
do: ["<% else %>\n", for(child <- else_body, do: Temple.Generator.to_eex(child))],
else: ""
),
"<% end %>"

View file

@ -15,7 +15,7 @@ defmodule Temple.Parser.Empty do
Temple.Ast.new(__MODULE__)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(_) do
[]
end

View file

@ -18,7 +18,7 @@ defmodule Temple.Parser.Match do
Temple.Ast.new(__MODULE__, elixir_ast: macro)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{elixir_ast: elixir_ast}) do
["<% ", Macro.to_string(elixir_ast), " %>"]
end

View file

@ -28,14 +28,14 @@ defmodule Temple.Parser.NonvoidElementsAliases do
Temple.Ast.new(__MODULE__, name: to_string(name), attrs: args, children: children)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{name: name, attrs: attrs, children: children}) do
[
"<",
name,
Temple.Parser.Utils.compile_attrs(attrs),
">\n",
for(child <- children, do: Temple.EEx.to_eex(child)),
for(child <- children, do: Temple.Generator.to_eex(child)),
"\n</",
name,
">"

View file

@ -17,13 +17,13 @@ defmodule Temple.Parser.RightArrow do
Temple.Ast.new(__MODULE__, elixir_ast: pattern, children: children)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{elixir_ast: elixir_ast, children: children}) do
[
"<% ",
Macro.to_string(elixir_ast),
" -> %>\n",
for(child <- children, do: Temple.EEx.to_eex(child))
for(child <- children, do: Temple.Generator.to_eex(child))
]
end
end

View file

@ -15,7 +15,7 @@ defmodule Temple.Parser.Text do
Temple.Ast.new(__MODULE__, text: text)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{text: text}) do
[text, "\n"]
end

View file

@ -20,7 +20,7 @@ defmodule Temple.Parser.VoidElementsAliases do
Temple.Ast.new(__MODULE__, name: name, attrs: args)
end
defimpl Temple.EEx do
defimpl Temple.Generator do
def to_eex(%{name: name, attrs: attrs}) do
[
"<",

View file

@ -65,7 +65,7 @@ defmodule Temple.Parser.AnonymousFunctionsTest do
end
end
describe "Temple.EEx.to_eex/1" do
describe "Temple.Generator.to_eex/1" do
test "emits eex" do
raw_ast =
quote do
@ -78,7 +78,7 @@ defmodule Temple.Parser.AnonymousFunctionsTest do
raw_ast
|> AnonymousFunctions.run()
|> struct(children: [])
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= form_for changeset, Routes.foo_path(conn, :create), fn form -> %>\n<% end %>\n|

View file

@ -106,7 +106,7 @@ defmodule Temple.Parser.ComponentsTest do
end
end
describe "Temple.EEx.to_eex/1" do
describe "Temple.Generator.to_eex/1" do
test "emits eex for non void component" do
raw_ast =
quote do
@ -118,7 +118,7 @@ defmodule Temple.Parser.ComponentsTest do
result =
raw_ast
|> Components.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= Phoenix.View.render_layout SomeModule, :self, [foo: :bar] do %>\nI'm a component!\n<% end %>|
@ -133,7 +133,7 @@ defmodule Temple.Parser.ComponentsTest do
result =
raw_ast
|> Components.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= Phoenix.View.render SomeModule, :self, [foo: :bar] %>|

View file

@ -34,7 +34,7 @@ defmodule Temple.Parser.DefaultTest do
Foo.bar!(baz)
end
|> Default.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|<%= Foo.bar!(baz) %>\n|
end

View file

@ -46,7 +46,7 @@ defmodule Temple.Parser.DoExpressionsTest do
end
end
|> DoExpressions.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= for(big <- boys) do %>\nbob\n<% end %>|
@ -64,7 +64,7 @@ defmodule Temple.Parser.DoExpressionsTest do
end
end
|> DoExpressions.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= if(foo?) do %>\nbob\nbobby\n<% else %>\ncarol\n<% end %>|
@ -79,7 +79,7 @@ defmodule Temple.Parser.DoExpressionsTest do
end
end
|> DoExpressions.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<%= case(foo?) do %>\n<% :bing -> %>\n<%= :bong %>\n<% end %>|

View file

@ -31,7 +31,7 @@ defmodule Temple.Parser.EmptyTest do
end
end
describe "Temple.EEx.to_eex/1" do
describe "Temple.Generator.to_eex/1" do
test "emits eex for non void component" do
raw_ast =
quote do
@ -41,7 +41,7 @@ defmodule Temple.Parser.EmptyTest do
result =
raw_ast
|> Empty.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ""
end

View file

@ -37,7 +37,7 @@ defmodule Temple.Parser.MatchTest do
end
end
describe "Temple.EEx.to_eex/1" do
describe "Temple.Generator.to_eex/1" do
test "emits eex" do
raw_ast =
quote do
@ -47,7 +47,7 @@ defmodule Temple.Parser.MatchTest do
result =
raw_ast
|> Match.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|<% yolo = :synergy %>|
end
@ -64,7 +64,7 @@ defmodule Temple.Parser.MatchTest do
result =
raw_ast
|> Match.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|<% yolo = if(true) do\n :synergy\nend %>|
end

View file

@ -93,7 +93,7 @@ defmodule Temple.Parser.NonvoidElementsAliasesTest do
end
end
|> NonvoidElementsAliases.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<div class="foo" id="<%= var %>">\n<select>\n<option>\nfoo\n\n</option>\n</select>\n</div>|

View file

@ -69,7 +69,7 @@ defmodule Temple.Parser.RightArrowTest do
end
|> List.first()
|> RightArrow.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<% :bing -> %>\n<%= :bong %>\n|

View file

@ -64,7 +64,7 @@ defmodule Temple.Parser.TempleNamespaceNonvoidTest do
end
end
|> TempleNamespaceNonvoid.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() ==
~s|<div class="foo" id="<%= var %>">\nfoo\n\n</div>|

View file

@ -57,7 +57,7 @@ defmodule Temple.Parser.TempleNamespaceVoidTest do
Temple.meta(content: "foo")
end
|> TempleNamespaceVoid.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|<meta content="foo">\n|
end

View file

@ -29,12 +29,12 @@ defmodule Temple.Parser.TextTest do
end
end
describe "Temple.EEx.to_eex/1" do
describe "Temple.Generator.to_eex/1" do
test "emits eex" do
result =
"string literal"
|> Text.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|string literal\n|
end

View file

@ -62,7 +62,7 @@ defmodule Temple.Parser.VoidElementsAliasesTest do
meta content: "foo"
end
|> VoidElementsAliases.run()
|> Temple.EEx.to_eex()
|> Temple.Generator.to_eex()
assert result |> :erlang.iolist_to_binary() == ~s|<meta content="foo">\n|
end