0001 function m2html(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 msgInvalidPair = 'Bad value for argument: ''%s''';
0102
0103 options = struct('verbose', 1,...
0104 'mFiles', {{'.'}},...
0105 'htmlDir', '.',...
0106 'recursive', 0,...
0107 'source', 1,...
0108 'syntaxHighlighting', 1,...
0109 'tabs', 4,...
0110 'globalHypertextLinks', 0,...
0111 'graph', 0,...
0112 'todo', 0,...
0113 'load', 0,...
0114 'save', 0,...
0115 'indexFile', 'index',...
0116 'extension', '.html',...
0117 'template', 'blue');
0118
0119 if nargin == 1 & isstruct(varargin{1})
0120 paramlist = [ fieldnames(varargin{1}) ...
0121 struct2cell(varargin{1}) ]';
0122 paramlist = { paramlist{:} };
0123 else
0124 if mod(nargin,2)
0125 error('Invalid parameter/value pair arguments.');
0126 end
0127 paramlist = varargin;
0128 end
0129
0130 optionsnames = lower(fieldnames(options));
0131 for i=1:2:length(paramlist)
0132 pname = paramlist{i};
0133 pvalue = paramlist{i+1};
0134 ind = strmatch(lower(pname),optionsnames);
0135 if isempty(ind)
0136 error(['Invalid parameter: ''' pname '''.']);
0137 elseif length(ind) > 1
0138 error(['Ambiguous parameter: ''' pname '''.']);
0139 end
0140 switch(optionsnames{ind})
0141 case 'verbose'
0142 if strcmpi(pvalue,'on')
0143 options.verbose = 1;
0144 elseif strcmpi(pvalue,'off')
0145 options.verbose = 0;
0146 else
0147 error(sprintf(msgInvalidPair,pname));
0148 end
0149 case 'mfiles'
0150 if iscellstr(pvalue)
0151 options.mFiles = pvalue;
0152 elseif ischar(pvalue)
0153 options.mFiles = cellstr(pvalue);
0154 else
0155 error(sprintf(msgInvalidPair,pname));
0156 end
0157 options.load = 0;
0158 case 'htmldir'
0159 if ischar(pvalue)
0160 if isempty(pvalue),
0161 options.htmlDir = '.';
0162 else
0163 options.htmlDir = pvalue;
0164 end
0165 else
0166 error(sprintf(msgInvalidPair,pname));
0167 end
0168 case 'recursive'
0169 if strcmpi(pvalue,'on')
0170 options.recursive = 1;
0171 elseif strcmpi(pvalue,'off')
0172 options.recursive = 0;
0173 else
0174 error(sprintf(msgInvalidPair,pname));
0175 end
0176 options.load = 0;
0177 case 'source'
0178 if strcmpi(pvalue,'on')
0179 options.source = 1;
0180 elseif strcmpi(pvalue,'off')
0181 options.source = 0;
0182 else
0183 error(sprintf(msgInvalidPair,pname));
0184 end
0185 case 'syntaxhighlighting'
0186 if strcmpi(pvalue,'on')
0187 options.syntaxHighlighting = 1;
0188 elseif strcmpi(pvalue,'off')
0189 options.syntaxHighlighting = 0;
0190 else
0191 error(sprintf(msgInvalidPair,pname));
0192 end
0193 case 'tabs'
0194 if pvalue >= 0
0195 options.tabs = pvalue;
0196 else
0197 error(sprintf(msgInvalidPair,pname));
0198 end
0199 case 'globalhypertextlinks'
0200 if strcmpi(pvalue,'on')
0201 options.globalHypertextLinks = 1;
0202 elseif strcmpi(pvalue,'off')
0203 options.globalHypertextLinks = 0;
0204 else
0205 error(sprintf(msgInvalidPair,pname));
0206 end
0207 options.load = 0;
0208 case 'graph'
0209 if strcmpi(pvalue,'on')
0210 options.graph = 1;
0211 elseif strcmpi(pvalue,'off')
0212 options.graph = 0;
0213 else
0214 error(sprintf(msgInvalidPair,pname));
0215 end
0216 case 'todo'
0217 if strcmpi(pvalue,'on')
0218 options.todo = 1;
0219 elseif strcmpi(pvalue,'off')
0220 options.todo = 0;
0221 else
0222 error(sprintf(msgInvalidPair,pname));
0223 end
0224 case 'load'
0225 if ischar(pvalue)
0226 try
0227 load(pvalue);
0228 catch
0229 error(sprintf('Unable to load %s.',pvalue));
0230 end
0231 options.load = 1;
0232 [dummy options.template] = fileparts(options.template);
0233 else
0234 error(sprintf(msgInvalidPair,pname));
0235 end
0236 case 'save'
0237 if strcmpi(pvalue,'on')
0238 options.save = 1;
0239 elseif strcmpi(pvalue,'off')
0240 options.save = 0;
0241 else
0242 error(sprintf(msgInvalidPair,pname));
0243 end
0244 case 'indexfile'
0245 if ischar(pvalue)
0246 options.indexFile = pvalue;
0247 else
0248 error(sprintf(msgInvalidPair,pname));
0249 end
0250 case 'extension'
0251 if ischar(pvalue) & pvalue(1) == '.'
0252 options.extension = pvalue;
0253 else
0254 error(sprintf(msgInvalidPair,pname));
0255 end
0256 case 'template'
0257 if ischar(pvalue)
0258 options.template = pvalue;
0259 else
0260 error(sprintf(msgInvalidPair,pname));
0261 end
0262 otherwise
0263 error(['Invalid parameter: ''' pname '''.']);
0264 end
0265 end
0266
0267
0268
0269
0270 s = fileparts(which(mfilename));
0271 options.template = fullfile(s,'templates',options.template);
0272 if exist(options.template) ~= 7
0273 error('[Template] Unknown template.');
0274 end
0275
0276
0277
0278
0279 if ~options.load
0280 mfiles = getmfiles(options.mFiles,{},options.recursive);
0281 if ~length(mfiles), fprintf('Nothing to be done.\n'); return; end
0282 if options.verbose,
0283 fprintf('Found %d M-files.\n',length(mfiles));
0284 end
0285 mfiles = sort(mfiles);
0286 end
0287
0288
0289
0290
0291 if ~options.load
0292 mdirs = {};
0293 names = {};
0294 for i=1:length(mfiles)
0295 [mdirs{i}, names{i}] = fileparts(mfiles{i});
0296 if isempty(mdirs{i}), mdirs{i} = '.'; end
0297 end
0298
0299 mdir = unique(mdirs);
0300 if options.verbose,
0301 fprintf('Found %d unique Matlab directories.\n',length(mdir));
0302 end
0303
0304 name = names;
0305
0306
0307
0308
0309 end
0310
0311
0312
0313
0314 if isempty(dir(options.htmlDir))
0315
0316 if options.verbose
0317 fprintf('Creating directory %s...\n',options.htmlDir);
0318 end
0319 if options.htmlDir(end) == filesep,
0320 options.htmlDir(end) = [];
0321 end
0322 [pathdir, namedir] = fileparts(options.htmlDir);
0323 if isempty(pathdir)
0324 [status, msg] = mkdir(namedir);
0325 else
0326 [status, msg] = mkdir(pathdir, namedir);
0327 end
0328 if ~status, error(msg); end
0329 end
0330
0331
0332
0333
0334 if ~options.load
0335 synopsis = cell(size(mfiles));
0336 h1line = cell(size(mfiles));
0337 subroutine = cell(size(mfiles));
0338 hrefs = sparse(length(mfiles),length(mfiles));
0339 todo = struct('mfile',[],'line',[],'comment',{{}});
0340 ismex = zeros(length(mfiles),length(mexexts));
0341
0342 for i=1:length(mfiles)
0343 s = mfileparse(mfiles{i}, mdirs, names, options);
0344 synopsis{i} = s.synopsis;
0345 h1line{i} = s.h1line;
0346 subroutine{i} = s.subroutine;
0347 hrefs(i,:) = s.hrefs;
0348 todo.mfile = [todo.mfile repmat(i,1,length(s.todo.line))];
0349 todo.line = [todo.line s.todo.line];
0350 todo.comment = {todo.comment{:} s.todo.comment{:}};
0351 ismex(i,:) = s.ismex;
0352 end
0353 hrefs = hrefs > 0;
0354 end
0355
0356
0357
0358
0359 matfilesave = 'm2html.mat';
0360
0361 if options.save
0362 if options.verbose
0363 fprintf('Saving MAT file %s...\n',matfilesave);
0364 end
0365 save(fullfile(options.htmlDir,matfilesave), ...
0366 'mfiles', 'names', 'mdirs', 'name', 'mdir', 'options', ...
0367 'hrefs', 'synopsis', 'h1line', 'subroutine', 'todo', 'ismex');
0368 end
0369
0370
0371
0372
0373 for i=1:length(mdir)
0374 if exist(fullfile(options.htmlDir,mdir{i})) ~= 7
0375 ldir = splitpath(mdir{i});
0376 for j=1:length(ldir)
0377 if exist(fullfile(options.htmlDir,ldir{1:j})) ~= 7
0378
0379 if options.verbose
0380 fprintf('Creating directory %s...\n',...
0381 fullfile(options.htmlDir,ldir{1:j}));
0382 end
0383 if j == 1
0384 [status, msg] = mkdir(options.htmlDir,ldir{1});
0385 else
0386 [status, msg] = mkdir(options.htmlDir,fullfile(ldir{1:j}));
0387 end
0388 error(msg);
0389 end
0390 end
0391 end
0392 end
0393
0394
0395
0396
0397 tpl_master = 'master.tpl';
0398 tpl_master_identifier_nbyline = 4;
0399
0400
0401 tpl = template(options.template,'remove');
0402 tpl = set(tpl,'file','TPL_MASTER',tpl_master);
0403 tpl = set(tpl,'block','TPL_MASTER','rowdir','rowdirs');
0404 tpl = set(tpl,'block','TPL_MASTER','idrow','idrows');
0405 tpl = set(tpl,'block','idrow','idcolumn','idcolumns');
0406
0407
0408 curfile = fullfile(options.htmlDir,[options.indexFile options.extension]);
0409 if options.verbose
0410 fprintf('Creating HTML file %s...\n',curfile);
0411 end
0412 fid = openfile(curfile,'w');
0413
0414
0415 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0416 datestr(now,13)]);
0417 tpl = set(tpl,'var','MASTERPATH', './');
0418 tpl = set(tpl,'var','DIRS', sprintf('%s ',mdir{:}));
0419
0420
0421 for i=1:length(mdir)
0422 tpl = set(tpl,'var','L_DIR',...
0423 fullurl(mdir{i},[options.indexFile options.extension]));
0424 tpl = set(tpl,'var','DIR',mdir{i});
0425 tpl = parse(tpl,'rowdirs','rowdir',1);
0426 end
0427
0428
0429 [sortnames, ind] = sort(names);
0430 m_mod = mod(length(sortnames), tpl_master_identifier_nbyline);
0431 ind = [ind zeros(1,tpl_master_identifier_nbyline-m_mod)];
0432 m_floor = floor(length(ind) / tpl_master_identifier_nbyline);
0433 ind = reshape(ind,m_floor,tpl_master_identifier_nbyline)';
0434
0435 for i=1:prod(size(ind))
0436 if ind(i)
0437 tpl = set(tpl,'var','L_IDNAME',...
0438 fullurl(mdirs{ind(i)},[names{ind(i)} options.extension]));
0439 tpl = set(tpl,'var','T_IDNAME',mdirs{ind(i)});
0440 tpl = set(tpl,'var','IDNAME',names{ind(i)});
0441 tpl = parse(tpl,'idcolumns','idcolumn',1);
0442 else
0443 tpl = set(tpl,'var','L_IDNAME','');
0444 tpl = set(tpl,'var','T_IDNAME','');
0445 tpl = set(tpl,'var','IDNAME','');
0446 tpl = parse(tpl,'idcolumns','idcolumn',1);
0447 end
0448 if mod(i,tpl_master_identifier_nbyline) == 0
0449 tpl = parse(tpl,'idrows','idrow',1);
0450 tpl = set(tpl,'var','idcolumns','');
0451 end
0452 end
0453
0454
0455 tpl = parse(tpl,'OUT','TPL_MASTER');
0456 fprintf(fid,'%s',get(tpl,'OUT'));
0457 fclose(fid);
0458
0459
0460
0461
0462
0463 d = dir(options.template);
0464 d = {d(~[d.isdir]).name};
0465
0466 for i=1:length(d)
0467 [p, n, ext] = fileparts(d{i});
0468 if ~strcmp(ext,'.tpl')
0469 if ~(exist(fullfile(options.htmlDir,d{i})))
0470 if options.verbose
0471 fprintf('Copying template file %s...\n',d{i});
0472 end
0473 [status, errmsg] = copyfile(fullfile(options.template,d{i}),...
0474 options.htmlDir);
0475 error(errmsg);
0476 end
0477 end
0478 end
0479
0480
0481
0482
0483 tpl_mdir = 'mdir.tpl';
0484 tpl_mdir_link = '<a href="%s">%s</a>';
0485 dotbase = 'graph';
0486
0487
0488 tpl = template(options.template,'remove');
0489 tpl = set(tpl,'file','TPL_MDIR',tpl_mdir);
0490 tpl = set(tpl,'block','TPL_MDIR','row-m','rows-m');
0491 tpl = set(tpl,'block','row-m','mexfile','mex');
0492 tpl = set(tpl,'block','TPL_MDIR','othermatlab','other');
0493 tpl = set(tpl,'block','othermatlab','row-other','rows-other');
0494 tpl = set(tpl,'block','TPL_MDIR','subfolder','subfold');
0495 tpl = set(tpl,'block','subfolder','subdir','subdirs');
0496 tpl = set(tpl,'block','TPL_MDIR','todolist','todolists');
0497 tpl = set(tpl,'block','TPL_MDIR','graph','graphs');
0498 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0499 datestr(now,13)]);
0500
0501 for i=1:length(mdir)
0502
0503 curfile = fullfile(options.htmlDir,mdir{i},...
0504 [options.indexFile options.extension]);
0505 if options.verbose
0506 fprintf('Creating HTML file %s...\n',curfile);
0507 end
0508 fid = openfile(curfile,'w');
0509
0510
0511 tpl = set(tpl,'var','INDEX', [options.indexFile options.extension]);
0512 tpl = set(tpl,'var','MASTERPATH',backtomaster(mdir{i}));
0513 tpl = set(tpl,'var','MDIR', mdir{i});
0514
0515
0516 tpl = set(tpl,'var','rows-m','');
0517 for j=1:length(mdirs)
0518 if strcmp(mdirs{j},mdir{i})
0519 tpl = set(tpl,'var','L_NAME', [names{j} options.extension]);
0520 tpl = set(tpl,'var','NAME', names{j});
0521 tpl = set(tpl,'var','H1LINE', h1line{j});
0522 if any(ismex(j,:))
0523 tpl = parse(tpl,'mex','mexfile');
0524 else
0525 tpl = set(tpl,'var','mex','');
0526 end
0527 tpl = parse(tpl,'rows-m','row-m',1);
0528 end
0529 end
0530
0531
0532 tpl = set(tpl,'var','other','');
0533 tpl = set(tpl,'var','rows-other','');
0534 w = what(mdir{i}); w = w(1);
0535 w = {w.mat{:} w.mdl{:} w.p{:}};
0536 for j=1:length(w)
0537 tpl = set(tpl,'var','OTHERFILE',w{j});
0538 tpl = parse(tpl,'rows-other','row-other',1);
0539 end
0540 if ~isempty(w)
0541 tpl = parse(tpl,'other','othermatlab');
0542 end
0543
0544
0545 tpl = set(tpl,'var','subdirs','');
0546 tpl = set(tpl,'var','subfold','');
0547 d = dir(mdir{i});
0548 d = {d([d.isdir]).name};
0549 d = {d{~ismember(d,{'.' '..'})}};
0550 for j=1:length(d)
0551 if ismember(fullfile(mdir{i},d{j}),mdir)
0552 tpl = set(tpl,'var','SUBDIRECTORY',...
0553 sprintf(tpl_mdir_link,...
0554 fullurl(d{j},[options.indexFile options.extension]),d{j}));
0555 else
0556 tpl = set(tpl,'var','SUBDIRECTORY',d{j});
0557 end
0558 tpl = parse(tpl,'subdirs','subdir',1);
0559 end
0560 if ~isempty(d)
0561 tpl = parse(tpl,'subfold','subfolder');
0562 end
0563
0564
0565 tpl = set(tpl,'var','todolists','');
0566 if options.todo
0567 if ~isempty(intersect(find(strcmp(mdir{i},mdirs)),todo.mfile))
0568 tpl = set(tpl,'var','LTODOLIST',['todo' options.extension]);
0569 tpl = parse(tpl,'todolists','todolist',1);
0570 end
0571 end
0572
0573
0574 tpl = set(tpl,'var','graphs','');
0575 if options.graph
0576 tpl = set(tpl,'var','LGRAPH',[dotbase options.extension]);
0577 tpl = parse(tpl,'graphs','graph',1);
0578 end
0579
0580
0581 tpl = parse(tpl,'OUT','TPL_MDIR');
0582 fprintf(fid,'%s',get(tpl,'OUT'));
0583 fclose(fid);
0584 end
0585
0586
0587
0588
0589 tpl_todo = 'todo.tpl';
0590
0591 if options.todo
0592
0593 tpl = template(options.template,'remove');
0594 tpl = set(tpl,'file','TPL_TODO',tpl_todo);
0595 tpl = set(tpl,'block','TPL_TODO','filelist','filelists');
0596 tpl = set(tpl,'block','filelist','row','rows');
0597 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0598 datestr(now,13)]);
0599
0600 for i=1:length(mdir)
0601 mfilestodo = intersect(find(strcmp(mdir{i},mdirs)),todo.mfile);
0602 if ~isempty(mfilestodo)
0603
0604 curfile = fullfile(options.htmlDir,mdir{i},...
0605 ['todo' options.extension]);
0606 if options.verbose
0607 fprintf('Creating HTML file %s...\n',curfile);
0608 end
0609 fid = openfile(curfile,'w');
0610
0611
0612 tpl = set(tpl,'var','INDEX',[options.indexFile options.extension]);
0613 tpl = set(tpl,'var','MASTERPATH', backtomaster(mdir{i}));
0614 tpl = set(tpl,'var','MDIR', mdir{i});
0615 tpl = set(tpl,'var','filelists', '');
0616
0617 for k=1:length(mfilestodo)
0618 tpl = set(tpl,'var','MFILE',names{mfilestodo(k)});
0619 tpl = set(tpl,'var','rows','');
0620 nbtodo = find(todo.mfile == mfilestodo(k));
0621 for l=1:length(nbtodo)
0622 tpl = set(tpl,'var','L_NBLINE',...
0623 [names{mfilestodo(k)} ...
0624 options.extension ...
0625 '#l' num2str(todo.line(nbtodo(l)))]);
0626 tpl = set(tpl,'var','NBLINE',num2str(todo.line(nbtodo(l))));
0627 tpl = set(tpl,'var','COMMENT',todo.comment{nbtodo(l)});
0628 tpl = parse(tpl,'rows','row',1);
0629 end
0630 tpl = parse(tpl,'filelists','filelist',1);
0631 end
0632
0633
0634 tpl = parse(tpl,'OUT','TPL_TODO');
0635 fprintf(fid,'%s',get(tpl,'OUT'));
0636 fclose(fid);
0637 end
0638 end
0639 end
0640
0641
0642
0643
0644 tpl_graph = 'graph.tpl';
0645 dot_exec = 'dot';
0646
0647
0648 if options.graph
0649
0650 tpl = template(options.template,'remove');
0651 tpl = set(tpl,'file','TPL_GRAPH',tpl_graph);
0652 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0653 datestr(now,13)]);
0654
0655 for i=1:length(mdir)
0656 mdotfile = fullfile(options.htmlDir,mdir{i},[dotbase '.dot']);
0657 if options.verbose
0658 fprintf('Creating dependency graph %s...',mdotfile);
0659 end
0660 ind = find(strcmp(mdirs,mdir{i}));
0661 href1 = zeros(length(ind),length(hrefs));
0662 for j=1:length(hrefs), href1(:,j) = hrefs(ind,j); end
0663 href2 = zeros(length(ind));
0664 for j=1:length(ind), href2(j,:) = href1(j,ind); end
0665 mdot({href2,{names{ind}},options,{mfiles{ind}}}, mdotfile);
0666 try
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681 eval(['!' dot_exec ' -Tcmap -Tpng ' mdotfile ...
0682 ' -o ' fullfile(options.htmlDir,mdir{i},[dotbase '.map']) ...
0683 ' -o ' fullfile(options.htmlDir,mdir{i},[dotbase '.png'])])
0684
0685 catch
0686 fprintf('failed.');
0687 end
0688 fprintf('\n');
0689 fid = openfile(fullfile(options.htmlDir,mdir{i},...
0690 [dotbase options.extension]),'w');
0691 tpl = set(tpl,'var','INDEX',[options.indexFile options.extension]);
0692 tpl = set(tpl,'var','MASTERPATH', backtomaster(mdir{i}));
0693 tpl = set(tpl,'var','MDIR', mdir{i});
0694 tpl = set(tpl,'var','GRAPH_IMG', [dotbase '.png']);
0695 fmap = openfile(fullfile(options.htmlDir,mdir{i},[dotbase '.map']),'r');
0696 tpl = set(tpl,'var','GRAPH_MAP', fscanf(fmap,'%c'));
0697 fclose(fmap);
0698 tpl = parse(tpl,'OUT','TPL_GRAPH');
0699 fprintf(fid,'%s', get(tpl,'OUT'));
0700 fclose(fid);
0701 end
0702 end
0703
0704
0705
0706
0707
0708 matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else', ...
0709 'end', 'for', 'function', 'global', 'if', 'otherwise', ...
0710 'persistent', 'return', 'switch', 'try', 'while'};
0711
0712
0713 tpl_mfile = 'mfile.tpl';
0714
0715 tpl_mfile_code = '<a href="%s" class="code" title="%s">%s</a>';
0716 tpl_mfile_keyword = '<span class="keyword">%s</span>';
0717 tpl_mfile_comment = '<span class="comment">%s</span>';
0718 tpl_mfile_string = '<span class="string">%s</span>';
0719 tpl_mfile_aname = '<a name="%s" href="#_subfunctions" class="code">%s</a>';
0720 tpl_mfile_line = '%04d %s\n';
0721
0722
0723 strtok_delim = sprintf(' \t\n\r(){}[]<>+-*~!|\\@&/,:;="''%%');
0724
0725
0726 tpl = template(options.template,'remove');
0727 tpl = set(tpl,'file','TPL_MFILE',tpl_mfile);
0728 tpl = set(tpl,'block','TPL_MFILE','pathline','pl');
0729 tpl = set(tpl,'block','TPL_MFILE','mexfile','mex');
0730 tpl = set(tpl,'block','TPL_MFILE','script','scriptfile');
0731 tpl = set(tpl,'block','TPL_MFILE','crossrefcall','crossrefcalls');
0732 tpl = set(tpl,'block','TPL_MFILE','crossrefcalled','crossrefcalleds');
0733 tpl = set(tpl,'block','TPL_MFILE','subfunction','subf');
0734 tpl = set(tpl,'block','subfunction','onesubfunction','onesubf');
0735 tpl = set(tpl,'block','TPL_MFILE','source','thesource');
0736 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0737 datestr(now,13)]);
0738
0739 for i=1:length(mdir)
0740 for j=1:length(mdirs)
0741 if strcmp(mdirs{j},mdir{i})
0742
0743 curfile = fullfile(options.htmlDir,mdir{i},...
0744 [names{j} options.extension]);
0745
0746
0747 if options.verbose
0748 fprintf('Creating HTML file %s...\n',curfile);
0749 end
0750 fid = openfile(curfile,'w');
0751
0752
0753 fid2 = openfile(mfiles{j},'r');
0754
0755
0756 tpl = set(tpl,'var','INDEX', [options.indexFile options.extension]);
0757 tpl = set(tpl,'var','MASTERPATH', backtomaster(mdir{i}));
0758 tpl = set(tpl,'var','MDIR', mdirs{j});
0759 tpl = set(tpl,'var','NAME', names{j});
0760 tpl = set(tpl,'var','H1LINE', h1line{j});
0761 tpl = set(tpl,'var','scriptfile', '');
0762 if isempty(synopsis{j})
0763 tpl = set(tpl,'var','SYNOPSIS',get(tpl,'var','script'));
0764 else
0765 tpl = set(tpl,'var','SYNOPSIS', synopsis{j});
0766 end
0767 s = splitpath(mdir{i});
0768 tpl = set(tpl,'var','pl','');
0769 for k=1:length(s)
0770 c = cell(1,k); for l=1:k, c{l} = filesep; end
0771 cpath = {s{1:k};c{:}}; cpath = [cpath{:}];
0772 if ~isempty(cpath), cpath = cpath(1:end-1); end
0773 if ismember(cpath,mdir)
0774 tpl = set(tpl,'var','LPATHDIR',[repmat('../',...
0775 1,length(s)-k) options.indexFile options.extension]);
0776 else
0777 tpl = set(tpl,'var','LPATHDIR','#');
0778 end
0779 tpl = set(tpl,'var','PATHDIR',s{k});
0780 tpl = parse(tpl,'pl','pathline',1);
0781 end
0782
0783
0784 tpl = set(tpl,'var','mex', '');
0785 samename = dir(fullfile(mdir{i},[names{j} '.*']));
0786 samename = {samename.name};
0787 for k=1:length(samename)
0788 [dummy, dummy, ext] = fileparts(samename{k});
0789 switch ext
0790 case '.c'
0791 tpl = set(tpl,'var','MEXTYPE', 'c');
0792 case {'.cpp' '.c++' '.cxx' '.C'}
0793 tpl = set(tpl,'var','MEXTYPE', 'c++');
0794 case {'.for' '.f' '.FOR' '.F'}
0795 tpl = set(tpl,'var','MEXTYPE', 'fortran');
0796 end
0797 end
0798 [exts, platform] = mexexts;
0799 mexplatforms = sprintf('%s, ',platform{find(ismex(j,:))});
0800 if ~isempty(mexplatforms)
0801 tpl = set(tpl,'var','PLATFORMS', mexplatforms(1:end-2));
0802 tpl = parse(tpl,'mex','mexfile');
0803 end
0804
0805
0806 descr = '';
0807 flagsynopcont = 0;
0808 flag_seealso = 0;
0809 while 1
0810 tline = fgets(fid2);
0811 if ~ischar(tline), break, end
0812 tline = entity(fliplr(deblank(fliplr(tline))));
0813
0814 if ~isempty(strmatch('function',tline))
0815 if ~isempty(strmatch('...',fliplr(deblank(tline))))
0816 flagsynopcont = 1;
0817 end
0818
0819 elseif ~isempty(strmatch('%',tline))
0820
0821 ind = findstr(lower(tline),'see also');
0822 if ~isempty(ind) | flag_seealso
0823
0824 indsamedir = find(strcmp(mdirs{j},mdirs));
0825 hrefnames = {names{indsamedir}};
0826 r = deblank(tline);
0827 flag_seealso = 1;
0828 tline = '';
0829 while 1
0830 [t,r,q] = strtok(r,sprintf(' \t\n\r.,;%%'));
0831 tline = [tline q];
0832 if isempty(t), break, end;
0833 ii = strcmpi(hrefnames,t);
0834 if any(ii)
0835 jj = find(ii);
0836 tline = [tline sprintf(tpl_mfile_code,...
0837 [hrefnames{jj(1)} options.extension],...
0838 synopsis{indsamedir(jj(1))},t)];
0839 else
0840 tline = [tline t];
0841 end
0842 end
0843 tline = sprintf('%s\n',tline);
0844 end
0845 descr = [descr tline(2:end)];
0846 elseif isempty(tline)
0847 if ~isempty(descr), break, end;
0848 else
0849 if flagsynopcont
0850 if isempty(strmatch('...',fliplr(deblank(tline))))
0851 flagsynopcont = 0;
0852 end
0853 else
0854 break;
0855 end
0856 end
0857 end
0858 tpl = set(tpl,'var','DESCRIPTION',...
0859 horztab(descr,options.tabs));
0860
0861
0862
0863 ind = find(hrefs(j,:) == 1);
0864 tpl = set(tpl,'var','crossrefcalls','');
0865 for k=1:length(ind)
0866 if strcmp(mdirs{j},mdirs{ind(k)})
0867 tpl = set(tpl,'var','L_NAME_CALL', ...
0868 [names{ind(k)} options.extension]);
0869 else
0870 tpl = set(tpl,'var','L_NAME_CALL', ...
0871 fullurl(backtomaster(mdirs{j}), ...
0872 mdirs{ind(k)}, ...
0873 [names{ind(k)} options.extension]));
0874 end
0875 tpl = set(tpl,'var','SYNOP_CALL', synopsis{ind(k)});
0876 tpl = set(tpl,'var','NAME_CALL', names{ind(k)});
0877 tpl = set(tpl,'var','H1LINE_CALL', h1line{ind(k)});
0878 tpl = parse(tpl,'crossrefcalls','crossrefcall',1);
0879 end
0880
0881 ind = find(hrefs(:,j) == 1);
0882 tpl = set(tpl,'var','crossrefcalleds','');
0883 for k=1:length(ind)
0884 if strcmp(mdirs{j},mdirs{ind(k)})
0885 tpl = set(tpl,'var','L_NAME_CALLED', ...
0886 [names{ind(k)} options.extension]);
0887 else
0888 tpl = set(tpl,'var','L_NAME_CALLED', ...
0889 fullurl(backtomaster(mdirs{j}),...
0890 mdirs{ind(k)}, ...
0891 [names{ind(k)} options.extension]));
0892 end
0893 tpl = set(tpl,'var','SYNOP_CALLED', synopsis{ind(k)});
0894 tpl = set(tpl,'var','NAME_CALLED', names{ind(k)});
0895 tpl = set(tpl,'var','H1LINE_CALLED', h1line{ind(k)});
0896 tpl = parse(tpl,'crossrefcalleds','crossrefcalled',1);
0897 end
0898
0899
0900 tpl = set(tpl,'var',{'subf' 'onesubf'},{'' ''});
0901 if ~isempty(subroutine{j}) & options.source
0902 for k=1:length(subroutine{j})
0903 tpl = set(tpl, 'var', 'L_SUB', ['#_sub' num2str(k)]);
0904 tpl = set(tpl, 'var', 'SUB', subroutine{j}{k});
0905 tpl = parse(tpl, 'onesubf', 'onesubfunction',1);
0906 end
0907 tpl = parse(tpl,'subf','subfunction');
0908 end
0909 subname = extractname(subroutine{j});
0910
0911
0912 if options.source & ~strcmpi(names{j},'contents')
0913 fseek(fid2,0,-1);
0914 it = 1;
0915 matlabsource = '';
0916 nbsubroutine = 1;
0917
0918 indhrefnames = find(hrefs(j,:) == 1);
0919 hrefnames = {names{indhrefnames}};
0920
0921 while 1
0922 tline = fgetl(fid2);
0923 if ~ischar(tline), break, end
0924 myline = '';
0925 splitc = splitcode(entity(tline));
0926 for k=1:length(splitc)
0927 if isempty(splitc{k})
0928 elseif ~isempty(strmatch('function',splitc{k}))
0929
0930 myline = [myline ...
0931 sprintf(tpl_mfile_aname,...
0932 ['_sub' num2str(nbsubroutine-1)],splitc{k})];
0933 nbsubroutine = nbsubroutine + 1;
0934 elseif splitc{k}(1) == ''''
0935 myline = [myline ...
0936 sprintf(tpl_mfile_string,splitc{k})];
0937 elseif splitc{k}(1) == '%'
0938 myline = [myline ...
0939 sprintf(tpl_mfile_comment,deblank(splitc{k}))];
0940 elseif ~isempty(strmatch('...',splitc{k}))
0941 myline = [myline sprintf(tpl_mfile_keyword,'...')];
0942 if ~isempty(splitc{k}(4:end))
0943 myline = [myline ...
0944 sprintf(tpl_mfile_comment,splitc{k}(4:end))];
0945 end
0946 else
0947
0948 r = splitc{k};
0949 while 1
0950 [t,r,q] = strtok(r,strtok_delim);
0951 myline = [myline q];
0952 if isempty(t), break, end;
0953
0954
0955 if options.syntaxHighlighting & ...
0956 any(strcmp(matlabKeywords,t))
0957 if strcmp('end',t)
0958 rr = fliplr(deblank(fliplr(r)));
0959 icomma = strmatch(',',rr);
0960 isemicolon = strmatch(';',rr);
0961 if ~(isempty(rr) | ~isempty([icomma isemicolon]))
0962 myline = [myline t];
0963 else
0964 myline = [myline sprintf(tpl_mfile_keyword,t)];
0965 end
0966 else
0967 myline = [myline sprintf(tpl_mfile_keyword,t)];
0968 end
0969 elseif any(strcmp(hrefnames,t))
0970 indt = indhrefnames(logical(strcmp(hrefnames,t)));
0971 flink = [t options.extension];
0972 ii = ismember({mdirs{indt}},mdirs{j});
0973 if ~any(ii)
0974
0975 flink = fullurl(backtomaster(mdirs{j}),...
0976 mdirs{indt(1)}, flink);
0977 else
0978 indt = indt(logical(ii));
0979 end
0980 myline = [myline sprintf(tpl_mfile_code,...
0981 flink, synopsis{indt(1)}, t)];
0982 elseif any(strcmp(subname,t))
0983 ii = find(strcmp(subname,t));
0984 myline = [myline sprintf(tpl_mfile_code,...
0985 ['#_sub' num2str(ii)],...
0986 ['sub' subroutine{j}{ii}],t)];
0987 else
0988 myline = [myline t];
0989 end
0990 end
0991 end
0992 end
0993 matlabsource = [matlabsource sprintf(tpl_mfile_line,it,myline)];
0994 it = it + 1;
0995 end
0996 tpl = set(tpl,'var','SOURCECODE',...
0997 horztab(matlabsource,options.tabs));
0998 tpl = parse(tpl,'thesource','source');
0999 else
1000 tpl = set(tpl,'var','thesource','');
1001 end
1002 tpl = parse(tpl,'OUT','TPL_MFILE');
1003 fprintf(fid,'%s',get(tpl,'OUT'));
1004 fclose(fid2);
1005 fclose(fid);
1006 end
1007 end
1008 end
1009
1010
1011 function mfiles = getmfiles(mdirs,mfiles,recursive)
1012
1013
1014 for i=1:length(mdirs)
1015 if exist(mdirs{i}) == 2
1016 mfiles{end+1} = mdirs{i};
1017 elseif exist(mdirs{i}) == 7
1018 w = what(mdirs{i});
1019 w = w(1);
1020 for j=1:length(w.m)
1021 mfiles{end+1} = fullfile(mdirs{i},w.m{j});
1022 end
1023 if recursive
1024 d = dir(mdirs{i});
1025 d = {d([d.isdir]).name};
1026 d = {d{~ismember(d,{'.' '..'})}};
1027 for j=1:length(d)
1028 mfiles = getmfiles(cellstr(fullfile(mdirs{i},d{j})),...
1029 mfiles,recursive);
1030 end
1031 end
1032 else
1033 fprintf('Warning: Unprocessed file %s.\n',mdirs{i});
1034 end
1035 end
1036
1037
1038 function s = backtomaster(mdir)
1039
1040
1041 ldir = splitpath(mdir);
1042 s = repmat('../',1,length(ldir));
1043
1044
1045 function ldir = splitpath(p)
1046
1047
1048 ldir = {};
1049 p = deblank(p);
1050 while 1
1051 [t,p] = strtok(p,filesep);
1052 if isempty(t), break; end
1053 if ~strcmp(t,'.')
1054 ldir{end+1} = t;
1055 end
1056 end
1057 if isempty(ldir)
1058 ldir{1} = '.';
1059 end
1060
1061
1062 function name = extractname(synopsis)
1063 if ischar(synopsis), synopsis = {synopsis}; end
1064 name = cell(size(synopsis));
1065 for i=1:length(synopsis)
1066 ind = findstr(synopsis{i},'=');
1067 if isempty(ind)
1068 ind = findstr(synopsis{i},'function');
1069 s = synopsis{i}(ind(1)+8:end);
1070 else
1071 s = synopsis{i}(ind(1)+1:end);
1072 end
1073 name{i} = strtok(s,[9:13 32 '(']);
1074 end
1075 if length(name) == 1, name = name{1}; end
1076
1077
1078 function f = fullurl(varargin)
1079
1080
1081 f = strrep(fullfile(varargin{:}),'\','/');
1082
1083
1084 function str = entity(str)
1085
1086
1087 str = strrep(str,'&','&');
1088 str = strrep(str,'<','<');
1089 str = strrep(str,'>','>');
1090 str = strrep(str,'"','"');
1091
1092
1093 function str = horztab(str,n)
1094
1095
1096
1097
1098 if n > 0
1099 str = strrep(str,sprintf('\t'),blanks(n));
1100 end