Created
January 11, 2018 03:26
-
-
Save elixirplayground/22e746c0a01b1f6db58dfa504bce20d1 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_table(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 parse_table(nested) do | |
[[header], body] = parse(nested) | |
IO.inspect body | |
Enum.map(body, &(Enum.zip(header, &1) |> Enum.into(%{}))) | |
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