) {
chop;
# If it's the function declaration line, then store it and skip
if (/^\s*function/) {
s/^\s*function\s*//;
$synopsis{$file} = $_;
$mtype{$file} = "function";
next;
}
# Compress multiple %'s to a single %
s/%+/%/g;
# Process comment lines and code lines separately
if (/^\s*%/) {
# cut out comment marker and surrounding white space
s/^\s*%\s*//;
# Store first comment line in lookfor
if (!$lookfor{$file}) {
$lookfor{$file} = $_;
}
# Split on nonalphanumerics, canonicalize to lower case
tr/A-Z/a-z/;
@words = grep(/[a-z]\w*/,split('\W',$_));
grep($hsymbols{$_}++,@words)
} else {
# Split off and ignore trailing comments
($statement,$comment) = split('%',$_,2);
# Split on nonalphanumerics, preserve case
@words = grep(/[a-zA-Z]\w*/,split('\W',$statement));
grep($csymbols{$_}++,@words)
}
}
close MFILE;
# Now mark each name that appears in the list of symbols
$dir = $mdir{$file};
if ($opt_g) {
foreach (grep($csymbols{$_},@names)) {
$ref{$file,$mfile{$_}} = 1;
}
foreach (grep($hsymbols{$_},@names)) {
$ref{$file,$mfile{$_}} = 2;
}
} else {
foreach (grep($csymbols{$_},@names)) {
$f = $mfile{$_};
next if ($mdir{$f} ne $dir);
$ref{$file,$f} = 1;
}
foreach (grep($hsymbols{$_},@names)) {
$f = $mfile{$_};
next if ($mdir{$f} ne $dir);
$ref{$file,$f} = 2;
}
}
undef(%csymbols);
undef(%hsymbols);
}
######################################################################
#
# Setup the html directories
#
######################################################################
# Create an html subdirectory name for every unique matlab directory in the
# list @mdirs. The name is constructed using the tail of the directory
# prefaced by a unique number.
#
# $hdir{$mdir} - html subdirectory for matlab directory $mdir
$x = 1;
foreach (@mdirs) {
@z = reverse(split("/",$_));
$hdir{$_} = "$x.".@z[0];
$x++;
}
# for each .m file, name a corresponding .html file
foreach (@mfiles) {
$hfile{$file} = $name{$_}.".html";
}
# Now test a build the corresponding html directories.
print "Checking HTML directories.\n" if $verbose;
if (!-e $hroot) {
mkdir($hroot,umask) || die("Cannot create directory $hroot\n");
chmod 0755, $hroot;
}
opendir(HDIR,$hroot) || die ("Cannot open directory $hroot\n");
closedir(HDIR);
die("HTML directory $hroot is not writable\n") if !-w $hroot;
print "HTML Directory $hroot is OK\n" if $verbose;
foreach (@mdirs) {
local($x) = $hroot."/".$hdir{$_};
if (!-e $x) {
mkdir($x,umask) || die("Cannot create directory $x\n");
chmod(0755,$x);
}
opendir(HDIR,$x) || die ("Cannot open directory $x\n");
closedir(HDIR);
die("HTML directory $x is not writable\n") if !-w $x;
print "HTML Directory $x is OK\n" if $verbose;
}
######################################################################
#
# Write the master index file
#
######################################################################
$indexfile = "$hroot/index.html";
print "Writing master $indexfile\n" if $verbose;
open(HFILE,">$indexfile") || die("Cannot open index file $indexfile\n");
print HFILE "Matlab Index\n";
print HFILE "\n";
print HFILE "Matlab Index
\n";
&tagline;
# Print a short introduction
# Print directory listing
print HFILE "
Matlab Directory Indices
\n\n";
print HFILE "
\n";
foreach $dir (@mdirs) {
print HFILE "- $dir
\n";
}
print HFILE "
\n";
# Include links to every file that was found
print HFILE "
Identifiers found in these directories
\n";
# We'll do this five across in alphabetical order
$i = 1;
foreach (@names) {
$b = " " x (15 - length($_));
$html = "$hdir{$mdir{$mfile{$_}}}/$_.html";
print HFILE "$_$b";
print HFILE "\n" if (0 == $i%5);
$i++;
}
print HFILE "
\n";
close(HFILE);
######################################################################
#
# Write an index for each html subdirectory
#
######################################################################
@readme = grep(/readme/i,@mfiles);
foreach $dir (@mdirs) {
$indexfile = "$hroot/$hdir{$dir}/index.html";
print "Writing an index file $indexfile\n" if $verbose;
open(HFILE,">$indexfile") || die("Cannot open index file $indexfile\n");
print HFILE "Index for Directory $dir\n";
print HFILE "\n";
print HFILE "[Return to Master Index]\n";
print HFILE "Index for $dir
\n";
&tagline;
# Now look for a Readme.m file, seemingly a Matlab standard. If there
# is one, then the help portion is included in the index file.
foreach $file (@readme) {
next if !($mdir{$file} eq $dir);
open(MFILE,$file) || die("Cannot open the file $file");
# Help Cross Reference information
undef(@zref);
foreach $_ (@mfiles) {
push(@zref,$name{$_}) if $ref{$file,$_} == 2;
}
# Look for the matlab help text block
$headline = "Readme";
&writehelpblock;
}
# Now write the index catalog for the .m files in this directory
print HFILE "
Matlab files in this Directory
\n\n";
foreach $file (@mfiles) {
next if $dir ne $mdir{$file};
$b = " " x (15 - length($name{$file}));
$html = $name{$file}.".html";
print HFILE "$name{$file}$b$lookfor{$file}\n";
}
print HFILE "
\n";
print HFILE "
\n";
close(HFILE);
}
######################################################################
#
# Write an html file for every m-file
#
######################################################################
# Now write the html file for each matlab file. Need to reread each matlab
# file to find the help text. Note that we can't do this in a single loop
# because we want the back reference information, and also some people
# put the help text before the function declarations.
# Need a list of mfiles with unique identifiers
@umfiles = values(%mfile);
foreach $file (@mfiles) {
$h = "$hroot/$hdir{$mdir{$file}}/$name{$file}.html";
print "Writing $h\n" if $verbose;
# Cross Reference information
# Find list of names.
undef(@xref);
undef(@yref);
undef(@zref);
foreach (@umfiles) {
next if ($name{$_} eq $name{$file});
push(@xref,$name{$_}) if $ref{$file,$_} == 1; # files we call
push(@yref,$name{$_}) if $ref{$_,$file} == 1; # files that call us
push(@zref,$name{$_}) if $ref{$file,$_} == 2; # files in the comment lines
}
open(MFILE,"<$file") || die("Cannot open $file");
open(HFILE,">$h") || die("Cannot open $h");
print HFILE "$hdir{$file}/$hfile{$file}\n";
print HFILE "\n";
print HFILE "[Index for $mdir{$file}]\n";
print HFILE "[Return to Master Index]\n";
print HFILE "$name{$file}
\n";
print HFILE "($mdir{$file}/$name{$file}.m)
\n";
# If this is a function, then write out the first line as a synposis
if ($mtype{$file}) {
print HFILE "
Function Synopsis
\n";
print HFILE "$synopsis{$file}
\n";
}
# Write the help block
$headline = "Help text";
&writehelpblock;
print HFILE "
Cross-Reference Information
" if (@xref || @yref);
if (@xref) {
print HFILE "This $mtype{$file} calls\n";
print HFILE "\n";
foreach $x (sort @xref) {
$html = "../$hdir{$mdir{$mfile{$x}}}/$x.html";
$b = " " x (15 - length("$x"));
print HFILE "- $x".$b."$mfile{$x}
\n";
}
print HFILE "
\n";
}
if (@yref) {
print HFILE "This $mtype{$file} is called by\n";
print HFILE "\n";
foreach $y (sort @yref) {
$html = "../$hdir{$mdir{$mfile{$y}}}/$y.html";
$b = " " x (15 - length("$y"));
print HFILE "- $y".$b."$mfile{$y}
\n";
}
print HFILE "
\n";
}
# Include source text if requested
if ($opt_i) {
print HFILE "
Listing of $mtype{$file} $mfile{$file}
\n";
seek(MFILE,0,0);
print HFILE "\n";
while () {
&htmlchar;
if (/^\s*%/) {
foreach $z (@zref) {
next if //;
$html = "../$hdir{$mdir{$mfile{$z}}}/$z.html";
s/(\W+)($z)(\W+)/$1$2<\/A>$3/gi;
}
} else {
foreach $x (@xref) {
next if //;
$html = "../$hdir{$mdir{$mfile{$x}}}/$x.html";
s/(\W+)($x)(\W+)/$1$2<\/A>$3/g;
s/^(\s*)($x)(\W+)/$1$2<\/A>$3/g;
}
}
print HFILE $_;
}
print HFILE "
\n";
}
# Print a date stamp
print HFILE "
\n";
&tagline;
print HFILE "";
close(MFILE);
close(HFILE);
}