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