58a42b32377d361d392cee6c383145a558e3be62
[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, child) do
48 IO.write(
49 file,
50 "- `#{inspect(child[:key])}` (`#{inspect(child[:type])}`): #{child[:description]}\n"
51 )
52 end
53
54 defp print_suggestion(file, suggestion) when is_list(suggestion) do
55 IO.write(file, " `#{inspect(suggestion)}`\n")
56 end
57
58 defp print_suggestion(file, suggestion) when is_function(suggestion) do
59 IO.write(file, " `#{inspect(suggestion.())}`\n")
60 end
61
62 defp print_suggestion(file, suggestion, as_list \\ false) do
63 list_mark = if as_list, do: "- ", else: ""
64 IO.write(file, " #{list_mark}`#{inspect(suggestion)}`\n")
65 end
66
67 defp print_suggestions(_file, nil), do: nil
68
69 defp print_suggestions(file, suggestions) do
70 IO.write(file, "Suggestions:\n")
71
72 if length(suggestions) > 1 do
73 for suggestion <- suggestions do
74 print_suggestion(file, suggestion, true)
75 end
76 else
77 print_suggestion(file, List.first(suggestions))
78 end
79 end
80 end