1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.XmlBuilder do
6 def to_xml({tag, attributes, content}) do
7 open_tag = make_open_tag(tag, attributes)
9 content_xml = to_xml(content)
11 "<#{open_tag}>#{content_xml}</#{tag}>"
14 def to_xml({tag, %{} = attributes}) do
15 open_tag = make_open_tag(tag, attributes)
20 def to_xml({tag, content}), do: to_xml({tag, %{}, content})
22 def to_xml(content) when is_binary(content) do
26 def to_xml(content) when is_list(content) do
27 for element <- content do
33 def to_xml(%NaiveDateTime{} = time) do
34 NaiveDateTime.to_iso8601(time)
37 def to_doc(content), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(content)
39 defp make_open_tag(tag, attributes) do
41 for {attribute, value} <- attributes do
42 value = String.replace(value, "\"", """)
43 "#{attribute}=\"#{value}\""
47 [tag, attributes_string] |> Enum.join(" ") |> String.trim()