Skip to content

Commit

Permalink
render comprehension html
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Jan 25, 2024
1 parent 124cd4c commit 0f6dfa9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
49 changes: 45 additions & 4 deletions lib/beacon/template/heex/json_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,55 @@ defmodule Beacon.Template.HEEx.JSONEncoder do
end
end

defp render_comprehension_block(_site, _assigns, {:eex_block, _arg, _content}) do
"TODO"
defp render_comprehension_block(site, assigns, {:eex_block, arg, nodes}) do
arg = ["<%= ", arg, " %>", "\n"]

template =
Enum.reduce(nodes, [arg], fn node, acc ->
text = [extract_eex_block_node_text(node) | " \n "]
[text | acc]
end)
|> Enum.reverse()
|> List.to_string()

Beacon.Template.HEEx.render(site, template, assigns)
end

defp extract_eex_block_node_text("end" = _value), do: ["<% end %>"]

defp extract_eex_block_node_text(value) when is_binary(value), do: value

defp extract_eex_block_node_text({:eex, expr, %{opt: ~c"="}}) do
["<%= ", expr, " %>"]
end

defp extract_eex_block_node_text({:eex, expr, _}) do
["<% ", expr, " %>"]
end

defp extract_eex_block_node_text({:eex_block, expr, children}) do
["<%= ", expr, " %>", extract_eex_block_node_text(children)]
end

defp extract_eex_block_node_text({:tag_block, tag, _, children, _}) do
["<", tag, ">", extract_eex_block_node_text(children), "</", tag, ">"]
end

defp extract_eex_block_content(_content) do
"TODO"
defp extract_eex_block_node_text(value) when is_tuple(value) do
value
|> Tuple.to_list()
|> Enum.reduce([], fn node, acc -> [extract_eex_block_node_text(node) | acc] end)
|> Enum.reverse()
end

defp extract_eex_block_node_text(value) when is_list(value) do
value
|> Enum.reduce([], fn node, acc -> [extract_eex_block_node_text(node) | acc] end)
|> Enum.reverse()
end

defp extract_eex_block_node_text(_value), do: []

def encode_eex_block(value) when is_tuple(value) do
value
|> Tuple.to_list()
Expand Down
3 changes: 2 additions & 1 deletion test/beacon/template/heex/json_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ defmodule Beacon.Template.HEEx.JSONEncoderTest do
%{
"arg" => "for employee <- @beacon_live_data[:employees] do",
"tag" => "eex_block",
"rendered_html" => "TODO",
"rendered_html" =>
"\n<!-- start -->CEO\n <div>\n\n\n <span>José</span>\n\n\n\n\n </div>\n\n<!-- start -->Manager\n <div>\n\n\n\n\n\n </div>\n",
"ast" =>
"[[[{\"type\":\"html_comment\",\"content\":[{\"type\":\"text\",\"content\":[\"<!-- start -->\",{}]}]},{\"type\":\"eex\",\"content\":[\"employee.position\",{\"line\":4,\"opt\":[61],\"column\":11}]},{\"type\":\"text\",\"content\":[\"\\n \",{\"newlines\":1}]},{\"type\":\"tag_block\",\"content\":[\"div\",{\"type\":\"text\",\"content\":[\"\\n \",{\"newlines\":1}]},{\"type\":\"eex_block\",\"content\":[\"for person <- @beacon_live_data[:persons] do\",{\"type\":[[\"text\",\"\\n \",{\"newlines\":1}],[\"eex_block\",\"if person.id == employee.id do\",[{\"type\":[[\"text\",\"\\n \",{\"newlines\":1}],[\"tag_block\",\"span\",[],[{\"type\":\"eex\",\"content\":[\"person.name\",{\"line\":8,\"opt\":[61],\"column\":23}]}],{\"mode\":\"inline\"}],[\"text\",\"\\n \",{\"newlines\":1}]],\"content\":[\"end\"]}]],[\"text\",\"\\n \",{\"newlines\":1}]],\"content\":[\"end\"]}]},{\"type\":\"text\",\"content\":[\"\\n \",{\"newlines\":1}]},{\"mode\":\"block\"}]},{\"type\":\"text\",\"content\":[\"\\n \",{\"newlines\":1}]}],\"end\"]]"
}
Expand Down

0 comments on commit 0f6dfa9

Please sign in to comment.