Created
January 11, 2018 03:13
-
-
Save elixirplayground/4eeba919d913fa3f834a4049ac8fa247 to your computer and use it in GitHub Desktop.
elixirplayground.com code share
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 Example do | |
def parse(elements, acc \\ []) | |
def parse([], acc), do: Enum.reverse(acc) | |
def parse([{"table", attrs, nested} | rest], acc) do | |
parse(rest, [{"v-table", attrs, parse(nested)} | acc]) | |
end | |
def parse([{"thead", _attrs, nested} | rest], acc) do | |
parse(rest, parse(nested) ++ acc) | |
end | |
def parse([{"tbody", _attrs, nested} | rest], acc) do | |
parse(rest, parse(nested) ++ acc) | |
end | |
def parse([{"tr", _attrs, nested} | rest], acc) do | |
parse(rest, [parse(nested) | acc]) | |
end | |
def parse([{"th", _attrs, [content]} | rest], acc) do | |
parse(rest, [content | acc]) | |
end | |
def parse([{"td", _attrs, [content]} | rest], acc) do | |
parse(rest, [content | acc]) | |
end | |
def test do | |
decodedHTML = [{"table", [{"class", "table table-striped"}], | |
[ | |
{"thead", [], | |
[ | |
{"tr", [], | |
[ | |
{"th", [{"scope", "col"}], ["#"]}, | |
{"th", [{"scope", "col"}], ["First"]}, | |
{"th", [{"scope", "col"}], ["Last"]}, | |
{"th", [{"scope", "col"}], ["Handle"]} | |
]} | |
]}, | |
{"tbody", [], | |
[ | |
{"tr", [], | |
[ | |
{"th", [{"scope", "row"}], ["1"]}, | |
{"td", [], ["Mark"]}, | |
{"td", [], ["Otto"]}, | |
{"td", [], ["@mdo"]} | |
]}, | |
{"tr", [], | |
[ | |
{"th", [{"scope", "row"}], ["2"]}, | |
{"td", [], ["Jacob"]}, | |
{"td", [], ["Thornton"]}, | |
{"td", [], ["@fat"]} | |
]}, | |
{"tr", [], | |
[ | |
{"th", [{"scope", "row"}], ["3"]}, | |
{"td", [], ["Larry"]}, | |
{"td", [], ["the Bird"]}, | |
{"td", [], ["@twitter"]} | |
]} | |
]} | |
]}] | |
IO.inspect parse(decodedHTML) | |
end | |
end | |
Example.test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment