Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Last active May 30, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wojtekmach/a1c1648b8015dbb67d35b89f2e743266 to your computer and use it in GitHub Desktop.
Save wojtekmach/a1c1648b8015dbb67d35b89f2e743266 to your computer and use it in GitHub Desktop.
defmodule PlaygroundTest do
use ExUnit.Case, async: true
test "playground" do
source = """
<p><%= bar( 1, 2 ) %></p>
"""
IO.puts "before:"
IO.puts source
IO.puts "after:"
IO.puts format_eex!(source)
end
defp format_eex!(source) do
source
|> tokenize!()
|> format!()
|> to_source()
end
defp tokenize!(source) do
{:ok, tokens} = EEx.Tokenizer.tokenize(source, 1, 1, %{indentation: 4, trim: false})
tokens
end
defp format!(tokens) do
Enum.map(tokens, fn
{:expr, line, column, marker, content} ->
content = content |> List.to_string() |> Code.format_string!()
content = [" ", content, " "] |> IO.iodata_to_binary() |> String.to_charlist()
{:expr, line, column, marker, content}
other ->
other
end)
end
defp to_source(tokens) do
Enum.map(tokens, fn
{:expr, _line, _column, marker, content} ->
["<%", marker, content, "%>"]
{:text, content} ->
content
{:eof, _, _} ->
""
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment