Last active
August 29, 2015 14:24
-
-
Save hackervera/29878e454870fb7eb1aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| @doc """ | |
| Convert a floki tree to html again | |
| """ | |
| def tag_parser(list) do | |
| {tag, options, children} = list | |
| formatted_options = options |> Enum.map(fn option -> | |
| {key, value} = option | |
| "#{key}='#{value}'" | |
| end) |> Enum.join(" ") | |
| formatted_kids = children |> Enum.map(fn child -> | |
| if is_tuple child do | |
| tag_parser child | |
| else | |
| child |> String.replace "\n", "" | |
| end | |
| end) |> Enum.join("") |> String.strip | |
| "<#{tag} #{formatted_options}>#{formatted_kids}</#{tag}>" | |
| end |
This file contains hidden or 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
| iex(23)> foo = "<p><a href='http://google.com'><span class='foo'>Yay</span></a></p>" | |
| "<p><a href='http://google.com'><span class='foo'>Yay</span></a></p>" | |
| iex(24)> tree = Floki.parse foo | |
| {"p", [], | |
| [{"a", [{"href", "http://google.com"}], | |
| [{"span", [{"class", "foo"}], ["Yay"]}]}]} | |
| iex(25)> Webmention.tag_parser tree | |
| "<p ><a href='http://google.com'><span class='foo'>Yay</span></a></p>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment