summaryrefslogtreecommitdiff
path: root/lib/tmpl.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tmpl.ex')
-rw-r--r--lib/tmpl.ex42
1 files changed, 31 insertions, 11 deletions
diff --git a/lib/tmpl.ex b/lib/tmpl.ex
index e4489ac..881cbc8 100644
--- a/lib/tmpl.ex
+++ b/lib/tmpl.ex
@@ -24,10 +24,26 @@ defmodule Tmpl do
end
end
- @colors [:white, :black, :blue, :green, :red, :brown, :purple, :orange, :yellow, :light_green, :cyan, :light_blue, :pink, :grey, :light_grey]
+ @colors [
+ :white,
+ :black,
+ :blue,
+ :green,
+ :red,
+ :brown,
+ :purple,
+ :orange,
+ :yellow,
+ :light_green,
+ :cyan,
+ :light_blue,
+ :pink,
+ :grey,
+ :light_grey
+ ]
for {color, index} <- Enum.with_index(@colors) do
- code = 48+index
+ code = 48 + index
def color_code(unquote(color)) do
unquote(code)
@@ -42,7 +58,9 @@ defmodule Tmpl do
end
end
- def account_nick(%{"id" => id, "name" => name}, %{variables: %{"message" => %{"network" => network}}}) do
+ def account_nick(%{"id" => id, "name" => name}, %{
+ variables: %{"message" => %{"network" => network}}
+ }) do
if user = Nola.UserTrack.find_by_account(network, %Nola.Account{id: id}) do
user.nick
else
@@ -53,7 +71,6 @@ defmodule Tmpl do
def account_nick(val, ctx) do
"{{account_nick}}"
end
-
end
def render(template, msg = %Nola.Message{}, context \\ %{}, safe \\ true) do
@@ -64,20 +81,23 @@ defmodule Tmpl do
case Liquex.parse(template) do
{:ok, template_ast} ->
do_render(template_ast, context, safe)
+
{:error, err, pos} ->
- Logger.debug("Liquid error: #{pos} - #{inspect template}")
- "[liquid ast error (at #{pos}): #{inspect err}]"
+ Logger.debug("Liquid error: #{pos} - #{inspect(template)}")
+ "[liquid ast error (at #{pos}): #{inspect(err)}]"
end
end
defp do_render(template_ast, context, safe) when is_list(template_ast) do
- context = Liquex.Context.new(mapify(context, safe))
- |> Map.put(:filter_module, Tmpl.Filter)
+ context =
+ Liquex.Context.new(mapify(context, safe))
+ |> Map.put(:filter_module, Tmpl.Filter)
+
{content, _context} = Liquex.render(template_ast, context)
to_string(content)
rescue
e ->
- Logger.error("Liquid error: #{inspect e}")
+ Logger.error("Liquid error: #{inspect(e)}")
"[liquid rendering error]"
end
@@ -87,8 +107,9 @@ defmodule Tmpl do
defp mapify(map = %{}, safe) do
map
- |> Enum.reduce(Map.new, fn({k,v}, acc) ->
+ |> Enum.reduce(Map.new(), fn {k, v}, acc ->
k = to_string(k)
+
if safe?(k, safe) do
if v = mapify(v, safe) do
Map.put(acc, k, v)
@@ -121,4 +142,3 @@ defmodule Tmpl do
defp safe?("password", true), do: false
defp safe?(_, true), do: true
end
-