Skip to content

Instantly share code, notes, and snippets.

@hackervera
Last active August 29, 2015 14:24
Show Gist options
  • Save hackervera/29878e454870fb7eb1aa to your computer and use it in GitHub Desktop.
Save hackervera/29878e454870fb7eb1aa to your computer and use it in GitHub Desktop.
@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
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