function create_index_html(directory, destination_dir)
% create_index_html creates an html index file.
% A directory is chosen, all m-files are read (even the ones in
% subdirectories). The directory, function name and the first line are
% displayed. (Compareable to contents.m - but a bit more)
%
% Note: an existing index.html file is overwritten.
%
% Input:
% * directory ... directory for which to generate the index
% * destination_dir ... where to place index.html file
%
% Example
% Creates the index html file for the m-files in the current path and all
% its subdirectories.
% |create_index_html( pwd )|
%
% See also: wg_publish, wg_publish_dir,wg_publish_all
%
%% Signature
% Author: W.Garn
% E-Mail: wgarn@yahoo.com
% Date: 2005/12/01 20:00:00
%
% Copyright 2005 W.Garn
%
if nargin<1
%directory = pwd;
directory = uigetdir(pwd,'Select directory with m-functions');
end
if nargin<2
destination_dir = [directory '\html\'];
end
if ischar(directory)
logFlag=1;%clc;
% Collect data
[m_file, m_dir] = get_m_files(directory);
start = length(directory)+1;
progressbar(0,2);
title = {};name = {};href = {};contents = {};
for k=1:length(m_file)
rel_dir = m_dir{k}(start+1:length(m_dir{k}));
dest = [destination_dir rel_dir];
if logFlag, fprintf(1,'%3i: %s\n',k,[dest '\' m_file{k}]); end
pos = allSlashes(dest);
title{k} = dest(max(pos)+1:length(dest)); % directory before file name
name{k} = m_file{k}(1:length(m_file{k})-2); % without extension
if isempty(rel_dir)
href{k} = ['<a href="' name{k} '.html" source="blank">' name{k} '</a>'];
if logFlag, fprintf(1,'%3i: %s\n',k,[ name{k}]); end
else
href{k} = ['<a href="' rel_dir '/' name{k} '.html">' name{k} '</a>'];
if logFlag, fprintf(1,'%3i: %s\n',k,[rel_dir '\' name{k}]); end
end
contents{k} = getContents([m_dir{k} '/' m_file{k}], name{k});
if logFlag, fprintf(1,'Contents: %s\n',contents{k}); end
progressbar(k/length(m_file),2);
end
str{1} = '<html>';
pos = allSlashes(directory);
first_title = directory(max(pos)+1:length(directory)); % directory before file name
str{2} = [' <title> Index ' first_title ' </title>'];
str{2} = [' <h1> ' first_title ' </h1>'];
ctitle = title{1}; j = 3;
for k =1:length(m_file)
if ~strcmp(ctitle,title{k})
ctitle = title{k};
str{j} = ['<h2>' ctitle '</h2>'];
j=j+1;
end
str{j} = [href{k} ' ' contents{k} '<br>'];
j=j+1;
end
str{j} = '</html>';
str2file(str, 'index.html', destination_dir);
web([destination_dir '\index.html']);
end
%-------------------------------------------------
function contents = getContents(abs_file, file)
% existence was checked prior
contents='';
fid = fopen(abs_file,'r');
line_1 = fgetl(fid);
if ischar(line_1)
I = strfind(line_1,'function');
if ~isempty(I) % then it is a function
line_2 = fgetl(fid);
if ischar(line_2) % there is a line
str = strtrim(line_2);
if str(1)=='%' % then there is a comment
str = strtrim(str(2:length(str)));
if strcmp(lower(file), lower( str(1:length(file)))) % the first word in line is the file name
contents=str(length(file)+1:length(str));
else
contents=str;
end
end
end
end
end
fclose(fid);