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