%PRCONTENT2HTML Construct HTML file from PRTools Contents file
%
%		PRCONTENT2HTML(NAME,RECREATE)


function out = prcontent2html(name,recreate)

if nargin < 2, recreate = 0; end

if exist([name '/Contents.m']) == 2
	file = which([name '/Contents.m']);
else
	error('Contents file not found')
end

t = gethf(file); % help part

t = strrep(t,char([10 13]),char(10));
t = strrep(t,char([13 10]),char(10));
t = strrep(t,char(13),char(10));
t = strrep(t,char(9),char([32 32]));
n = length(strfind(t,10));

s = ['<html>' website_manual '<head><title>' name '</title></head>' newline];

title = listn(t,1);
version = listn(t,2);
s = [s '<body bgcolor="#ffffff"><h3 align="center">' upper(name) '</h3>'];
s = [s '<h3 align="center">' title ', ' version '</h3>'];
for j=3:n                             % do for all lines
	r1 = listn(t,j);
	r2 = listn(t,j+1);
	u = cleanstr(r1);
	[tok,posttok] = strtok(u);
	ntok = abs(tok);
	single = isempty(strtok(posttok));
	emptyline = isempty(tok);
	if length(r1) > 2
		skip = (strcmp(r1(1:3),'---'));
	else
		skip = 0;
	end
	if length(r2) > 2
		section = strcmp(r2(1:3),'---');
	else
		section = 0;
	end
	
	if emptyline                          % start new paragraph
		s = [s '</p>' newline '<p>'];
	
	elseif section & ~skip
		if j < 6 % first section
			s = [s '<h3>' u '</h3>' newline];
		else  % following section, end old table first
			s = [s '</table>' newline '<h3>' u '</h3>' newline];
		end
		s = [s '<table cellspacing="0" cellpadding="0" width="100%"'...
					' align="center" border="0"'];       % start table
	elseif ~skip
		
		file = which(tok);
		pp = fileparts(file);
		[pp2,subdir] = fileparts(pp);
		s = [s '<tr> <td width="100" valign="top">' commandrefs(tok,recreate,subdir) '</td>'];
		s = [s '<td>' posttok '</td></tr>'];
	end
end
s = [s '</table>'];
s = [s '</p></body></html>'];
writf([name '.html'],s);
if nargout > 0
	out = s;
end

return

function i = isupper(s)
% true if s doesnot change by upper
i = strcmp(s,upper(s));
return

function out = code(s,n)
	%convert to code, add n spaces
	if nargin < 2, n = 0; end
	out = ['<font face="Courier">' htmlspace(n) s '</font>'];
return

function out = makecode(s,n)
	%convert any consequetive set of uppers to lower and make them Courier
	if strmatch(s(end),char(',','.'))
		send = s(end); s(end) = [];
	elseif length(s) > 1 & strmatch(s(end-1:end),char(', ','. '))
		send = s(end-1:end); s(end-1:end) = [];
	else
		send = [];
	end
	if nargin < 2, n = 0; end
	out = [];
	if isupper(s)
		%out = code(lower(s),n);
		out = code(s,n);
	else
		while ~isempty(s)
			[t,s] = strtok(s);
			if isupper(t)
				%t = code(lower(t));
				t = code(t);
			end
			out = [out ' ' t];
		end
	end
	out = [out send];
return
	
function s = cleanstr(s)
	V = char([9 10 11 12 13 32]);
	for n=1:length(s)
		if all(V~=s(n))
			break;
		end
	end
	s = s(n:end);
	for n=length(s):-1:1
		if all(V~=s(n))
			break
		end
	end
	s = s(1:n);
return

function s = commandrefs(r,recreate,subdir)
	if nargin < 2, recreate = 0; end
	if regexp(r,'^ *$')
		s = r;
		return
	end
	s = [];
	tok = lower(r);
	htmlname = fullfile(subdir,[tok '.html']);
	if recreate
		tt = prhelp2html(tok,1,subdir);
	end
	
	if exist(htmlname,'file') == 2
		s = [s '<a href="' htmlname '">' tok '</a>, '];
	elseif isempty(tok) | regexp(tok,'^ *')
		;
	else
		s = [s tok ', '];
	end
	s = strrep(s,'\','/'); % avoid  \ in html addresses
	
return


function s = htmlspace(n)
	if nargin < 1, n=1; end
	if n > 0
		s = repmat('&nbsp;',1,n);
	else
		s = [];
	end
return

%LISTN List lines specified by their line number
%
% t = listn(r,n)
% Get the lines in r given by the line numbers in n.
function t = listn(r,n)
k = [0,find(r==newline)];
t = [];
for j = n
	if j < length(k)
    t = [t,r(k(j)+1:k(j+1))];
	end
end
return


function s = gethf(file)
%GETHF Create heading file from m-file
%
%	s = gethf(file)
%
% The heading of the given m-file consisting of all starting % lines
% is isolated and returned in s.
%
%	gethf(dir)
%
% All headings of the files in dir are merged into a file named headings.doc.
% Default dir is cd.
%
% Use mergeprt for restoring
if nargin == 0
	gethf(cd);
end
if isdir(file)
	files = dir(file);
	docfile = ['headings.doc'];
	m = size(files,1);
	for i = 1:m
		if (~files(i,:).isdir) & (files(i,:).name(end-1:end) == '.m')
			name = files(i,:).name;
			s = gethf(name);
			s = ['##1' name(1:end-2) newline s newline '##2' newline];
			%disp(s)
			appendf(docfile,s);
		end
	end
else
	%disp(file)
	[s,ns] = readf(file);
	p = grep(s,'%'); 
	if isempty(p) % no comments found
		s = [];
		return 
	end
	p = [p p(end)+10];  % trick
	I = find(p - [0,p(1:length(p)-1)] ~= 1);
	if p(1) == 1 & isempty(I), I = length(p) + 1; end
%	if length(I) > 0 & I(1) > 1
	if length(I) > 0
		if I(1) == 1 
			I(1) = [];
		end
		n = I(1) - 1;
		s = listn(s,p(1):n+p(1)-1);
		s = strrep(s,[newline '%'],newline);
		%s = strrep(s,[newline ' '],newline);
		%s = strrep(s,[newline newline],'&*&');
		%s = strrep(s,[newline '	'],'*&*	');
		%s = strrep(s,newline,' ');
		%s = strrep(s,'&*&',[newline newline]);
		%s = strrep(s,'*&*',newline);
		s = s(2:end);
	else 
		s = '';
	end
end


function [r,n] = readf(file,newline)
%READF Readfile
%
% [r,n] = readf(file,newline)
% Reads file into string r. The number of lines
% is returned in n.
if nargin < 2, newline = 13; end
fid = fopen(deblank(file),'r');
if fid < 0
   error(['Cann''t open ' file])
end
r = fscanf(fid,'%c');
fclose(fid);
n = length(find(r==newline));
if r(length(r)) ~= newline, n = n + 1; end
return


function writf(file,r)
%WRITF Write file
%
% writf(file,r)
% Write file from string r
fid = fopen(file,'w');
if fid < 0
   error(['Cannot open file ' file])
end
fprintf(fid,'%c',r);
fclose(fid);
return



function [k,z] = grep(r,s)
%GREP Get line specific lines
%
% [k,n] = grep(r,s)
% Get the numbers of all lines in the set of lines r 
% that contain s.
% n is the total number of lines.
n = [0,find(r==newline)];
m = findstr(r,s);
[i,j] = sort([n,m]);;
q = [0,j(1:length(j)-1)]-j;
k = j(find(q>0))-1;
z = length(n)-1; % # of lines
return


function t = website_manual
t = ['<table border="0" cellspacing="0" cellpadding="3" width="100%" '...
	   'align="center"><tr><td><a href="//prtools.org/" target="_top">website</a>' ...
		 '</td><td><p align="right"><a '...
     'href="http://prtools.org/manual" target="_top">manual</a></p></td></tr></table></p>'];
return