[14] | 1 | %PRCONTENT2HTML Construct HTML file from PRTools Contents file
|
---|
| 2 | %
|
---|
[29] | 3 | % PRCONTENT2HTML(NAME,RECREATE,HTMLDIR)
|
---|
[14] | 4 |
|
---|
| 5 |
|
---|
[29] | 6 | function out = prcontent2html(name,recreate,htmldir)
|
---|
[14] | 7 |
|
---|
[29] | 8 | if nargin < 3, htmldir = cd; end
|
---|
| 9 | if nargin < 2 | isempty(recreate), recreate = 0; end
|
---|
[14] | 10 |
|
---|
| 11 | if exist([name '/Contents.m']) == 2
|
---|
| 12 | file = which([name '/Contents.m']);
|
---|
| 13 | else
|
---|
| 14 | error('Contents file not found')
|
---|
| 15 | end
|
---|
| 16 |
|
---|
| 17 | t = gethf(file); % help part
|
---|
| 18 |
|
---|
| 19 | t = strrep(t,char([10 13]),char(10));
|
---|
| 20 | t = strrep(t,char([13 10]),char(10));
|
---|
| 21 | t = strrep(t,char(13),char(10));
|
---|
| 22 | t = strrep(t,char(9),char([32 32]));
|
---|
| 23 | n = length(strfind(t,10));
|
---|
| 24 |
|
---|
| 25 | s = ['<html>' website_manual '<head><title>' name '</title></head>' newline];
|
---|
| 26 |
|
---|
| 27 | title = listn(t,1);
|
---|
| 28 | version = listn(t,2);
|
---|
| 29 | s = [s '<body bgcolor="#ffffff"><h3 align="center">' upper(name) '</h3>'];
|
---|
| 30 | s = [s '<h3 align="center">' title ', ' version '</h3>'];
|
---|
| 31 | for j=3:n % do for all lines
|
---|
| 32 | r1 = listn(t,j);
|
---|
| 33 | r2 = listn(t,j+1);
|
---|
| 34 | u = cleanstr(r1);
|
---|
| 35 | [tok,posttok] = strtok(u);
|
---|
| 36 | ntok = abs(tok);
|
---|
| 37 | single = isempty(strtok(posttok));
|
---|
| 38 | emptyline = isempty(tok);
|
---|
| 39 | if length(r1) > 2
|
---|
| 40 | skip = (strcmp(r1(1:3),'---'));
|
---|
| 41 | else
|
---|
| 42 | skip = 0;
|
---|
| 43 | end
|
---|
| 44 | if length(r2) > 2
|
---|
| 45 | section = strcmp(r2(1:3),'---');
|
---|
| 46 | else
|
---|
| 47 | section = 0;
|
---|
| 48 | end
|
---|
| 49 |
|
---|
| 50 | if emptyline % start new paragraph
|
---|
| 51 | s = [s '</p>' newline '<p>'];
|
---|
| 52 |
|
---|
| 53 | elseif section & ~skip
|
---|
| 54 | if j < 6 % first section
|
---|
| 55 | s = [s '<h3>' u '</h3>' newline];
|
---|
| 56 | else % following section, end old table first
|
---|
| 57 | s = [s '</table>' newline '<h3>' u '</h3>' newline];
|
---|
| 58 | end
|
---|
| 59 | s = [s '<table cellspacing="0" cellpadding="0" width="100%"'...
|
---|
| 60 | ' align="center" border="0"']; % start table
|
---|
| 61 | elseif ~skip
|
---|
| 62 |
|
---|
| 63 | file = which(tok);
|
---|
| 64 | pp = fileparts(file);
|
---|
| 65 | [pp2,subdir] = fileparts(pp);
|
---|
[29] | 66 | s = [s '<tr> <td width="100" valign="top">' commandrefs(tok,recreate,subdir,htmldir) '</td>'];
|
---|
[14] | 67 | s = [s '<td>' posttok '</td></tr>'];
|
---|
| 68 | end
|
---|
| 69 | end
|
---|
| 70 | s = [s '</table>'];
|
---|
| 71 | s = [s '</p></body></html>'];
|
---|
[29] | 72 | writf([fullfile(htmldir,name) '.html'],s);
|
---|
[14] | 73 | if nargout > 0
|
---|
| 74 | out = s;
|
---|
| 75 | end
|
---|
| 76 |
|
---|
| 77 | return
|
---|
| 78 |
|
---|
| 79 | function i = isupper(s)
|
---|
| 80 | % true if s doesnot change by upper
|
---|
| 81 | i = strcmp(s,upper(s));
|
---|
| 82 | return
|
---|
| 83 |
|
---|
| 84 | function out = code(s,n)
|
---|
| 85 | %convert to code, add n spaces
|
---|
| 86 | if nargin < 2, n = 0; end
|
---|
| 87 | out = ['<font face="Courier">' htmlspace(n) s '</font>'];
|
---|
| 88 | return
|
---|
| 89 |
|
---|
| 90 | function out = makecode(s,n)
|
---|
| 91 | %convert any consequetive set of uppers to lower and make them Courier
|
---|
| 92 | if strmatch(s(end),char(',','.'))
|
---|
| 93 | send = s(end); s(end) = [];
|
---|
| 94 | elseif length(s) > 1 & strmatch(s(end-1:end),char(', ','. '))
|
---|
| 95 | send = s(end-1:end); s(end-1:end) = [];
|
---|
| 96 | else
|
---|
| 97 | send = [];
|
---|
| 98 | end
|
---|
| 99 | if nargin < 2, n = 0; end
|
---|
| 100 | out = [];
|
---|
| 101 | if isupper(s)
|
---|
| 102 | %out = code(lower(s),n);
|
---|
| 103 | out = code(s,n);
|
---|
| 104 | else
|
---|
| 105 | while ~isempty(s)
|
---|
| 106 | [t,s] = strtok(s);
|
---|
| 107 | if isupper(t)
|
---|
| 108 | %t = code(lower(t));
|
---|
| 109 | t = code(t);
|
---|
| 110 | end
|
---|
| 111 | out = [out ' ' t];
|
---|
| 112 | end
|
---|
| 113 | end
|
---|
| 114 | out = [out send];
|
---|
| 115 | return
|
---|
| 116 |
|
---|
| 117 | function s = cleanstr(s)
|
---|
| 118 | V = char([9 10 11 12 13 32]);
|
---|
| 119 | for n=1:length(s)
|
---|
| 120 | if all(V~=s(n))
|
---|
| 121 | break;
|
---|
| 122 | end
|
---|
| 123 | end
|
---|
| 124 | s = s(n:end);
|
---|
| 125 | for n=length(s):-1:1
|
---|
| 126 | if all(V~=s(n))
|
---|
| 127 | break
|
---|
| 128 | end
|
---|
| 129 | end
|
---|
| 130 | s = s(1:n);
|
---|
| 131 | return
|
---|
| 132 |
|
---|
[29] | 133 | function s = commandrefs(r,recreate,subdir,htmldir)
|
---|
[14] | 134 | if nargin < 2, recreate = 0; end
|
---|
| 135 | if regexp(r,'^ *$')
|
---|
| 136 | s = r;
|
---|
| 137 | return
|
---|
| 138 | end
|
---|
| 139 | s = [];
|
---|
| 140 | tok = lower(r);
|
---|
| 141 | htmlname = fullfile(subdir,[tok '.html']);
|
---|
| 142 | if recreate
|
---|
| 143 | tt = prhelp2html(tok,1,subdir);
|
---|
| 144 | end
|
---|
| 145 |
|
---|
[29] | 146 | if exist(fullfile(htmldir,htmlname),'file') == 2
|
---|
[14] | 147 | s = [s '<a href="' htmlname '">' tok '</a>, '];
|
---|
| 148 | elseif isempty(tok) | regexp(tok,'^ *')
|
---|
| 149 | ;
|
---|
| 150 | else
|
---|
| 151 | s = [s tok ', '];
|
---|
| 152 | end
|
---|
| 153 | s = strrep(s,'\','/'); % avoid \ in html addresses
|
---|
| 154 |
|
---|
| 155 | return
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | function s = htmlspace(n)
|
---|
| 159 | if nargin < 1, n=1; end
|
---|
| 160 | if n > 0
|
---|
| 161 | s = repmat(' ',1,n);
|
---|
| 162 | else
|
---|
| 163 | s = [];
|
---|
| 164 | end
|
---|
| 165 | return
|
---|
| 166 |
|
---|
| 167 | %LISTN List lines specified by their line number
|
---|
| 168 | %
|
---|
| 169 | % t = listn(r,n)
|
---|
| 170 | % Get the lines in r given by the line numbers in n.
|
---|
| 171 | function t = listn(r,n)
|
---|
| 172 | k = [0,find(r==newline)];
|
---|
| 173 | t = [];
|
---|
| 174 | for j = n
|
---|
| 175 | if j < length(k)
|
---|
| 176 | t = [t,r(k(j)+1:k(j+1))];
|
---|
| 177 | end
|
---|
| 178 | end
|
---|
| 179 | return
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | function s = gethf(file)
|
---|
| 183 | %GETHF Create heading file from m-file
|
---|
| 184 | %
|
---|
| 185 | % s = gethf(file)
|
---|
| 186 | %
|
---|
| 187 | % The heading of the given m-file consisting of all starting % lines
|
---|
| 188 | % is isolated and returned in s.
|
---|
| 189 | %
|
---|
| 190 | % gethf(dir)
|
---|
| 191 | %
|
---|
| 192 | % All headings of the files in dir are merged into a file named headings.doc.
|
---|
| 193 | % Default dir is cd.
|
---|
| 194 | %
|
---|
| 195 | % Use mergeprt for restoring
|
---|
| 196 | if nargin == 0
|
---|
| 197 | gethf(cd);
|
---|
| 198 | end
|
---|
| 199 | if isdir(file)
|
---|
| 200 | files = dir(file);
|
---|
| 201 | docfile = ['headings.doc'];
|
---|
| 202 | m = size(files,1);
|
---|
| 203 | for i = 1:m
|
---|
| 204 | if (~files(i,:).isdir) & (files(i,:).name(end-1:end) == '.m')
|
---|
| 205 | name = files(i,:).name;
|
---|
| 206 | s = gethf(name);
|
---|
| 207 | s = ['##1' name(1:end-2) newline s newline '##2' newline];
|
---|
| 208 | %disp(s)
|
---|
| 209 | appendf(docfile,s);
|
---|
| 210 | end
|
---|
| 211 | end
|
---|
| 212 | else
|
---|
| 213 | %disp(file)
|
---|
| 214 | [s,ns] = readf(file);
|
---|
| 215 | p = grep(s,'%');
|
---|
| 216 | if isempty(p) % no comments found
|
---|
| 217 | s = [];
|
---|
| 218 | return
|
---|
| 219 | end
|
---|
| 220 | p = [p p(end)+10]; % trick
|
---|
| 221 | I = find(p - [0,p(1:length(p)-1)] ~= 1);
|
---|
| 222 | if p(1) == 1 & isempty(I), I = length(p) + 1; end
|
---|
| 223 | % if length(I) > 0 & I(1) > 1
|
---|
| 224 | if length(I) > 0
|
---|
| 225 | if I(1) == 1
|
---|
| 226 | I(1) = [];
|
---|
| 227 | end
|
---|
| 228 | n = I(1) - 1;
|
---|
| 229 | s = listn(s,p(1):n+p(1)-1);
|
---|
| 230 | s = strrep(s,[newline '%'],newline);
|
---|
| 231 | %s = strrep(s,[newline ' '],newline);
|
---|
| 232 | %s = strrep(s,[newline newline],'&*&');
|
---|
| 233 | %s = strrep(s,[newline ' '],'*&* ');
|
---|
| 234 | %s = strrep(s,newline,' ');
|
---|
| 235 | %s = strrep(s,'&*&',[newline newline]);
|
---|
| 236 | %s = strrep(s,'*&*',newline);
|
---|
| 237 | s = s(2:end);
|
---|
| 238 | else
|
---|
| 239 | s = '';
|
---|
| 240 | end
|
---|
| 241 | end
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | function [r,n] = readf(file,newline)
|
---|
| 245 | %READF Readfile
|
---|
| 246 | %
|
---|
| 247 | % [r,n] = readf(file,newline)
|
---|
| 248 | % Reads file into string r. The number of lines
|
---|
| 249 | % is returned in n.
|
---|
| 250 | if nargin < 2, newline = 13; end
|
---|
| 251 | fid = fopen(deblank(file),'r');
|
---|
| 252 | if fid < 0
|
---|
| 253 | error(['Cann''t open ' file])
|
---|
| 254 | end
|
---|
| 255 | r = fscanf(fid,'%c');
|
---|
| 256 | fclose(fid);
|
---|
| 257 | n = length(find(r==newline));
|
---|
| 258 | if r(length(r)) ~= newline, n = n + 1; end
|
---|
| 259 | return
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | function writf(file,r)
|
---|
| 263 | %WRITF Write file
|
---|
| 264 | %
|
---|
| 265 | % writf(file,r)
|
---|
| 266 | % Write file from string r
|
---|
| 267 | fid = fopen(file,'w');
|
---|
| 268 | if fid < 0
|
---|
| 269 | error(['Cannot open file ' file])
|
---|
| 270 | end
|
---|
| 271 | fprintf(fid,'%c',r);
|
---|
| 272 | fclose(fid);
|
---|
| 273 | return
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | function [k,z] = grep(r,s)
|
---|
| 278 | %GREP Get line specific lines
|
---|
| 279 | %
|
---|
| 280 | % [k,n] = grep(r,s)
|
---|
| 281 | % Get the numbers of all lines in the set of lines r
|
---|
| 282 | % that contain s.
|
---|
| 283 | % n is the total number of lines.
|
---|
| 284 | n = [0,find(r==newline)];
|
---|
| 285 | m = findstr(r,s);
|
---|
| 286 | [i,j] = sort([n,m]);;
|
---|
| 287 | q = [0,j(1:length(j)-1)]-j;
|
---|
| 288 | k = j(find(q>0))-1;
|
---|
| 289 | z = length(n)-1; % # of lines
|
---|
| 290 | return
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | function t = website_manual
|
---|
| 294 | t = ['<table border="0" cellspacing="0" cellpadding="3" width="100%" '...
|
---|
| 295 | 'align="center"><tr><td><a href="//prtools.org/" target="_top">website</a>' ...
|
---|
| 296 | '</td><td><p align="right"><a '...
|
---|
| 297 | 'href="http://prtools.org/manual" target="_top">manual</a></p></td></tr></table></p>'];
|
---|
| 298 | return |
---|