Merge remote-tracking branch 'upstream/develop' into registration-workflow
[akkoma] / lib / pleroma / docs / markdown.ex
index 27a0966311be8e6076db672b5ffae9dd601551d4..eac0789a6d4b53baebeeacecb967d8ac058586b7 100644 (file)
@@ -1,29 +1,33 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
 defmodule Pleroma.Docs.Markdown do
-  @behaviour Pleroma.Docs.Formatter
+  @behaviour Pleroma.Docs.Generator
 
+  @spec process(keyword()) :: {:ok, String.t()}
   def process(descriptions) do
-    config_path = "docs/config.md"
-    {:ok, file} = File.open(config_path, [:write])
-    IO.write(file, "# Generated configuration\r\n\r\n")
-    IO.write(file, "Date of generation: #{Date.utc_today()}\r\n\r\n")
+    config_path = "docs/generated_config.md"
+    {:ok, file} = File.open(config_path, [:utf8, :write])
+    IO.write(file, "# Generated configuration\n")
+    IO.write(file, "Date of generation: #{Date.utc_today()}\n\n")
 
     IO.write(
       file,
-      "This file describe the configuration, it is recommended to edit the relevant *.secret.exs file instead of the others founds in the ``config`` directory.
-If you run Pleroma with ``MIX_ENV=prod`` the file is ``prod.secret.exs``, otherwise it is ``dev.secret.exs``.\r\n\r\n"
+      "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" <>
+        "If you run Pleroma with ``MIX_ENV=prod`` the file is ``prod.secret.exs``, otherwise it is ``dev.secret.exs``.\n\n"
     )
 
     for group <- descriptions do
       if is_nil(group[:key]) do
-        IO.write(file, "## #{inspect(group[:group])}\r\n\r\n")
+        IO.write(file, "## #{inspect(group[:group])}\n")
       else
-        IO.write(file, "## #{inspect(group[:key])}\r\n\r\n")
+        IO.write(file, "## #{inspect(group[:key])}\n")
       end
 
-      IO.write(file, "Type: `#{group[:type]}`  \r\n")
-      IO.write(file, "#{group[:description]}  \r\n\r\n")
+      IO.write(file, "#{group[:description]}\n")
 
-      for child <- group[:children] do
+      for child <- group[:children] || [] do
         print_child_header(file, child)
 
         print_suggestions(file, child[:suggestions])
@@ -37,31 +41,57 @@ If you run Pleroma with ``MIX_ENV=prod`` the file is ``prod.secret.exs``, otherw
         end
       end
 
-      IO.write(file, "\r\n")
+      IO.write(file, "\n")
     end
 
     :ok = File.close(file)
     {:ok, config_path}
   end
 
+  defp print_child_header(file, %{key: key, type: type, description: description} = _child) do
+    IO.write(
+      file,
+      "- `#{inspect(key)}` (`#{inspect(type)}`): #{description}  \n"
+    )
+  end
+
+  defp print_child_header(file, %{key: key, type: type} = _child) do
+    IO.write(file, "- `#{inspect(key)}` (`#{inspect(type)}`)  \n")
+  end
+
+  defp print_suggestion(file, suggestion) when is_list(suggestion) do
+    IO.write(file, "  `#{inspect(suggestion)}`\n")
+  end
+
   defp print_suggestion(file, suggestion) when is_function(suggestion) do
-    IO.write(file, "    `#{inspect(suggestion.())}`\r\n")
+    IO.write(file, "  `#{inspect(suggestion.())}`\n")
+  end
+
+  defp print_suggestion(file, suggestion, as_list \\ false) do
+    list_mark = if as_list, do: "- ", else: ""
+    IO.write(file, "  #{list_mark}`#{inspect(suggestion)}`\n")
   end
 
-  defp print_suggestion(file, suggestion) do
-    IO.write(file, "    `#{inspect(suggestion)}`\r\n")
+  defp print_suggestions(file, {:list_behaviour_implementations, behaviour}) do
+    suggestions = Pleroma.Docs.Generator.list_behaviour_implementations(behaviour)
+    print_suggestions(file, suggestions)
   end
 
+  defp print_suggestions(_file, nil), do: nil
+
+  defp print_suggestions(_file, ""), do: nil
+
   defp print_suggestions(file, suggestions) do
-    IO.write(file, "Suggestions:  \r\n")
+    if length(suggestions) > 1 do
+      IO.write(file, "Suggestions:\n")
 
-    for suggestion <- suggestions do
-      print_suggestion(file, suggestion)
-    end
-  end
+      for suggestion <- suggestions do
+        print_suggestion(file, suggestion, true)
+      end
+    else
+      IO.write(file, "  Suggestion: ")
 
-  defp print_child_header(file, child) do
-    IO.write(file, "* `#{inspect(child[:key])}`: #{child[:description]}  \r\n")
-    IO.write(file, "Type: `#{inspect(child[:type])}`  \r\n")
+      print_suggestion(file, List.first(suggestions))
+    end
   end
 end