Permalink
Browse files

new --gen-docsrc option, new option parser, fix for relative seealso …

…references
  • Loading branch information...
1 parent a7f2f95 commit 243932825d04618645706251d4de92c225063a5d @etrepum etrepum committed Jul 19, 2010
Showing with 67 additions and 32 deletions.
  1. +36 −26 erldocs
  2. +31 −6 src/erldocs.erl
View
62 erldocs
@@ -4,43 +4,53 @@
%% @doc Called automatically by escript
-spec main(list()) -> ok.
main(Args) ->
-
ok = file:set_cwd(filename:dirname(escript:script_name())),
ok = filelib:ensure_dir("ebin/"),
code:add_path("ebin"),
{ok, _} = compile:file("src/erldocs.erl", [{outdir, "ebin"}, verbose]),
- try dispatch(create_conf(sys_conf(Args)), Args)
- catch throw:Error ->
- log("Error running script:~n ~p",[Error])
+ Conf = create_conf(Args),
+ try erldocs:dispatch(Conf)
+ catch Type:Error ->
+ log("Error running script:~n~p~n~p~n",
+ [erlang:get_stacktrace(), {Type, Error}])
+ end.
+
+-spec parse_options([string()]) -> [{atom(), _}].
+parse_options(Args) ->
+ lists:flatmap(fun (Arg) -> parse_option(Arg) end, Args).
+
+-spec parse_option(string()) -> [{atom(), _}].
+parse_option("--sys-conf=" ++ Path) ->
+ [{sys_conf, Path}];
+parse_option("--gen-docsrc") ->
+ [{gen_docsrc, true}];
+parse_option("copystatic") ->
+ [{copystatic, true}];
+parse_option("--name=" ++ Name) ->
+ [{name, Name}];
+parse_option(Unknown) ->
+ throw({unknown_option, Unknown}).
+
+-spec getopt(atom(), [{atom(), _}], _) -> _.
+getopt(Key, Opts, Default) ->
+ case lists:keyfind(Key, 1, Opts) of
+ false ->
+ Default;
+ {_, V} ->
+ V
end.
-%% @doc Parses arguments passed to script and calls
-%% appropriate function.
--spec dispatch(list(), list()) -> ok.
-dispatch(Conf, ["copystatic" | _]) ->
- erldocs:copy_static_files(Conf);
-dispatch(Conf, _) ->
- Start = erlang:now(),
- erldocs:build(Conf),
- Diff = timer:now_diff(erlang:now(), Start),
- Mins = trunc(Diff * 1.667e-8),
- log("Woot, finished in ~p Minutes ~p Seconds~n",
- [Mins, trunc((Diff * 1.0e-6) - (Mins * 60))]).
-
-sys_conf([]) ->
- "priv/sys.conf";
-sys_conf(["--sys-conf=" ++ Path | _T]) ->
- Path;
-sys_conf([_H | T]) ->
- sys_conf(T).
-
-create_conf(Path) ->
+-spec create_conf([string()]) -> [{atom(), _}].
+create_conf(Args) ->
+ Opts = parse_options(Args),
+ Path = getopt(sys_conf, Opts, "priv/sys.conf"),
{ok, [Config]} = file:consult(Path),
{erldocs, Conf} = lists:keyfind(erldocs, 1, Config),
{ok, Root} = file:get_cwd(),
- [{root, Root} | Conf].
+ Opts ++ [{root, Root} | Conf].
+-spec log(string(), [_]) -> ok.
log(Str, Args) ->
io:format(Str, Args).
View
37 src/erldocs.erl
@@ -1,5 +1,5 @@
-module(erldocs).
--export([ copy_static_files/1, build/1 ]).
+-export([copy_static_files/1, build/1, dispatch/1]).
-export([mapreduce/4, pmapreduce/4, pmapreduce/5]).
-include_lib("kernel/include/file.hrl").
@@ -23,6 +23,23 @@ copy_file(Src, Dest) ->
ok
end.
+%% @doc Parses arguments passed to script and calls
+%% appropriate function.
+-spec dispatch(list()) -> ok.
+dispatch(Conf) ->
+ case lists:keyfind(copystatic, 1, Conf) of
+ {_, true} ->
+ copy_static_files(Conf);
+ false ->
+ Start = erlang:now(),
+ build(Conf),
+ Diff = timer:now_diff(erlang:now(), Start),
+ Mins = trunc(Diff * 1.667e-8),
+ log("Woot, finished in ~p Minutes ~p Seconds~n",
+ [Mins, trunc((Diff * 1.0e-6) - (Mins * 60))])
+ end.
+
+
%% @doc Build everything
-spec build(list()) -> ok.
build(Conf) ->
@@ -39,7 +56,7 @@ build(Conf) ->
ok = copy_static_files(Conf).
build_apps(Conf, Tpl, App, Index) ->
- Files = ensure_docsrc(App),
+ Files = ensure_docsrc(App, Conf),
AppName = bname(App),
log("Building ~s (~p files)~n", [AppName, length(Files)]),
Map = fun (F) -> build_file_map(Conf, Tpl, AppName, F) end,
@@ -88,11 +105,17 @@ build_file_map(Conf, Tpl, AppName, File) ->
strip_cos(Index) ->
[X || X = [_, App |_] <- Index, nomatch == re:run(App, "^cos") ].
-ensure_docsrc(AppDir) ->
- case filelib:is_dir(AppDir ++ "/doc/src/") of
- true -> filelib:wildcard(AppDir ++ "/doc/src/*.xml");
+ensure_docsrc(AppDir, Conf) ->
+ case lists:keyfind(gen_docsrc, 1, Conf) of
false ->
- log("No docs for ~s, attempting to build~n", [bname(AppDir)]),
+ case filelib:is_dir(AppDir ++ "/doc/src/") of
+ true -> filelib:wildcard(AppDir ++ "/doc/src/*.xml");
+ false ->
+ log("No docs for ~s, attempting to build~n", [bname(AppDir)]),
+ tmp_cd(AppDir ++ "/doc/src/", fun() -> gen_docsrc(AppDir) end)
+ end;
+ {_, true} ->
+ log("Attempting to build docs for ~s~n", [bname(AppDir)]),
tmp_cd(AppDir ++ "/doc/src/", fun() -> gen_docsrc(AppDir) end)
end.
@@ -191,6 +214,7 @@ render(erlref, App, Mod, Xml, Conf, Tpl) ->
ok = filelib:ensure_dir(filename:dirname(File) ++ "/"),
Acc = [{ids,[]}, {list, ul}, {functions, []}],
+
{[_Id, _List, {functions, Funs}], NXml}
= render(fun tr_erlref/2, Xml, Acc),
@@ -312,6 +336,7 @@ tr_erlref({v, [], Child}, _Acc) ->
{li, [], [{code, [], Child}]};
tr_erlref({seealso, [{marker, Marker}], Child}, _Acc) ->
N = case string:tokens(Marker, ":") of
+ [] -> add_html(lists:flatten(Child));
[Tmp] -> add_html(Tmp);
[Ap | Md] -> "../"++Ap++"/" ++ add_html(lists:flatten(Md))
end,

0 comments on commit 2439328

Please sign in to comment.