You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
748 B

  1. defmodule Microformats2 do
  2. if Code.ensure_loaded?(HTTPotion) do
  3. def parse(url) do
  4. response = HTTPotion.get(url, follow_redirects: true)
  5. if HTTPotion.Response.success?(response) do
  6. parse(response.body, url)
  7. else
  8. :error
  9. end
  10. end
  11. end
  12. def parse(content, url) when is_binary(content), do: parse(Floki.parse(content), url)
  13. def parse(content, url) do
  14. doc =
  15. content
  16. |> Floki.filter_out("template")
  17. |> Floki.filter_out("style")
  18. |> Floki.filter_out("script")
  19. |> Floki.filter_out(:comment)
  20. rels = Microformats2.Rels.parse(doc, url)
  21. items = Microformats2.Items.parse(doc, doc, url)
  22. %{items: items, rels: rels[:rels], rel_urls: rels[:rel_urls]}
  23. end
  24. end