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