Code review

* <https://ilja.space/notice/ALpd6nux5hT2nsfetM>
    * This commit: `|> fill_props(token) after the cond` in the parser
    * TODO: Don't use intention-specific data in the tokens (e.g. left is reverse, x is mfm-spinX)
* <https://ilja.space/notice/ALpcK6W59UjkIUofU8>
    * TODO: Use less files
* This commit: Change nested if-statement in mfm.ex to `cond do`
This commit is contained in:
Ilja 2022-07-25 01:59:41 +02:00
parent 0a2894f07a
commit ecd3f750cb
2 changed files with 52 additions and 50 deletions

View file

@ -63,22 +63,23 @@ defmodule MfmParser.Parser do
defp get_node(token = %{content: content}) do defp get_node(token = %{content: content}) do
cond do cond do
content =~ "$[flip" -> %Node.MFM.Flip{} |> fill_props(token) content =~ "$[flip" -> %Node.MFM.Flip{}
content =~ "$[font" -> %Node.MFM.Font{} |> fill_props(token) content =~ "$[font" -> %Node.MFM.Font{}
content =~ "$[x" -> %Node.MFM.X{} |> fill_props(token) content =~ "$[x" -> %Node.MFM.X{}
content =~ "$[blur" -> %Node.MFM.Blur{} |> fill_props(token) content =~ "$[blur" -> %Node.MFM.Blur{}
content =~ "$[jelly" -> %Node.MFM.Jelly{} |> fill_props(token) content =~ "$[jelly" -> %Node.MFM.Jelly{}
content =~ "$[tada" -> %Node.MFM.Tada{} |> fill_props(token) content =~ "$[tada" -> %Node.MFM.Tada{}
content =~ "$[jump" -> %Node.MFM.Jump{} |> fill_props(token) content =~ "$[jump" -> %Node.MFM.Jump{}
content =~ "$[bounce" -> %Node.MFM.Bounce{} |> fill_props(token) content =~ "$[bounce" -> %Node.MFM.Bounce{}
content =~ "$[spin" -> %Node.MFM.Spin{} |> fill_props(token) content =~ "$[spin" -> %Node.MFM.Spin{}
content =~ "$[shake" -> %Node.MFM.Shake{} |> fill_props(token) content =~ "$[shake" -> %Node.MFM.Shake{}
content =~ "$[twitch" -> %Node.MFM.Twitch{} |> fill_props(token) content =~ "$[twitch" -> %Node.MFM.Twitch{}
content =~ "$[rainbow" -> %Node.MFM.Rainbow{} |> fill_props(token) content =~ "$[rainbow" -> %Node.MFM.Rainbow{}
content =~ "$[sparkle" -> %Node.MFM.Sparkle{} |> fill_props(token) content =~ "$[sparkle" -> %Node.MFM.Sparkle{}
content =~ "$[rotate" -> %Node.MFM.Rotate{} |> fill_props(token) content =~ "$[rotate" -> %Node.MFM.Rotate{}
true -> %Node.MFM.Undefined{} |> fill_props(token) true -> %Node.MFM.Undefined{}
end end
|> fill_props(token)
end end
defp fill_props(node = %{props: props}, %{content: content}) do defp fill_props(node = %{props: props}, %{content: content}) do

View file

@ -1,45 +1,46 @@
defmodule MfmParser.Token.MFM do defmodule MfmParser.Token.MFM do
def to_props(opts_string) when is_binary(opts_string) do def to_props(opts_string) when is_binary(opts_string) do
if opts_string =~ "." do cond do
Regex.replace(~r/^.*?\./u, opts_string, "") opts_string =~ "." ->
|> String.trim() Regex.replace(~r/^.*?\./u, opts_string, "")
|> String.split(",") |> String.trim()
|> Enum.reduce(%{}, fn opt, acc -> |> String.split(",")
acc |> Enum.reduce(%{}, fn opt, acc ->
|> Map.merge( acc
cond do |> Map.merge(
opt =~ "speed" -> cond do
%{speed: String.replace(opt, "speed=", "")} opt =~ "speed" ->
%{speed: String.replace(opt, "speed=", "")}
opt =~ "v" -> opt =~ "v" ->
%{v: true} %{v: true}
opt =~ "h" -> opt =~ "h" ->
%{h: true} %{h: true}
opt =~ "x" -> opt =~ "x" ->
%{keyframes_name: "mfm-spinX"} %{axis: "x"}
opt =~ "y" -> opt =~ "y" ->
%{keyframes_name: "mfm-spinY"} %{axis: "y"}
opt =~ "left" -> opt =~ "left" ->
%{direction: "reverse"} %{direction: "left"}
opt =~ "alternate" -> opt =~ "alternate" ->
%{direction: "alternate"} %{direction: "alternate"}
true -> true ->
if Regex.match?(~r/^\$\[font/, opts_string) do if Regex.match?(~r/^\$\[font/, opts_string) do
%{font: opt} %{font: opt}
else else
%{} %{}
end end
end end
) )
end) end)
else
if opts_string =~ "$[x" do opts_string =~ "$[x" ->
%{ %{
size: size:
case opts_string |> String.replace("$[x", "") |> String.trim() do case opts_string |> String.replace("$[x", "") |> String.trim() do
@ -49,9 +50,9 @@ defmodule MfmParser.Token.MFM do
_ -> "100%" _ -> "100%"
end end
} }
else
true ->
%{} %{}
end
end end
end end
end end