%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2012. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%%------------------------------------------------------------------
%%% Purpose:Convert Erlang files to html.
%%%------------------------------------------------------------------
-module(erl2html2).
-export([convert/2, convert/3]).
convert([], _Dest) -> % Fake clause.
ok;
convert(File, Dest) ->
%% The generated code uses the BGCOLOR attribute in the
%% BODY tag, which wasn't valid until HTML 3.2. Also,
%% good HTML should either override all colour attributes
%% or none of them -- *never* just a few.
%%
%% FIXME: The colours should *really* be set with
%% stylesheets...
Header = ["<!DOCTYPE HTML PUBLIC "
"\"-//W3C//DTD HTML 3.2 Final//EN\">\n"
"<!-- autogenerated by '"++atom_to_list(?MODULE)++"'. -->\n"
"<html>\n"
"<head><title>", File, "</title></head>\n\n"
"<body bgcolor=\"white\" text=\"black\""
" link=\"blue\" vlink=\"purple\" alink=\"red\">\n"],
convert(File, Dest, Header).
convert(File, Dest, Header) ->
%% statistics(runtime),
case parse_file(File) of
{ok,Functions} ->
%% io:format("~p~n",[Functions]),
case file:read_file(File) of
{ok,Bin} ->
{Html,Lines} = build_html(Bin,Functions),
Html1 = [Header,"<pre>\n",Html,"</pre>\n",
footer(Lines),"</body>\n</html>\n"],
file:write_file(Dest,Html1);
Error ->
Error
end;
Error ->
Error
end.
%%%-----------------------------------------------------------------
%%% Parse the input file to get the line numbers for all function
%%% definitions. This will be used when creating link targets for each
%%% function in build_html/5.
%%%
%%% All function clauses are also marked in order to allow
%%% possibly_enhance/2 to write these in bold.
parse_file(File) ->
case epp:parse_file(File,[],[]) of
{ok,Forms} ->
{ok,get_functions(Forms,File,false)};
Error ->
Error
end.
get_functions([{attribute,_,file,{File,_}}|Forms],File,_) ->
get_functions(Forms,File,true);
get_functions([{attribute,_,file,{_OtherFile,_}}|Forms],File,_) ->
get_functions(Forms,File,false);
get_functions([{function,L,F,A,[_|C]}|Forms],File,true) ->
Clauses = [{clause,CL} || {clause,CL,_,_,_} <- C],
[{atom_to_list(F),A,L} | Clauses] ++ get_functions(Forms,File,true);
get_functions([_|Forms],File,InCorrectFile) ->
get_functions(Forms,File,InCorrectFile);
get_functions([],_,_) ->
[].
%%%-----------------------------------------------------------------
%%% Add a link target for each line and one for each function definition.
build_html(Bin,Functions) ->
build_html(binary:split(Bin,<<"\n">>),1,Functions,false,[]).
build_html(Split,L,[{F,A,L}|Functions],_IsFuncDef,Acc) ->
FALink = http_uri:encode(F++"-"++integer_to_list(A)),
NewAcc = [Acc,"<a name=\"",FALink,"\"/>"],
build_html(Split,L,Functions,true,NewAcc);
build_html(Split,L,[{clause,L}|Functions],_IsFuncDef,Acc) ->
build_html(Split,L,Functions,true,Acc);
build_html([Line,Bin],L,Functions,IsFuncDef,Acc) ->
NewAcc = [Acc,line_number(L),line(binary_to_list(Line),IsFuncDef),$\n],
build_html(binary:split(Bin,<<"\n">>),L+1,Functions,false,NewAcc);
build_html([Line],L,Functions,IsFuncDef,Acc) ->
{[Acc,line_number(L),line(binary_to_list(Line),IsFuncDef)],L}.
line_number(L) ->
LStr = integer_to_list(L),
Pred =
case length(LStr) of
Length when Length < 5 ->
lists:duplicate(5-Length,$\s);
_ ->
[]
end,
["<a name=\"",LStr,"\"/>",Pred,LStr,": "].
line(Str,IsFuncDef) ->
Str1 = htmlize(Str),
possibly_enhance(Str1,IsFuncDef).
%%%-----------------------------------------------------------------
%%% Substitute special characters that should not appear in HTML
htmlize([$<|Str]) ->
[$&,$l,$t,$;|htmlize(Str)];
htmlize([$>|Str]) ->
[$&,$g,$t,$;|htmlize(Str)];
htmlize([$&|Str]) ->
[$&,$a,$m,$p,$;|htmlize(Str)];
htmlize([$"|Str]) ->
[$&,$q,$u,$o,$t,$;|htmlize(Str)];
htmlize([Ch|Str]) ->
[Ch|htmlize(Str)];
htmlize([]) ->
[].
%%%-----------------------------------------------------------------
%%% Write comments in italic and function definitions in bold.
possibly_enhance(Str,true) ->
case lists:splitwith(fun($() -> false; (_) -> true end, Str) of
{_,[]} -> Str;
{F,A} -> ["<b>",F,"</b>",A]
end;
possibly_enhance([$%|_]=Str,_) ->
["<i>",Str,"</i>"];
possibly_enhance([$-|_]=Str,_) ->
possibly_enhance(Str,true);
possibly_enhance(Str,false) ->
Str.
%%%-----------------------------------------------------------------
%%% End of the file
footer(_Lines) ->
"".
%% {_, Time} = statistics(runtime),
%% io:format("Converted ~p lines in ~.2f Seconds.~n",
%% [_Lines, Time/1000]),
%% S = "<i>The transformation of this file (~p lines) took ~.2f seconds</i>",
%% F = lists:flatten(io_lib:format(S, [_Lines, Time/1000])),
%% ["<hr size=1>",F,"<br>\n"].