1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Docs.Generator do
6 @callback process(keyword()) :: {:ok, String.t()}
8 @spec process(module(), keyword()) :: {:ok, String.t()}
9 def process(implementation, descriptions) do
10 implementation.process(descriptions)
13 @spec list_behaviour_implementations(behaviour :: module()) :: [module()]
14 def list_behaviour_implementations(behaviour) do
16 |> Enum.filter(fn {module, _} ->
17 # This shouldn't be needed as all modules are expected to have module_info/1,
18 # but in test enviroments some transient modules `:elixir_compiler_XX`
19 # are loaded for some reason (where XX is a random integer).
20 if function_exported?(module, :module_info, 1) do
21 module.module_info(:attributes)
22 |> Keyword.get_values(:behaviour)
24 |> Enum.member?(behaviour)
27 |> Enum.map(fn {module, _} -> module end)
32 - atoms to strings with leading `:`
33 - module names to strings, without leading `Elixir.`
34 - add humanized labels to `keys` if label is not defined, e.g. `:instance` -> `Instance`
36 @spec convert_to_strings([map()]) :: [map()]
37 def convert_to_strings(descriptions) do
38 Enum.map(descriptions, &format_entity(&1))
41 defp format_entity(entity) do
44 |> Map.put(:group, atom_to_string(entity[:group]))
48 defp format_key(%{key: key} = entity) do
50 |> Map.put(:key, atom_to_string(key))
51 |> Map.put(:label, entity[:label] || humanize(key))
54 defp format_key(%{group: group} = entity) do
55 Map.put(entity, :label, entity[:label] || humanize(group))
58 defp format_key(entity), do: entity
60 defp format_children(%{children: children} = entity) do
61 Map.put(entity, :children, Enum.map(children, &format_child(&1)))
64 defp format_children(entity), do: entity
66 defp format_child(%{suggestions: suggestions} = entity) do
68 |> Map.put(:suggestions, format_suggestions(suggestions))
74 defp format_child(entity) do
81 defp format_group(%{group: group} = entity) do
82 Map.put(entity, :group, format_suggestion(group))
85 defp format_group(entity), do: entity
87 defp atom_to_string(entity) when is_binary(entity), do: entity
89 defp atom_to_string(entity) when is_atom(entity), do: inspect(entity)
91 defp humanize(entity) do
92 string = inspect(entity)
94 if String.starts_with?(string, ":"),
95 do: Phoenix.Naming.humanize(entity),
99 defp format_suggestions({:list_behaviour_implementations, behaviour}) do
101 |> list_behaviour_implementations()
102 |> format_suggestions()
105 defp format_suggestions([]), do: []
107 defp format_suggestions([suggestion | tail]) do
108 [format_suggestion(suggestion) | format_suggestions(tail)]
111 defp format_suggestion(entity) when is_atom(entity) do
112 atom_to_string(entity)
115 defp format_suggestion([head | tail] = entity) when is_list(entity) do
116 [format_suggestion(head) | format_suggestions(tail)]
119 defp format_suggestion(entity) when is_tuple(entity) do
120 format_suggestions(Tuple.to_list(entity)) |> List.to_tuple()
123 defp format_suggestion(entity), do: entity
126 defimpl Jason.Encoder, for: Tuple do
127 def encode(tuple, opts), do: Jason.Encode.list(Tuple.to_list(tuple), opts)
130 defimpl Jason.Encoder, for: [Regex, Function] do
131 def encode(term, opts), do: Jason.Encode.string(inspect(term), opts)
134 defimpl String.Chars, for: Regex do
135 def to_string(term), do: inspect(term)