Packages
surface
0.7.1
0.12.3
0.12.2
0.12.1
0.12.0
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.1.0-alpha.2
0.1.0-alpha.1
0.1.0-alpha.0
A component based library for Phoenix LiveView
Current section
Files
Jump to
Current section
Files
lib/surface/compiler/converter_0_5.ex
defmodule Surface.Compiler.Converter_0_5 do
@moduledoc false
@behaviour Surface.Compiler.Converter
@impl true
def opts() do
[handle_full_node: ["If", "For"]]
end
@impl true
def after_convert_file(ext, content) when ext in [".ex", ".exs"] do
content = Regex.replace(~r/~H("""|\"|\[|\(|\{)/s, content, "~F\\1")
Regex.replace(~r/^(\s*slot[\s|\(].+?,\s*)props:(.+)/m, content, "\\1args:\\2")
end
def after_convert_file(_ext, content) do
content
end
@impl true
def convert(:expr, text, _state, _opts) do
if String.starts_with?(text, "{") and String.ends_with?(text, "}") do
case Regex.run(~r/^{\s*#\s*(.*?)\s*}$/, text) do
[_, comment] -> "!-- #{comment} --"
_ -> text |> String.slice(1..-2) |> maybe_trim_one()
end
else
text
end
end
def convert(:unquoted_string, value, _state, _opts) do
"{#{value}}"
end
def convert(:double_quoted_string, value, _state, _opts) do
new_value = Regex.replace(~r/{{(.+?)}}/s, value, fn _, x -> "\#{#{maybe_trim_one(x)}}" end)
if new_value != value do
"{#{new_value}}"
else
value
end
end
def convert(:tag_open_begin, "<If", _state, _opts) do
"{#if"
end
def convert(:tag_open_end, text, %{tag_open_begin: "<If"}, _opts) do
[_, condition] = Regex.run(~r/condition={{(.+)}}/s, text)
" #{String.trim(condition)}}"
end
def convert(:tag_close, "</If>", _state, _opts) do
"{/if}"
end
def convert(:tag_open_begin, "<For", _state, _opts) do
"{#for"
end
def convert(:tag_open_end, text, %{tag_open_begin: "<For"}, _opts) do
[_, each] = Regex.run(~r/each={{(.+)}}/s, text)
" #{String.trim(each)}}"
end
def convert(:tag_close, "</For>", _state, _opts) do
"{/for}"
end
def convert(:tag_name, "template", _state, _opts) do
"#template"
end
def convert(:tag_name, "slot", _state, _opts) do
"#slot"
end
def convert(:attr_name, ":props", %{tag_name: "slot"}, _opts) do
":args"
end
def convert(:attr_name, "phx_feedback_for", %{tag_name: "ErrorTag"}, _opts) do
"feedback_for"
end
def convert(_type, text, _state, _opts) do
text
end
defp maybe_trim_one(original_text) do
text = String.replace_prefix(original_text, " ", "")
# don't remove the last space if the closing `}` is at a new line
# containing only spaces (indentation)
if Regex.match?(~r/\n\s*$/, text) do
text
else
String.replace_suffix(text, " ", "")
end
end
end