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