Skip to content

Commit

Permalink
Merge pull request #1 from pylon/section-interpolation
Browse files Browse the repository at this point in the history
Support normal and inverse section interpolation
  • Loading branch information
space-pope authored Oct 5, 2017
2 parents cd774cc + 4edbb20 commit fb95395
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
43 changes: 42 additions & 1 deletion lib/mustache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,41 @@ defmodule Mustache do
end
end

defp process_section(template, data) do
matches = Regex.run(section_regex(), template)
case matches do
nil -> template
[full, predicate, var, body] ->
val = indifferent_access(data, var)
section_val = case predicate do
"#" -> process_if(body, val)
"^" -> process_unless(body, val)
end
process_section(String.replace(template, full, section_val), data)
end
end

defp process_if(template, val) do
case val do
nil -> ""
false -> ""
[] -> ""
[_ | _] -> val
|> Stream.map(&(render(template, &1)))
|> Enum.join()
val -> render(template, val)
end
end

defp process_unless(template, val) do
case val do
nil -> render(template, val)
false -> render(template, val)
[] -> render(template, val)
_val -> ""
end
end

defp indifferent_access(map, string_key) do
map[string_key] || map[string_key |> String.to_atom]
end
Expand Down Expand Up @@ -86,6 +121,10 @@ defmodule Mustache do
regex("{{{\\s*", "\\s*}}}")
end

defp section_regex do
~r<{{\s*(#|\^)\s*([\w.]+)\s*}}(.*?){{\s*/\s*\2\s*}}>
end

defp regex(otag, ctag, body \\ "\\w+") do
Regex.compile!("#{otag}#{body}#{ctag}")
end
Expand All @@ -108,7 +147,9 @@ defmodule Mustache do
end

defp strategies do
[{ fn(template) -> Regex.match?(triple_regex(), template) end,
[{ fn(template) -> Regex.match?(section_regex(), template) end,
fn(template, data) -> process_section(template, data) end },
{ fn(template) -> Regex.match?(triple_regex(), template) end,
fn(template, data) -> triple_mustaches(template, data) end},
{ fn(template) -> Regex.match?(regex("{{", "}}", "\\w+\\.\\w+"), template) end,
fn(template, data) -> scan_for_dot(template, data) end },
Expand Down
20 changes: 18 additions & 2 deletions test/mustache_feature_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,28 @@ defmodule MustacheFeatureTest do
%{person: %{name: "Joe"}}) == "\"Joe\" == \"Joe\""
end

@tag :pending
test "Dotted Names - Basic Interpolation" do
#Sections

test "Section Interpolation" do
assert Mustache.render("\"{{person.name}}\" == \"{{#person}}{{name}}{{/person}}\"",
%{person: %{name: "Joe"}}) == "\"Joe\" == \"Joe\""
end

test "Section Interpolation - Inverse" do
assert Mustache.render("\"{{person.name}}\" == \"{{^person}}{{name}}{{/person}}\"",
%{person: %{name: "Joe"}}) == "\"Joe\" == \"\""
end

test "Section Interpolation - Basic and Inverse" do
assert Mustache.render("\"{{person.name}}\" == \"{{#person_2}}{{name}}{{/person_2}}{{^person_2}}Joe{{/person_2}}\"",
%{person: %{name: "Joe"}}) == "\"Joe\" == \"Joe\""
end

test "Section Interpolation - list values" do
assert Mustache.render("\"{{#people}}{{name}} {{/people}}\"",
%{people: [%{name: "Joe"}, %{name: "Jill"}]}) == "\"Joe Jill \""
end

#Whitespace sensitivity

test "Interpolation - Surrounding Whitespace" do
Expand Down

0 comments on commit fb95395

Please sign in to comment.