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

View File

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