Improve API documentation generator

This commit is contained in:
Christophe Romain 2017-06-20 14:45:57 +02:00
parent a11e833a98
commit a7841ed486
1 changed files with 33 additions and 29 deletions

View File

@ -87,13 +87,8 @@ md_tag(strong, V) ->
md_tag(_, V) -> md_tag(_, V) ->
V. V.
unbinarize(binary) -> string;
%% rescode_to_int(ok) -> unbinarize(Other) -> Other.
%% 0;
%% rescode_to_int(true) ->
%% 0;
%% rescode_to_int(_) ->
%% 1.
perl_gen({Name, integer}, Int, _Indent, HTMLOutput) -> perl_gen({Name, integer}, Int, _Indent, HTMLOutput) ->
[?ARG(Name), ?OP_L(" => "), ?NUM(Int)]; [?ARG(Name), ?OP_L(" => "), ?NUM(Int)];
@ -345,6 +340,14 @@ gen_calls(#ejabberd_commands{args_example=Values, args=ArgsDesc,
end end
end. end.
gen_param(Name, Type, undefined, HTMLOutput) ->
[?TAG(li, [?TAG_R(strong, atom_to_list(Name)), <<" :: ">>,
?RAW(io_lib:format("~p", [unbinarize(Type)]))])];
gen_param(Name, Type, Desc, HTMLOutput) ->
[?TAG(dt, [?TAG_R(strong, atom_to_list(Name)), <<" :: ">>,
?RAW(io_lib:format("~p", [unbinarize(Type)]))]),
?TAG(dd, ?RAW(Desc))].
gen_doc(#ejabberd_commands{name=Name, tags=_Tags, desc=Desc, longdesc=LongDesc, gen_doc(#ejabberd_commands{name=Name, tags=_Tags, desc=Desc, longdesc=LongDesc,
args=Args, args_desc=ArgsDesc, args=Args, args_desc=ArgsDesc,
result=Result, result_desc=ResultDesc}=Cmd, HTMLOutput, Langs) -> result=Result, result_desc=ResultDesc}=Cmd, HTMLOutput, Langs) ->
@ -354,35 +357,36 @@ gen_doc(#ejabberd_commands{name=Name, tags=_Tags, desc=Desc, longdesc=LongDesc,
end, end,
ArgsText = case ArgsDesc of ArgsText = case ArgsDesc of
none -> none ->
[?TAG(ul, "args-list", lists:map(fun({AName, Type}) -> [?TAG(ul, "args-list", [gen_param(AName, Type, undefined, HTMLOutput)
[?TAG(li, [?TAG_R(strong, atom_to_list(AName)), <<" :: ">>, || {AName, Type} <- Args])];
?RAW(io_lib:format("~p", [Type]))])]
end, Args))];
_ -> _ ->
[?TAG(dl, "args-list", lists:map(fun({{AName, Type}, ADesc}) -> [?TAG(dl, "args-list", [gen_param(AName, Type, ADesc, HTMLOutput)
[?TAG(dt, [?TAG_R(strong, atom_to_list(AName)), <<" :: ">>, || {{AName, Type}, ADesc} <- lists:zip(Args, ArgsDesc)])]
?RAW(io_lib:format("~p", [Type]))]),
?TAG(dd, ?RAW(ADesc))]
end, lists:zip(Args, ArgsDesc)))]
end, end,
ResultText = case ResultDesc of ResultText = case Result of
none -> {res,rescode} ->
[?RAW(io_lib:format("~p", [Result]))]; [?TAG(dl, [gen_param(res, integer,
_ -> "Status code (0 on success, 1 otherwise)",
[?TAG(dl, [ HTMLOutput)])];
?TAG(dt, io_lib:format("~p", [Result])), {res,restuple} ->
?TAG_R(dd, ResultDesc)])] [?TAG(dl, [gen_param(res, string,
"Raw result string",
HTMLOutput)])];
{RName, Type} ->
case ResultDesc of
none ->
[?TAG(ul, [gen_param(RName, Type, undefined, HTMLOutput)])];
_ ->
[?TAG(dl, [gen_param(RName, Type, ResultDesc, HTMLOutput)])]
end
end, end,
try try
[?TAG(h1, [?TAG(strong, atom_to_list(Name)), <<" - ">>, ?RAW(Desc)]), [?TAG(h1, [?TAG(strong, atom_to_list(Name)), <<" - ">>, ?RAW(Desc)]),
?TAG(p, ?RAW(LDesc)), ?TAG(p, ?RAW(LDesc)),
?TAG(h2, <<"Arguments:">>), ?TAG(h2, <<"Arguments:">>), ArgsText,
ArgsText, ?TAG(h2, <<"Result:">>), ResultText,
?TAG(h2, <<"Result:">>), ?TAG(h2, <<"Examples:">>), gen_calls(Cmd, HTMLOutput, Langs)]
ResultText,
?TAG(h2, <<"Examples:">>),
gen_calls(Cmd, HTMLOutput, Langs)]
catch catch
_:Ex -> _:Ex ->
throw(iolist_to_binary(io_lib:format( throw(iolist_to_binary(io_lib:format(