Allow dashes in domain name search
[akkoma] / lib / pleroma / xml_builder.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.XmlBuilder do
6 def to_xml({tag, attributes, content}) do
7 open_tag = make_open_tag(tag, attributes)
8
9 content_xml = to_xml(content)
10
11 "<#{open_tag}>#{content_xml}</#{tag}>"
12 end
13
14 def to_xml({tag, %{} = attributes}) do
15 open_tag = make_open_tag(tag, attributes)
16
17 "<#{open_tag} />"
18 end
19
20 def to_xml({tag, content}), do: to_xml({tag, %{}, content})
21
22 def to_xml(content) when is_binary(content) do
23 to_string(content)
24 end
25
26 def to_xml(content) when is_list(content) do
27 for element <- content do
28 to_xml(element)
29 end
30 |> Enum.join()
31 end
32
33 def to_xml(%NaiveDateTime{} = time) do
34 NaiveDateTime.to_iso8601(time)
35 end
36
37 def to_doc(content), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(content)
38
39 defp make_open_tag(tag, attributes) do
40 attributes_string =
41 for {attribute, value} <- attributes do
42 value = String.replace(value, "\"", "&quot;")
43 "#{attribute}=\"#{value}\""
44 end
45 |> Enum.join(" ")
46
47 [tag, attributes_string] |> Enum.join(" ") |> String.trim()
48 end
49 end