Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
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.

45 lines
1.0KB

  1. defmodule Pleroma.XmlBuilder do
  2. def to_xml({tag, attributes, content}) do
  3. open_tag = make_open_tag(tag, attributes)
  4. content_xml = to_xml(content)
  5. "<#{open_tag}>#{content_xml}</#{tag}>"
  6. end
  7. def to_xml({tag, %{} = attributes}) do
  8. open_tag = make_open_tag(tag, attributes)
  9. "<#{open_tag} />"
  10. end
  11. def to_xml({tag, content}), do: to_xml({tag, %{}, content})
  12. def to_xml(content) when is_binary(content) do
  13. to_string(content)
  14. end
  15. def to_xml(content) when is_list(content) do
  16. for element <- content do
  17. to_xml(element)
  18. end
  19. |> Enum.join()
  20. end
  21. def to_xml(%NaiveDateTime{} = time) do
  22. NaiveDateTime.to_iso8601(time)
  23. end
  24. def to_doc(content), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(content)
  25. defp make_open_tag(tag, attributes) do
  26. attributes_string =
  27. for {attribute, value} <- attributes do
  28. "#{attribute}=\"#{value}\""
  29. end
  30. |> Enum.join(" ")
  31. [tag, attributes_string] |> Enum.join(" ") |> String.trim()
  32. end
  33. end