logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

generator.ex (3959B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Docs.Generator do
  5. @callback process(keyword()) :: {:ok, String.t()}
  6. @spec process(module(), keyword()) :: {:ok, String.t()}
  7. def process(implementation, descriptions) do
  8. implementation.process(descriptions)
  9. end
  10. @spec list_behaviour_implementations(behaviour :: module()) :: [module()]
  11. def list_behaviour_implementations(behaviour) do
  12. :code.all_loaded()
  13. |> Enum.filter(fn {module, _} ->
  14. # This shouldn't be needed as all modules are expected to have module_info/1,
  15. # but in test environments some transient modules `:elixir_compiler_XX`
  16. # are loaded for some reason (where XX is a random integer).
  17. Code.ensure_loaded(module)
  18. if function_exported?(module, :module_info, 1) do
  19. module.module_info(:attributes)
  20. |> Keyword.get_values(:behaviour)
  21. |> List.flatten()
  22. |> Enum.member?(behaviour)
  23. end
  24. end)
  25. |> Enum.map(fn {module, _} -> module end)
  26. end
  27. @doc """
  28. Converts:
  29. - atoms to strings with leading `:`
  30. - module names to strings, without leading `Elixir.`
  31. - add humanized labels to `keys` if label is not defined, e.g. `:instance` -> `Instance`
  32. """
  33. @spec convert_to_strings([map()]) :: [map()]
  34. def convert_to_strings(descriptions) do
  35. Enum.map(descriptions, &format_entity(&1))
  36. end
  37. defp format_entity(entity) do
  38. entity
  39. |> format_key()
  40. |> Map.put(:group, atom_to_string(entity[:group]))
  41. |> format_children()
  42. end
  43. defp format_key(%{key: key} = entity) do
  44. entity
  45. |> Map.put(:key, atom_to_string(key))
  46. |> Map.put(:label, entity[:label] || humanize(key))
  47. end
  48. defp format_key(%{group: group} = entity) do
  49. Map.put(entity, :label, entity[:label] || humanize(group))
  50. end
  51. defp format_key(entity), do: entity
  52. defp format_children(%{children: children} = entity) do
  53. Map.put(entity, :children, Enum.map(children, &format_child(&1)))
  54. end
  55. defp format_children(entity), do: entity
  56. defp format_child(%{suggestions: suggestions} = entity) do
  57. entity
  58. |> Map.put(:suggestions, format_suggestions(suggestions))
  59. |> format_key()
  60. |> format_group()
  61. |> format_children()
  62. end
  63. defp format_child(entity) do
  64. entity
  65. |> format_key()
  66. |> format_group()
  67. |> format_children()
  68. end
  69. defp format_group(%{group: group} = entity) do
  70. Map.put(entity, :group, format_suggestion(group))
  71. end
  72. defp format_group(entity), do: entity
  73. defp atom_to_string(entity) when is_binary(entity), do: entity
  74. defp atom_to_string(entity) when is_atom(entity), do: inspect(entity)
  75. defp humanize(entity) do
  76. string = inspect(entity)
  77. if String.starts_with?(string, ":"),
  78. do: Phoenix.Naming.humanize(entity),
  79. else: string
  80. end
  81. defp format_suggestions({:list_behaviour_implementations, behaviour}) do
  82. behaviour
  83. |> list_behaviour_implementations()
  84. |> format_suggestions()
  85. end
  86. defp format_suggestions([]), do: []
  87. defp format_suggestions([suggestion | tail]) do
  88. [format_suggestion(suggestion) | format_suggestions(tail)]
  89. end
  90. defp format_suggestion(entity) when is_atom(entity) do
  91. atom_to_string(entity)
  92. end
  93. defp format_suggestion([head | tail] = entity) when is_list(entity) do
  94. [format_suggestion(head) | format_suggestions(tail)]
  95. end
  96. defp format_suggestion(entity) when is_tuple(entity) do
  97. format_suggestions(Tuple.to_list(entity)) |> List.to_tuple()
  98. end
  99. defp format_suggestion(entity), do: entity
  100. end
  101. defimpl Jason.Encoder, for: Tuple do
  102. def encode(tuple, opts), do: Jason.Encode.list(Tuple.to_list(tuple), opts)
  103. end
  104. defimpl Jason.Encoder, for: [Regex, Function] do
  105. def encode(term, opts), do: Jason.Encode.string(inspect(term), opts)
  106. end
  107. defimpl String.Chars, for: Regex do
  108. def to_string(term), do: inspect(term)
  109. end