2018-12-24 12:34:45 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 10:24:04 +00:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/xml_builder.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2020-01-22 21:40:40 +00:00
|
|
|
defmodule Mobilizon.Federation.WebFinger.XmlBuilder do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
2020-01-22 01:14:42 +00:00
|
|
|
Builds XRD for WebFinger host_meta.
|
2018-06-14 16:15:27 +00:00
|
|
|
"""
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
def to_xml({tag, attributes, content}) do
|
|
|
|
open_tag = make_open_tag(tag, attributes)
|
|
|
|
content_xml = to_xml(content)
|
|
|
|
|
|
|
|
"<#{open_tag}>#{content_xml}</#{tag}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_xml({tag, %{} = attributes}) do
|
|
|
|
open_tag = make_open_tag(tag, attributes)
|
|
|
|
|
|
|
|
"<#{open_tag} />"
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_xml({tag, content}), do: to_xml({tag, %{}, content})
|
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
def to_xml(content) when is_binary(content), do: to_string(content)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
def to_xml(content) when is_list(content) do
|
2020-01-22 01:14:42 +00:00
|
|
|
content
|
|
|
|
|> Enum.map(&to_xml/1)
|
2018-05-17 09:32:23 +00:00
|
|
|
|> Enum.join()
|
|
|
|
end
|
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
def to_xml(%NaiveDateTime{} = time), do: NaiveDateTime.to_iso8601(time)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
def to_doc(content), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(content)
|
|
|
|
|
|
|
|
defp make_open_tag(tag, attributes) do
|
|
|
|
attributes_string =
|
2020-01-22 01:14:42 +00:00
|
|
|
attributes
|
|
|
|
|> Enum.map(fn {attribute, value} -> "#{attribute}=\"#{value}\"" end)
|
2018-05-17 09:32:23 +00:00
|
|
|
|> Enum.join(" ")
|
|
|
|
|
|
|
|
[tag, attributes_string] |> Enum.join(" ") |> String.trim()
|
|
|
|
end
|
|
|
|
end
|