f22432ea48c924ca617d0a39070f65d025c77d4a
[akkoma] / lib / pleroma / docs / json.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.Docs.JSON do
6 @behaviour Pleroma.Docs.Generator
7 @external_resource "config/description.exs"
8 @raw_config Pleroma.Config.Loader.read("config/description.exs")
9 @raw_descriptions @raw_config[:pleroma][:config_description]
10 @term __MODULE__.Compiled
11
12 @spec compile :: :ok
13 def compile do
14 descriptions =
15 Pleroma.Web.ActivityPub.MRF.config_descriptions()
16 |> Enum.reduce(@raw_descriptions, fn description, acc -> [description | acc] end)
17
18 :persistent_term.put(@term, Pleroma.Docs.Generator.convert_to_strings(descriptions))
19 end
20
21 @spec compiled_descriptions :: Map.t()
22 def compiled_descriptions do
23 :persistent_term.get(@term)
24 end
25
26 @spec process(keyword()) :: {:ok, String.t()}
27 def process(descriptions) do
28 with path <- "docs/generated_config.json",
29 {:ok, file} <- File.open(path, [:write, :utf8]),
30 formatted_descriptions <-
31 Pleroma.Docs.Generator.convert_to_strings(descriptions),
32 json <- Jason.encode!(formatted_descriptions),
33 :ok <- IO.write(file, json),
34 :ok <- File.close(file) do
35 {:ok, path}
36 end
37 end
38 end