aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/docs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/docs')
-rw-r--r--lib/pleroma/docs/generator.ex121
-rw-r--r--lib/pleroma/docs/json.ex24
-rw-r--r--lib/pleroma/docs/markdown.ex88
3 files changed, 233 insertions, 0 deletions
diff --git a/lib/pleroma/docs/generator.ex b/lib/pleroma/docs/generator.ex
new file mode 100644
index 000000000..6b12dcdd9
--- /dev/null
+++ b/lib/pleroma/docs/generator.ex
@@ -0,0 +1,121 @@
+defmodule Pleroma.Docs.Generator do
+ @callback process(keyword()) :: {:ok, String.t()}
+
+ @spec process(module(), keyword()) :: {:ok, String.t()}
+ def process(implementation, descriptions) do
+ implementation.process(descriptions)
+ end
+
+ @spec list_modules_in_dir(String.t(), String.t()) :: [module()]
+ def list_modules_in_dir(dir, start) do
+ with {:ok, files} <- File.ls(dir) do
+ files
+ |> Enum.filter(&String.ends_with?(&1, ".ex"))
+ |> Enum.map(fn filename ->
+ module = filename |> String.trim_trailing(".ex") |> Macro.camelize()
+ String.to_existing_atom(start <> module)
+ end)
+ end
+ end
+
+ @doc """
+ Converts:
+ - atoms to strings with leading `:`
+ - module names to strings, without leading `Elixir.`
+ - add humanized labels to `keys` if label is not defined, e.g. `:instance` -> `Instance`
+ """
+ @spec convert_to_strings([map()]) :: [map()]
+ def convert_to_strings(descriptions) do
+ Enum.map(descriptions, &format_entity(&1))
+ end
+
+ defp format_entity(entity) do
+ entity
+ |> format_key()
+ |> Map.put(:group, atom_to_string(entity[:group]))
+ |> format_children()
+ end
+
+ defp format_key(%{key: key} = entity) do
+ entity
+ |> Map.put(:key, atom_to_string(key))
+ |> Map.put(:label, entity[:label] || humanize(key))
+ end
+
+ defp format_key(%{group: group} = entity) do
+ Map.put(entity, :label, entity[:label] || humanize(group))
+ end
+
+ defp format_key(entity), do: entity
+
+ defp format_children(%{children: children} = entity) do
+ Map.put(entity, :children, Enum.map(children, &format_child(&1)))
+ end
+
+ defp format_children(entity), do: entity
+
+ defp format_child(%{suggestions: suggestions} = entity) do
+ entity
+ |> Map.put(:suggestions, format_suggestions(suggestions))
+ |> format_key()
+ |> format_group()
+ |> format_children()
+ end
+
+ defp format_child(entity) do
+ entity
+ |> format_key()
+ |> format_group()
+ |> format_children()
+ end
+
+ defp format_group(%{group: group} = entity) do
+ Map.put(entity, :group, format_suggestion(group))
+ end
+
+ defp format_group(entity), do: entity
+
+ defp atom_to_string(entity) when is_binary(entity), do: entity
+
+ defp atom_to_string(entity) when is_atom(entity), do: inspect(entity)
+
+ defp humanize(entity) do
+ string = inspect(entity)
+
+ if String.starts_with?(string, ":"),
+ do: Phoenix.Naming.humanize(entity),
+ else: string
+ end
+
+ defp format_suggestions([]), do: []
+
+ defp format_suggestions([suggestion | tail]) do
+ [format_suggestion(suggestion) | format_suggestions(tail)]
+ end
+
+ defp format_suggestion(entity) when is_atom(entity) do
+ atom_to_string(entity)
+ end
+
+ defp format_suggestion([head | tail] = entity) when is_list(entity) do
+ [format_suggestion(head) | format_suggestions(tail)]
+ end
+
+ defp format_suggestion(entity) when is_tuple(entity) do
+ format_suggestions(Tuple.to_list(entity)) |> List.to_tuple()
+ end
+
+ defp format_suggestion(entity), do: entity
+end
+
+defimpl Jason.Encoder, for: Tuple do
+ def encode(tuple, opts), do: Jason.Encode.list(Tuple.to_list(tuple), opts)
+end
+
+defimpl Jason.Encoder, for: [Regex, Function] do
+ def encode(term, opts), do: Jason.Encode.string(inspect(term), opts)
+end
+
+defimpl String.Chars, for: Regex do
+ def to_string(term), do: inspect(term)
+end
diff --git a/lib/pleroma/docs/json.ex b/lib/pleroma/docs/json.ex
new file mode 100644
index 000000000..6508a7bdb
--- /dev/null
+++ b/lib/pleroma/docs/json.ex
@@ -0,0 +1,24 @@
+defmodule Pleroma.Docs.JSON do
+ @behaviour Pleroma.Docs.Generator
+
+ @spec process(keyword()) :: {:ok, String.t()}
+ def process(descriptions) do
+ with path <- "docs/generated_config.json",
+ {:ok, file} <- File.open(path, [:write, :utf8]),
+ formatted_descriptions <-
+ Pleroma.Docs.Generator.convert_to_strings(descriptions),
+ json <- Jason.encode!(formatted_descriptions),
+ :ok <- IO.write(file, json),
+ :ok <- File.close(file) do
+ {:ok, path}
+ end
+ end
+
+ def compile do
+ with config <- Pleroma.Config.Loader.load("config/description.exs") do
+ config[:pleroma][:config_description]
+ |> Pleroma.Docs.Generator.convert_to_strings()
+ |> Jason.encode!()
+ end
+ end
+end
diff --git a/lib/pleroma/docs/markdown.ex b/lib/pleroma/docs/markdown.ex
new file mode 100644
index 000000000..68b106499
--- /dev/null
+++ b/lib/pleroma/docs/markdown.ex
@@ -0,0 +1,88 @@
+defmodule Pleroma.Docs.Markdown do
+ @behaviour Pleroma.Docs.Generator
+
+ @spec process(keyword()) :: {:ok, String.t()}
+ def process(descriptions) do
+ 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.\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])}\n")
+ else
+ IO.write(file, "## #{inspect(group[:key])}\n")
+ end
+
+ IO.write(file, "#{group[:description]}\n")
+
+ for child <- group[:children] || [] do
+ print_child_header(file, child)
+
+ print_suggestions(file, child[:suggestions])
+
+ if child[:children] do
+ for subchild <- child[:children] do
+ print_child_header(file, subchild)
+
+ print_suggestions(file, subchild[:suggestions])
+ end
+ end
+ end
+
+ 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.())}`\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_suggestions(_file, nil), do: nil
+
+ defp print_suggestions(_file, ""), do: nil
+
+ defp print_suggestions(file, suggestions) do
+ if length(suggestions) > 1 do
+ IO.write(file, "Suggestions:\n")
+
+ for suggestion <- suggestions do
+ print_suggestion(file, suggestion, true)
+ end
+ else
+ IO.write(file, " Suggestion: ")
+
+ print_suggestion(file, List.first(suggestions))
+ end
+ end
+end