New command to produce markdown with tags and their associated commands

This commit is contained in:
Badlop 2021-08-21 20:50:02 +02:00
parent ac4f240261
commit 655dcbcb74
2 changed files with 30 additions and 2 deletions

View File

@ -91,6 +91,16 @@ get_commands_spec() ->
"that will have example invocation include in markdown document"],
result_desc = "0 if command failed, 1 when succeeded",
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
result_example = ok},
#ejabberd_commands{name = gen_markdown_doc_for_tags, tags = [documentation],
desc = "Generates markdown documentation for ejabberd_commands",
module = ejabberd_commands_doc, function = generate_tags_md,
args = [{file, binary}],
result = {res, rescode},
args_desc = ["Path to file where generated "
"documentation should be stored"],
result_desc = "0 if command failed, 1 when succeeded",
args_example = ["/home/me/docs/tags.md"],
result_example = ok}].
start_link() ->

View File

@ -28,6 +28,7 @@
-export([generate_html_output/3]).
-export([generate_md_output/3]).
-export([generate_tags_md/1]).
-include("ejabberd_commands.hrl").
@ -360,6 +361,13 @@ gen_param(Name, Type, Desc, HTMLOutput) ->
[?TAG(dt, [?TAG_R(strong, atom_to_list(Name)), <<" :: ">>, ?RAW(format_type(Type))]),
?TAG(dd, ?RAW(Desc))].
make_tags(HTMLOutput) ->
TagsList = ejabberd_commands:get_tags_commands(1000000),
lists:map(fun(T) -> gen_tags(T, HTMLOutput) end, TagsList).
gen_tags({TagName, Commands}, HTMLOutput) ->
[?TAG(h1, TagName) | [?TAG(p, ?RAW("* *`"++C++"`*")) || C <- Commands]].
gen_doc(#ejabberd_commands{name=Name, tags=Tags, desc=Desc, longdesc=LongDesc,
args=Args, args_desc=ArgsDesc, note=Note, definer=Definer,
result=Result, result_desc=ResultDesc}=Cmd, HTMLOutput, Langs) ->
@ -389,7 +397,7 @@ gen_doc(#ejabberd_commands{name=Name, tags=Tags, desc=Desc, longdesc=LongDesc,
[?TAG(dl, [gen_param(RName, Type, ResultDesc, HTMLOutput)])]
end
end,
TagsText = [?RAW(atom_to_list(Tag) ++ " ") || Tag <- Tags],
TagsText = [?RAW("*`"++atom_to_list(Tag)++"`* ") || Tag <- Tags],
IsDefinerMod = case Definer of
unknown -> true;
_ -> lists:member(gen_mod, proplists:get_value(behaviour, Definer:module_info(attributes)))
@ -482,13 +490,23 @@ generate_md_output(File, RegExp, Languages) ->
Langs = binary:split(Languages, <<",">>, [global]),
Header = <<"---\ntitle: Administration API reference\ntoc: true\nmenu: API Reference\norder: 1\n"
"// Autogenerated with 'ejabberdctl gen_markdown_doc_for_commands'\n---\n\n"
"This section describes API of ejabberd.">>,
"This section describes API of ejabberd.\n">>,
Out = lists:map(fun(C) -> gen_doc(C, false, Langs) end, Cmds4),
{ok, Fh} = file:open(File, [write]),
io:format(Fh, "~ts~ts", [Header, Out]),
file:close(Fh),
ok.
generate_tags_md(File) ->
Header = <<"---\ntitle: API Tags\ntoc: true\nmenu: API Tags\norder: 2\n"
"// Autogenerated with 'ejabberdctl gen_markdown_doc_for_tags'\n---\n\n"
"This section enumerates the tags and their associated API.\n">>,
Tags = make_tags(false),
{ok, Fh} = file:open(File, [write]),
io:format(Fh, "~ts~ts", [Header, Tags]),
file:close(Fh),
ok.
html_pre() ->
"<!DOCTYPE>
<html>