feeb4320e62b10ed56b9160eea548fe83fe9c77d
[akkoma] / lib / pleroma / docs / json.ex
1 defmodule Pleroma.Docs.JSON do
2 @behaviour Pleroma.Docs.Generator
3 @external_resource "config/description.exs"
4 @raw_config Pleroma.Config.Loader.read("config/description.exs")
5 @raw_descriptions @raw_config[:pleroma][:config_description]
6 @term __MODULE__.Compiled
7
8 @spec compile :: :ok
9 def compile do
10 :persistent_term.put(@term, Pleroma.Docs.Generator.convert_to_strings(@raw_descriptions))
11 end
12
13 @spec compiled_descriptions :: Map.t()
14 def compiled_descriptions do
15 :persistent_term.get(@term)
16 end
17
18 @spec process(keyword()) :: {:ok, String.t()}
19 def process(descriptions) do
20 with path <- "docs/generated_config.json",
21 {:ok, file} <- File.open(path, [:write, :utf8]),
22 formatted_descriptions <-
23 Pleroma.Docs.Generator.convert_to_strings(descriptions),
24 json <- Jason.encode!(formatted_descriptions),
25 :ok <- IO.write(file, json),
26 :ok <- File.close(file) do
27 {:ok, path}
28 end
29 end
30 end