-
-
Save wojtekmach/a1c1648b8015dbb67d35b89f2e743266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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