1 defmodule Pleroma.Docs.Markdown do
2 @behaviour Pleroma.Docs.Generator
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")
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"
17 for group <- descriptions do
18 if is_nil(group[:key]) do
19 IO.write(file, "## #{inspect(group[:group])}\n")
21 IO.write(file, "## #{inspect(group[:key])}\n")
24 IO.write(file, "#{group[:description]}\n")
26 for child <- group[:children] || [] do
27 print_child_header(file, child)
29 print_suggestions(file, child[:suggestions])
31 if child[:children] do
32 for subchild <- child[:children] do
33 print_child_header(file, subchild)
35 print_suggestions(file, subchild[:suggestions])
43 :ok = File.close(file)
47 defp print_child_header(file, %{key: key, type: type, description: description} = _child) do
50 "- `#{inspect(key)}` (`#{inspect(type)}`): #{description} \n"
54 defp print_child_header(file, %{key: key, type: type} = _child) do
55 IO.write(file, "- `#{inspect(key)}` (`#{inspect(type)}`) \n")
58 defp print_suggestion(file, suggestion) when is_list(suggestion) do
59 IO.write(file, " `#{inspect(suggestion)}`\n")
62 defp print_suggestion(file, suggestion) when is_function(suggestion) do
63 IO.write(file, " `#{inspect(suggestion.())}`\n")
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")
71 defp print_suggestions(_file, nil), do: nil
73 defp print_suggestions(_file, ""), do: nil
75 defp print_suggestions(file, suggestions) do
76 if length(suggestions) > 1 do
77 IO.write(file, "Suggestions:\n")
79 for suggestion <- suggestions do
80 print_suggestion(file, suggestion, true)
83 IO.write(file, " Suggestion: ")
85 print_suggestion(file, List.first(suggestions))