-module(erlman).
%% Module which generates an HTML page in current directory which provides
%% a convenient index over Erlang manpages. Also provides a shellscript to
%% do something similar. Generates 3 files in the current directory:
%% modules_search.html, modules.html and eman
%%
%% Typical use:
%%
%% erlman:go("/usr/local/doc/erlang").
%%
%% Then point your browser at the file "modules.html".
%%
%% -or-
%%
%% run the shellscript (after making it executable), e.g.
%%
%% eman lis
%%
-version('1.0').
-author('matthias@corelatus.se').
-export([go/0, go/1]).
-include_lib("kernel/include/file.hrl").
%% The argument is the directory where you unpacked the otp_html_R8.tar.gz
%% file.
go(Dir) ->
Search = list_to_binary(javascript(Dir)),
ok = file:write_file(target_filename(), Search),
Frames = list_to_binary(frames(Dir)),
ok = file:write_file(frame_filename(), Frames),
Shellscript = list_to_binary(shellscript(Dir)),
ok = file:write_file(shellscript_filename(), Shellscript).
%% This is where the HTML files happen to be on my machines.
go() ->
go("/usr/local/doc/erlang").
%% where to write the resulting HTML page
target_filename() ->
"modules_search.html".
frame_filename() ->
"modules.html".
shellscript_filename() ->
"eman".
%% Javascript which does the searching. Currently implemented as a linear
%% search, could be changed to binary search if speed is a problem
%% (it isn't on my machine).
javascript(Dir) ->
Format_fun = fun(Module, Canonical, Strip) ->
["\"", Module, " ",
string:substr(Canonical, Strip), "\","]
end,
["
Erlang Manpage Quick Index
"].
%% The Frame containing the search field and the page we'll look at
frames(Dir) ->
["
Module Index With Search
"].
shellscript(Dir) ->
FF = fun(Module, Canonical, _Strip) ->
[Module, " file://", Canonical, "\n"]
end,
["#!/bin/sh
search=^$1
page=`grep \"^$1.*[ ]\" $0 | head -n 1| cut -f 2 -d ' '`
echo $page
mozilla -remote \"openurl($page)\"
exit
echo \"we never reach this line\"
",
manpage_array(Dir, FF)].
%% Returns a list strings of form
%% "modulename canonical/path/modulename.html"
manpage_array(Dir, Format) ->
manpage_array(Dir, length(Dir) + 2, Format).
%% Strip is how much of the start of the filename to chop off
manpage_array(Dir, Strip, Format) ->
{ok, FilenameList} = file:list_dir(Dir),
F = fun(Filename, Acc) ->
Canonical = Dir ++ "/" ++ Filename,
{ok, Fileinfo} = file:read_file_info(Canonical),
case Fileinfo#file_info.type of
directory ->
possibly_recurse(Filename, Canonical, Strip, Acc, Format);
regular ->
possibly_include(Filename, Canonical, Strip, Acc, Format);
_ ->
Acc
end
end,
Megalist = lists:foldl(F, [], FilenameList).
%% We don't want to recurse into all directories, this lets us throw out some
possibly_recurse("permuted_index", Canonical, Strip, Acc, Format) ->
Acc;
possibly_recurse(_, Canonical, Strip, Acc, Format) ->
Acc ++ manpage_array(Canonical, Strip, Format).
%% We don't want all files
possibly_include("index.html", _, _, Acc, _Format) ->
Acc;
possibly_include("part_" ++ _, _, _, Acc, _Format) ->
Acc;
possibly_include(Filename, Canonical, Strip, Acc, Format) ->
case lists:reverse(Filename) of
"lmth." ++ _ -> %% .html ;-)
[Module|_] = string:tokens(Filename, "."),
[Format(Module, Canonical, Strip)|Acc];
_ ->
Acc
end.