7e54e9d58297b9165e80e441297528c3c099b6c5
[akkoma] / lib / pleroma / docs / markdown.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.Markdown do
6 @behaviour Pleroma.Docs.Generator
7
8 @spec process(keyword()) :: {:ok, String.t()}
9 def process(descriptions) do
10 config_path = "docs/generated_config.md"
11 {:ok, file} = File.open(config_path, [:utf8, :write])
12 IO.write(file, "# Generated configuration\n")
13 IO.write(file, "Date of generation: #{Date.utc_today()}\n\n")
14
15 IO.write(
16 file,
17 "This file describe the configuration, it is recommended to edit the relevant `*.secret.exs` file instead of the others founds in the ``config`` directory.\n\n" <>
18 "If you run Pleroma with ``MIX_ENV=prod`` the file is ``prod.secret.exs``, otherwise it is ``dev.secret.exs``.\n\n"
19 )
20
21 for group <- descriptions do
22 if is_nil(group[:key]) do
23 IO.write(file, "## #{inspect(group[:group])}\n")
24 else
25 IO.write(file, "## #{inspect(group[:key])}\n")
26 end
27
28 IO.write(file, "#{group[:description]}\n")
29
30 for child <- group[:children] || [] do
31 print_child_header(file, child)
32
33 print_suggestions(file, child[:suggestions])
34
35 if child[:children] do
36 for subchild <- child[:children] do
37 print_child_header(file, subchild)
38
39 print_suggestions(file, subchild[:suggestions])
40 end
41 end
42 end
43
44 IO.write(file, "\n")
45 end
46
47 :ok = File.close(file)
48 {:ok, config_path}
49 end
50
51 defp print_child_header(file, %{key: key, type: type, description: description} = _child) do
52 IO.write(
53 file,
54 "- `#{inspect(key)}` (`#{inspect(type)}`): #{description} \n"
55 )
56 end
57
58 defp print_child_header(file, %{key: key, type: type} = _child) do
59 IO.write(file, "- `#{inspect(key)}` (`#{inspect(type)}`) \n")
60 end
61
62 defp print_suggestion(file, suggestion) when is_list(suggestion) do
63 IO.write(file, " `#{inspect(suggestion)}`\n")
64 end
65
66 defp print_suggestion(file, suggestion) when is_function(suggestion) do
67 IO.write(file, " `#{inspect(suggestion.())}`\n")
68 end
69
70 defp print_suggestion(file, suggestion, as_list \\ false) do
71 list_mark = if as_list, do: "- ", else: ""
72 IO.write(file, " #{list_mark}`#{inspect(suggestion)}`\n")
73 end
74
75 defp print_suggestions(file, {:list_behaviour_implementations, behaviour}) do
76 suggestions = Pleroma.Docs.Generator.list_behaviour_implementations(behaviour)
77 print_suggestions(file, suggestions)
78 end
79
80 defp print_suggestions(_file, nil), do: nil
81
82 defp print_suggestions(_file, ""), do: nil
83
84 defp print_suggestions(file, suggestions) do
85 if length(suggestions) > 1 do
86 IO.write(file, "Suggestions:\n")
87
88 for suggestion <- suggestions do
89 print_suggestion(file, suggestion, true)
90 end
91 else
92 IO.write(file, " Suggestion: ")
93
94 print_suggestion(file, List.first(suggestions))
95 end
96 end
97 end