This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/test/support/utils.ex
Mitchell Hanberg c965048f40
Better whitespace handling and control (#145)
* Fine tune whitespace

The EEx outut now emits more human-readable and predictable formatting.
This includes proper indenting, at least for each "root" template.

* Internal whitespace control

You can now use a bang version of any nonvoid tag to emit the markup
witout the internal whitespace. This means that there will not be a
newline emitted after the opening tag and before the closing tag.
2021-08-29 17:45:07 -04:00

52 lines
1 KiB
Elixir

defmodule Temple.Support.Utils do
defmacro __using__(_) do
quote do
import Kernel, except: [==: 2, =~: 2]
import unquote(__MODULE__)
end
end
def a == b when is_binary(a) and is_binary(b) do
a = String.replace(a, "\n", "")
b = String.replace(b, "\n", "")
Kernel.==(a, b)
end
def a =~ b when is_binary(a) and is_binary(b) do
a = String.replace(a, "\n", "")
b = String.replace(b, "\n", "")
Kernel.=~(a, b)
end
def env do
require Temple.Component
__ENV__
end
def evaluate_template(template, assigns \\ %{}) do
template
|> EEx.compile_string(engine: Phoenix.HTML.Engine)
|> Code.eval_quoted([assigns: assigns], env())
|> elem(0)
|> Phoenix.HTML.safe_to_string()
end
@doc """
Converts an iolist to a binary and appends a new line.
"""
def iolist_to_binary(iolist) do
iolist
|> :erlang.iolist_to_binary()
|> append_new_line()
end
@doc """
Appends a new line to a string.
"""
def append_new_line(string) do
string <> "\n"
end
end