2020-10-12 17:00:50 +00:00
# Pleroma: A lightweight social networking server
2023-01-02 20:38:50 +00:00
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
2020-10-12 17:00:50 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
2019-08-30 10:21:48 +00:00
defmodule Pleroma.Docs.Markdown do
2019-08-30 16:14:01 +00:00
@behaviour Pleroma.Docs.Generator
2019-08-30 10:21:48 +00:00
2019-08-30 16:14:01 +00:00
@spec process ( keyword ( ) ) :: { :ok , String . t ( ) }
2019-08-30 10:21:48 +00:00
def process ( descriptions ) do
2019-09-13 16:02:42 +00:00
config_path = " docs/generated_config.md "
2019-09-03 16:22:25 +00:00
{ :ok , file } = File . open ( config_path , [ :utf8 , :write ] )
2019-09-13 16:02:42 +00:00
IO . write ( file , " # Generated configuration \n " )
2019-09-03 16:22:25 +00:00
IO . write ( file , " Date of generation: #{ Date . utc_today ( ) } \n \n " )
2019-08-30 10:21:48 +00:00
IO . write (
file ,
2019-09-03 16:22:25 +00:00
" 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 "
2019-08-30 10:21:48 +00:00
)
for group <- descriptions do
if is_nil ( group [ :key ] ) do
2019-09-03 16:22:25 +00:00
IO . write ( file , " # # #{ inspect ( group [ :group ] ) } \n " )
2019-08-30 10:21:48 +00:00
else
2019-09-03 16:22:25 +00:00
IO . write ( file , " # # #{ inspect ( group [ :key ] ) } \n " )
2019-08-30 10:21:48 +00:00
end
2019-09-03 16:22:25 +00:00
IO . write ( file , " #{ group [ :description ] } \n " )
2019-08-30 10:21:48 +00:00
2019-09-17 20:04:21 +00:00
for child <- group [ :children ] || [ ] do
2019-08-30 10:21:48 +00:00
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
2019-09-03 16:22:25 +00:00
IO . write ( file , " \n " )
2019-08-30 10:21:48 +00:00
end
:ok = File . close ( file )
{ :ok , config_path }
end
2019-09-17 20:14:56 +00:00
defp print_child_header ( file , %{ key : key , type : type , description : description } = _child ) do
2019-09-17 19:43:27 +00:00
IO . write (
file ,
2019-09-17 21:02:24 +00:00
" - ` #{ inspect ( key ) } ` (` #{ inspect ( type ) } `): #{ description } \n "
2019-09-17 19:43:27 +00:00
)
end
2019-09-17 20:14:56 +00:00
defp print_child_header ( file , %{ key : key , type : type } = _child ) do
2019-09-17 21:02:24 +00:00
IO . write ( file , " - ` #{ inspect ( key ) } ` (` #{ inspect ( type ) } `) \n " )
2019-09-17 20:14:56 +00:00
end
2019-08-30 16:14:01 +00:00
defp print_suggestion ( file , suggestion ) when is_list ( suggestion ) do
2019-09-03 16:22:25 +00:00
IO . write ( file , " ` #{ inspect ( suggestion ) } ` \n " )
2019-08-30 16:14:01 +00:00
end
2019-08-30 10:21:48 +00:00
defp print_suggestion ( file , suggestion ) when is_function ( suggestion ) do
2019-09-03 16:22:25 +00:00
IO . write ( file , " ` #{ inspect ( suggestion . ( ) ) } ` \n " )
2019-08-30 10:21:48 +00:00
end
2019-08-30 16:14:01 +00:00
defp print_suggestion ( file , suggestion , as_list \\ false ) do
2019-09-03 16:22:25 +00:00
list_mark = if as_list , do : " - " , else : " "
IO . write ( file , " #{ list_mark } ` #{ inspect ( suggestion ) } ` \n " )
2019-08-30 10:21:48 +00:00
end
2020-07-12 15:23:33 +00:00
defp print_suggestions ( file , { :list_behaviour_implementations , behaviour } ) do
suggestions = Pleroma.Docs.Generator . list_behaviour_implementations ( behaviour )
print_suggestions ( file , suggestions )
end
2019-08-30 16:14:01 +00:00
defp print_suggestions ( _file , nil ) , do : nil
2019-09-17 20:00:02 +00:00
defp print_suggestions ( _file , " " ) , do : nil
2019-08-30 10:21:48 +00:00
defp print_suggestions ( file , suggestions ) do
2019-08-30 16:14:01 +00:00
if length ( suggestions ) > 1 do
2019-09-17 21:02:24 +00:00
IO . write ( file , " Suggestions: \n " )
2019-09-17 20:22:54 +00:00
2019-08-30 16:14:01 +00:00
for suggestion <- suggestions do
print_suggestion ( file , suggestion , true )
end
else
2019-09-17 20:33:32 +00:00
IO . write ( file , " Suggestion: " )
2019-09-17 20:22:54 +00:00
2019-08-30 16:14:01 +00:00
print_suggestion ( file , List . first ( suggestions ) )
2019-08-30 10:21:48 +00:00
end
end
end