Guest User

Untitled

a guest
Jan 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. defmodule Example do
  2.  
  3. def parse(elements, acc \\ [])
  4. def parse([], acc), do: Enum.reverse(acc)
  5.  
  6. def parse([{"table", attrs, nested} | rest], acc) do
  7. parse(rest, [{"v-table", attrs, parse(nested)} | acc])
  8. end
  9.  
  10. def parse([{"thead", _attrs, nested} | rest], acc) do
  11. parse(rest, parse(nested) ++ acc)
  12. end
  13.  
  14. def parse([{"tbody", _attrs, nested} | rest], acc) do
  15. parse(rest, parse(nested) ++ acc)
  16. end
  17.  
  18. def parse([{"tr", _attrs, nested} | rest], acc) do
  19. parse(rest, [parse(nested) | acc])
  20. end
  21.  
  22. def parse([{"th", _attrs, [content]} | rest], acc) do
  23. parse(rest, [content | acc])
  24. end
  25.  
  26. def parse([{"td", _attrs, [content]} | rest], acc) do
  27. parse(rest, [content | acc])
  28. end
  29.  
  30. def test do
  31.  
  32. decodedHTML = [{"table", [{"class", "table table-striped"}],
  33. [
  34. {"thead", [],
  35. [
  36. {"tr", [],
  37. [
  38. {"th", [{"scope", "col"}], ["#"]},
  39. {"th", [{"scope", "col"}], ["First"]},
  40. {"th", [{"scope", "col"}], ["Last"]},
  41. {"th", [{"scope", "col"}], ["Handle"]}
  42. ]}
  43. ]},
  44. {"tbody", [],
  45. [
  46. {"tr", [],
  47. [
  48. {"th", [{"scope", "row"}], ["1"]},
  49. {"td", [], ["Mark"]},
  50. {"td", [], ["Otto"]},
  51. {"td", [], ["@mdo"]}
  52. ]},
  53. {"tr", [],
  54. [
  55. {"th", [{"scope", "row"}], ["2"]},
  56. {"td", [], ["Jacob"]},
  57. {"td", [], ["Thornton"]},
  58. {"td", [], ["@fat"]}
  59. ]},
  60. {"tr", [],
  61. [
  62. {"th", [{"scope", "row"}], ["3"]},
  63. {"td", [], ["Larry"]},
  64. {"td", [], ["the Bird"]},
  65. {"td", [], ["@twitter"]}
  66. ]}
  67. ]}
  68. ]}]
  69. IO.inspect parse(decodedHTML)
  70. end
  71.  
  72. end
  73.  
  74. Example.test
Add Comment
Please, Sign In to add comment