1%% This is taken from the code of distel.
2
3-module(fun_app).
4-export([html_index/2]). % , lines/3, curry/2]).
5
6html_index(file,Dir) ->
7  fold_file(curry(fun lines/3,Dir),[],filename:join([Dir,"doc","man_index.html"])).
8
9fold_file(Fun,Acc0,File) ->
10  {ok, FD} = file:open(File, [read]),
11  Acc = fold_file_lines(FD,Fun,Acc0),
12  file:close(FD),
13  Acc.
14
15fold_file_lines(FD,Fun,Acc) ->
16  case io:get_line(FD, "") of
17    eof -> Acc;
18    Line -> fold_file_lines(FD,Fun,Fun(trim_nl(Line),Acc))
19  end.
20
21trim_nl(Str) -> lists:reverse(tl(lists:reverse(Str))).
22
23lines(Line,_,Dir) ->
24  case string:tokens(Line, "<> \"") of
25    ["TD", "A", "HREF=", "../"++Href, M|_] ->
26      case filename:basename(Href, ".html") of
27	"index" -> ok;
28	M -> e_set({file,M}, filename:join([Dir,Href]))
29      end;
30    _ -> ok
31  end.
32
33e_set(Key,Val) -> ets:insert(?MODULE, {Key,Val}).
34
35curry(F, Arg) ->
36  case erlang:fun_info(F,arity) of
37    {_,1} -> fun() -> F(Arg) end;
38    {_,2} -> fun(A) -> F(A,Arg) end;
39    {_,3} -> fun(A,B) -> F(A,B,Arg) end;
40    {_,4} -> fun(A,B,C) -> F(A,B,C,Arg) end
41  end.
42