Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
Split Mustache.Compiler and Mustache.Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Ito committed Jul 14, 2013
1 parent 5e2a9c6 commit ccc8aa8
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 120 deletions.
139 changes: 19 additions & 120 deletions lib/mustache/compiler.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Mustache.Compiler do
import Kernel, except: [to_binary: 1]

def compile(source, options) do
line = options[:line] || 1
Expand Down Expand Up @@ -189,7 +188,7 @@ defmodule Mustache.Compiler do

if is_function(var, 0), do: var = var.()

unquote(buffer) <> Mustache.Compiler.escape_html(Mustache.Compiler.to_binary(var))
unquote(buffer) <> Mustache.Utils.escape_html(Mustache.Utils.to_binary(var))
end
end

Expand All @@ -199,27 +198,27 @@ defmodule Mustache.Compiler do

if is_function(var, 0), do: var = var.()

unquote(buffer) <> Mustache.Compiler.to_binary(var)
unquote(buffer) <> Mustache.Utils.to_binary(var)
end
end

def handle_dotted_name(buffer, var, atoms) do
defp handle_dotted_name(buffer, var, atoms) do
quote do
var = adding = Mustache.Compiler.recur_access(unquote(var), unquote(atoms))
var = adding = Mustache.Utils.recur_access(unquote(var), unquote(atoms))

if is_function(var, 0), do: var = var.()

unquote(buffer) <> Mustache.Compiler.escape_html(Mustache.Compiler.to_binary(var))
unquote(buffer) <> Mustache.Utils.escape_html(Mustache.Utils.to_binary(var))
end
end

def handle_unescaped_dotted_name(buffer, var, atoms) do
defp handle_unescaped_dotted_name(buffer, var, atoms) do
quote do
var = Mustache.Compiler.recur_access(unquote(var), unquote(atoms))
var = Mustache.Utils.recur_access(unquote(var), unquote(atoms))

if is_function(var, 0), do: var = var.()

unquote(buffer) <> Mustache.Compiler.to_binary(var)
unquote(buffer) <> Mustache.Utils.to_binary(var)
end
end

Expand All @@ -231,7 +230,7 @@ defmodule Mustache.Compiler do
var = unquote(var)
vars = unquote(vars)
fun = unquote(fun)
coll = Mustache.Compiler.to_coll(var, vars, binding)
coll = Mustache.Utils.to_coll(var, vars, binding)
Enum.map(coll, fun) |> Enum.join
end
end
Expand All @@ -244,9 +243,9 @@ defmodule Mustache.Compiler do
var = unquote(var)
vars = unquote(vars)
fun = unquote(fun)
coll = Mustache.Compiler.to_coll(var, vars, binding)
coll = Mustache.Utils.to_coll(var, vars, binding)
case coll do
[] -> fun.(Mustache.Compiler.to_nilcoll(vars, binding))
[] -> fun.(Mustache.Utils.to_nilcoll(vars, binding))
_ -> ""
end
end
Expand All @@ -259,7 +258,7 @@ defmodule Mustache.Compiler do
quote do
var = unquote(var)
fun = unquote(fun)
coll = Mustache.Compiler.to_coll_for_dot(var)
coll = Mustache.Utils.to_coll_for_dot(var)
Enum.map(coll, fun) |> Enum.join
end
end
Expand All @@ -274,10 +273,10 @@ defmodule Mustache.Compiler do
real_vars = Enum.map vars, fn(atom) -> { atom, [], nil} end
fun = {:fn,[],[[do: {:->,[],[{[real_vars],[],expr}]}]]}
quote do
var = Mustache.Compiler.recur_access_for_dotted(unquote(top_var), unquote(atoms))
var = Mustache.Utils.recur_access_for_dotted(unquote(top_var), unquote(atoms))
vars = unquote(vars)
fun = unquote(fun)
coll = Mustache.Compiler.to_coll(var, vars, binding)
coll = Mustache.Utils.to_coll(var, vars, binding)
Enum.map(coll, fun) |> Enum.join
end
end
Expand All @@ -288,12 +287,12 @@ defmodule Mustache.Compiler do
real_vars = Enum.map vars, fn(atom) -> { atom, [], nil} end
fun = {:fn,[],[[do: {:->,[],[{[real_vars],[],expr}]}]]}
quote do
var = Mustache.Compiler.recur_access_for_dotted(unquote(top_var), unquote(atoms))
var = Mustache.Utils.recur_access_for_dotted(unquote(top_var), unquote(atoms))
vars = unquote(vars)
fun = unquote(fun)
coll = Mustache.Compiler.to_coll(var, vars, binding)
coll = Mustache.Utils.to_coll(var, vars, binding)
case coll do
[] -> fun.(Mustache.Compiler.to_nilcoll(vars, binding))
[] -> fun.(Mustache.Utils.to_nilcoll(vars, binding))
_ -> ""
end
end
Expand All @@ -304,114 +303,14 @@ defmodule Mustache.Compiler do
real_vars = [{ :., [], nil}]
fun = {:fn,[],[[do: {:->,[],[{[real_vars],[],expr}]}]]}
quote do
var = Mustache.Compiler.recur_access_for_dotted(unquote(top_var), unquote(atoms))
var = Mustache.Utils.recur_access_for_dotted(unquote(top_var), unquote(atoms))
fun = unquote(fun)
coll = Mustache.Compiler.to_coll_for_dot(var)
coll = Mustache.Utils.to_coll_for_dot(var)
Enum.map(coll, fun) |> Enum.join
end
end

defp handle_dotted_inverted_expr_including_dot(_expr, _atom, _atoms) do
quote do: ""
end

# utils

def to_coll(term, vars, bind) when is_list(term) do
cond do
term == [] ->
[]
is_keyword?(term) ->
[Enum.map(vars, fn(x) -> term[x] || bind[x] end)]
true ->
Enum.map term, fn(elem) ->
if is_keyword?(elem) do
Enum.map(vars, fn(x) -> elem[x] || bind[x] end)
else
to_nilcoll(vars, bind)
end
end
end
end

def to_coll(term, _vars, _bind) when term == nil or term == false, do: []
def to_coll(_term, vars, bind), do: [to_nilcoll(vars, bind)]

def to_nilcoll(vars, bind), do: Enum.map(vars, bind[&1])

defp is_keyword?(list) when is_list(list), do: :lists.all(is_keyword_tuple?(&1), list)
defp is_keyword?(_), do: false

defp is_keyword_tuple?({ x, _ }) when is_atom(x), do: true
defp is_keyword_tuple?(_), do: false

def to_coll_for_dot(term) when is_list(term), do: Enum.map(term, [&1])
def to_col_for_dot(term), do: [[term]]

def to_binary(float) when is_float(float) do
bin = round(float * 100000000000000) |> Kernel.to_binary
{ integer, decimal } = split_float(bin)
Kernel.to_binary([integer, ".", decimal])
end
def to_binary(other), do: Kernel.to_binary(other)

defp split_float(bin) do
binary_to_list(bin)
|> :lists.reverse
|> split_float(0, '')
end

defp split_float(list, 14, '') do
{ :lists.reverse(list), '0' }
end

defp split_float(list, 14, acc) do
{ :lists.reverse(list), acc }
end

defp split_float([?0|t], i, acc) do
split_float(t, i + 1, acc)
end

defp split_float([h|t], i, acc) do
split_float(t, i + 1, [h|acc])
end

def recur_access(term, []), do: term
def recur_access(term, [atom|t]) do
if is_keyword?(term), do: recur_access(term[atom], t), else: []
end

def recur_access_for_dotted(term, []), do: term
def recur_access_for_dotted(term, [atom|t]) do
if is_keyword?(term), do: recur_access(term[atom], t), else: []
end

def escape_html(str) do
escape_html(:unicode.characters_to_list(str), [])
|> Enum.reverse
|> to_binary
end

@table_for_escape_html [
{ '\'', '&#39;' },
{ '&', '&amp;' },
{ '"', '&quot;' },
{ '<', '&lt;' },
{ '>', '&gt;' },
]

lc { k, v } inlist @table_for_escape_html do
defp escape_html(unquote(k) ++ t, acc) do
escape_html(t, unquote(Enum.reverse(v)) ++ acc)
end
end

defp escape_html([h|t], acc) do
escape_html(t, [h|acc])
end

defp escape_html([], acc) do
acc
end
end
101 changes: 101 additions & 0 deletions lib/mustache/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
defmodule Mustache.Utils do
import Kernel, except: [to_binary: 1]

def to_coll(term, vars, bind) when is_list(term) do
cond do
term == [] ->
[]
is_keyword?(term) ->
[Enum.map(vars, fn(x) -> term[x] || bind[x] end)]
true ->
Enum.map term, fn(elem) ->
if is_keyword?(elem) do
Enum.map(vars, fn(x) -> elem[x] || bind[x] end)
else
to_nilcoll(vars, bind)
end
end
end
end

def to_coll(term, _vars, _bind) when term == nil or term == false, do: []
def to_coll(_term, vars, bind), do: [to_nilcoll(vars, bind)]

def to_nilcoll(vars, bind), do: Enum.map(vars, bind[&1])

defp is_keyword?(list) when is_list(list), do: :lists.all(is_keyword_tuple?(&1), list)
defp is_keyword?(_), do: false

defp is_keyword_tuple?({ x, _ }) when is_atom(x), do: true
defp is_keyword_tuple?(_), do: false

def to_coll_for_dot(term) when is_list(term), do: Enum.map(term, [&1])
def to_col_for_dot(term), do: [[term]]

def to_binary(float) when is_float(float) do
bin = round(float * 100000000000000) |> Kernel.to_binary
{ integer, decimal } = split_float(bin)
Kernel.to_binary([integer, ".", decimal])
end
def to_binary(other), do: Kernel.to_binary(other)

defp split_float(bin) do
binary_to_list(bin)
|> :lists.reverse
|> split_float(0, '')
end

defp split_float(list, 14, '') do
{ :lists.reverse(list), '0' }
end

defp split_float(list, 14, acc) do
{ :lists.reverse(list), acc }
end

defp split_float([?0|t], i, acc) do
split_float(t, i + 1, acc)
end

defp split_float([h|t], i, acc) do
split_float(t, i + 1, [h|acc])
end

def recur_access(term, []), do: term
def recur_access(term, [atom|t]) do
if is_keyword?(term), do: recur_access(term[atom], t), else: []
end

def recur_access_for_dotted(term, []), do: term
def recur_access_for_dotted(term, [atom|t]) do
if is_keyword?(term), do: recur_access(term[atom], t), else: []
end

def escape_html(str) do
escape_html(:unicode.characters_to_list(str), [])
|> Enum.reverse
|> to_binary
end

@table_for_escape_html [
{ '\'', '&#39;' },
{ '&', '&amp;' },
{ '"', '&quot;' },
{ '<', '&lt;' },
{ '>', '&gt;' },
]

lc { k, v } inlist @table_for_escape_html do
defp escape_html(unquote(k) ++ t, acc) do
escape_html(t, unquote(Enum.reverse(v)) ++ acc)
end
end

defp escape_html([h|t], acc) do
escape_html(t, [h|acc])
end

defp escape_html([], acc) do
acc
end
end

0 comments on commit ccc8aa8

Please sign in to comment.