1 defmodule Pleroma.Docs.Generator do
2 @callback process(keyword()) :: {:ok, String.t()}
4 @spec process(module(), keyword()) :: {:ok, String.t()}
5 def process(implementation, descriptions) do
6 implementation.process(descriptions)
9 @spec list_behaviour_implementations(behaviour :: module()) :: [module()]
10 def list_behaviour_implementations(behaviour) do
12 |> Enum.filter(fn {module, _} ->
13 # This shouldn't be needed as all modules are expected to have module_info/1,
14 # but in test enviroments some transient modules `:elixir_compiler_XX`
15 # are loaded for some reason (where XX is a random integer).
16 if function_exported?(module, :module_info, 1) do
17 module.module_info(:attributes)
18 |> Keyword.get_values(:behaviour)
20 |> Enum.member?(behaviour)
23 |> Enum.map(fn {module, _} -> module end)
28 - atoms to strings with leading `:`
29 - module names to strings, without leading `Elixir.`
30 - add humanized labels to `keys` if label is not defined, e.g. `:instance` -> `Instance`
32 @spec convert_to_strings([map()]) :: [map()]
33 def convert_to_strings(descriptions) do
34 Enum.map(descriptions, &format_entity(&1))
37 defp format_entity(entity) do
40 |> Map.put(:group, atom_to_string(entity[:group]))
44 defp format_key(%{key: key} = entity) do
46 |> Map.put(:key, atom_to_string(key))
47 |> Map.put(:label, entity[:label] || humanize(key))
50 defp format_key(%{group: group} = entity) do
51 Map.put(entity, :label, entity[:label] || humanize(group))
54 defp format_key(entity), do: entity
56 defp format_children(%{children: children} = entity) do
57 Map.put(entity, :children, Enum.map(children, &format_child(&1)))
60 defp format_children(entity), do: entity
62 defp format_child(%{suggestions: suggestions} = entity) do
64 |> Map.put(:suggestions, format_suggestions(suggestions))
70 defp format_child(entity) do
77 defp format_group(%{group: group} = entity) do
78 Map.put(entity, :group, format_suggestion(group))
81 defp format_group(entity), do: entity
83 defp atom_to_string(entity) when is_binary(entity), do: entity
85 defp atom_to_string(entity) when is_atom(entity), do: inspect(entity)
87 defp humanize(entity) do
88 string = inspect(entity)
90 if String.starts_with?(string, ":"),
91 do: Phoenix.Naming.humanize(entity),
95 defp format_suggestions({:list_behaviour_implementations, behaviour}) do
97 |> list_behaviour_implementations()
98 |> format_suggestions()
101 defp format_suggestions([]), do: []
103 defp format_suggestions([suggestion | tail]) do
104 [format_suggestion(suggestion) | format_suggestions(tail)]
107 defp format_suggestion(entity) when is_atom(entity) do
108 atom_to_string(entity)
111 defp format_suggestion([head | tail] = entity) when is_list(entity) do
112 [format_suggestion(head) | format_suggestions(tail)]
115 defp format_suggestion(entity) when is_tuple(entity) do
116 format_suggestions(Tuple.to_list(entity)) |> List.to_tuple()
119 defp format_suggestion(entity), do: entity
122 defimpl Jason.Encoder, for: Tuple do
123 def encode(tuple, opts), do: Jason.Encode.list(Tuple.to_list(tuple), opts)
126 defimpl Jason.Encoder, for: [Regex, Function] do
127 def encode(term, opts), do: Jason.Encode.string(inspect(term), opts)
130 defimpl String.Chars, for: Regex do
131 def to_string(term), do: inspect(term)