Skip to content

Instantly share code, notes, and snippets.

@elixirplayground
Created January 11, 2018 03:15
Show Gist options
  • Save elixirplayground/9a02cbaa828ce359d264637c87f4f070 to your computer and use it in GitHub Desktop.
Save elixirplayground/9a02cbaa828ce359d264637c87f4f070 to your computer and use it in GitHub Desktop.
elixirplayground.com code share
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