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
0671 href1 = zeros(length(ind),length(hrefs));
0672 for j=1:length(hrefs), href1(:,j) = hrefs(ind,j); end
0673 href2 = zeros(length(ind));
0674 for j=1:length(ind), href2(j,:) = href1(j,ind); end
0675 mdot({href2,{names{ind}},options,{mfiles{ind}}}, mdotfile);
0676
0677 try
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692 eval(['!' dot_exec ' -Tcmap -Tpng ' mdotfile ...
0693 ' -o ' fullfile(options.htmlDir,mdir{i},[dotbase '.map']) ...
0694 ' -o ' fullfile(options.htmlDir,mdir{i},[dotbase '.png'])])
0695
0696 catch
0697 fprintf('failed.');
0698 end
0699 fprintf('\n');
0700 fid = openfile(fullfile(options.htmlDir,mdir{i},...
0701 [dotbase options.extension]),'w');
0702 tpl = set(tpl,'var','INDEX',[options.indexFile options.extension]);
0703 tpl = set(tpl,'var','MASTERPATH', backtomaster(mdir{i}));
0704 tpl = set(tpl,'var','MDIR', mdir{i});
0705 tpl = set(tpl,'var','GRAPH_IMG', [dotbase '.png']);
0706 fmap = openfile(fullfile(options.htmlDir,mdir{i},[dotbase '.map']),'r');
0707 tpl = set(tpl,'var','GRAPH_MAP', fscanf(fmap,'%c'));
0708 fclose(fmap);
0709 tpl = parse(tpl,'OUT','TPL_GRAPH');
0710 fprintf(fid,'%s', get(tpl,'OUT'));
0711 fclose(fid);
0712 end
0713 end
0714
0715
0716
0717
0718
0719 matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else', ...
0720 'end', 'for', 'function', 'global', 'if', 'otherwise', ...
0721 'persistent', 'return', 'switch', 'try', 'while'};
0722
0723
0724 tpl_mfile = 'mfile.tpl';
0725
0726 tpl_mfile_code = '<a href="%s" class="code" title="%s">%s</a>';
0727 tpl_mfile_keyword = '<span class="keyword">%s</span>';
0728 tpl_mfile_comment = '<span class="comment">%s</span>';
0729 tpl_mfile_string = '<span class="string">%s</span>';
0730 tpl_mfile_aname = '<a name="%s" href="#_subfunctions" class="code">%s</a>';
0731 tpl_mfile_line = '%04d %s\n';
0732
0733
0734 strtok_delim = sprintf(' \t\n\r(){}[]<>+-*~!|\\@&/,:;="''%%');
0735
0736
0737 tpl = template(options.template,'remove');
0738 tpl = set(tpl,'file','TPL_MFILE',tpl_mfile);
0739 tpl = set(tpl,'block','TPL_MFILE','pathline','pl');
0740 tpl = set(tpl,'block','TPL_MFILE','mexfile','mex');
0741 tpl = set(tpl,'block','TPL_MFILE','script','scriptfile');
0742 tpl = set(tpl,'block','TPL_MFILE','crossrefcall','crossrefcalls');
0743 tpl = set(tpl,'block','TPL_MFILE','crossrefcalled','crossrefcalleds');
0744 tpl = set(tpl,'block','TPL_MFILE','subfunction','subf');
0745 tpl = set(tpl,'block','subfunction','onesubfunction','onesubf');
0746 tpl = set(tpl,'block','TPL_MFILE','source','thesource');
0747 tpl = set(tpl,'var','DATE',[datestr(now,8) ' ' datestr(now,1) ' ' ...
0748 datestr(now,13)]);
0749
0750 for i=1:length(mdir)
0751 for j=1:length(mdirs)
0752 if strcmp(mdirs{j},mdir{i})
0753
0754 curfile = fullfile(options.htmlDir,mdir{i},...
0755 [names{j} options.extension]);
0756
0757
0758 if options.verbose
0759 fprintf('Creating HTML file %s...\n',curfile);
0760 end
0761 fid = openfile(curfile,'w');
0762
0763
0764 fid2 = openfile(mfiles{j},'r');
0765
0766
0767 tpl = set(tpl,'var','INDEX', [options.indexFile options.extension]);
0768 tpl = set(tpl,'var','MASTERPATH', backtomaster(mdir{i}));
0769 tpl = set(tpl,'var','MDIR', mdirs{j});
0770 tpl = set(tpl,'var','NAME', names{j});
0771 tpl = set(tpl,'var','H1LINE', h1line{j});
0772 tpl = set(tpl,'var','scriptfile', '');
0773 if isempty(synopsis{j})
0774 tpl = set(tpl,'var','SYNOPSIS',get(tpl,'var','script'));
0775 else
0776 tpl = set(tpl,'var','SYNOPSIS', synopsis{j});
0777 end
0778 s = splitpath(mdir{i});
0779 tpl = set(tpl,'var','pl','');
0780 for k=1:length(s)
0781 c = cell(1,k); for l=1:k, c{l} = filesep; end
0782 cpath = {s{1:k};c{:}}; cpath = [cpath{:}];
0783 if ~isempty(cpath), cpath = cpath(1:end-1); end
0784 if ismember(cpath,mdir)
0785 tpl = set(tpl,'var','LPATHDIR',[repmat('../',...
0786 1,length(s)-k) options.indexFile options.extension]);
0787 else
0788 tpl = set(tpl,'var','LPATHDIR','#');
0789 end
0790 tpl = set(tpl,'var','PATHDIR',s{k});
0791 tpl = parse(tpl,'pl','pathline',1);
0792 end
0793
0794
0795 tpl = set(tpl,'var','mex', '');
0796 samename = dir(fullfile(mdir{i},[names{j} '.*']));
0797 samename = {samename.name};
0798 for k=1:length(samename)
0799 [dummy, dummy, ext] = fileparts(samename{k});
0800 switch ext
0801 case '.c'
0802 tpl = set(tpl,'var','MEXTYPE', 'c');
0803 case {'.cpp' '.c++' '.cxx' '.C'}
0804 tpl = set(tpl,'var','MEXTYPE', 'c++');
0805 case {'.for' '.f' '.FOR' '.F'}
0806 tpl = set(tpl,'var','MEXTYPE', 'fortran');
0807 end
0808 end
0809 [exts, platform] = mexexts;
0810 mexplatforms = sprintf('%s, ',platform{find(ismex(j,:))});
0811 if ~isempty(mexplatforms)
0812 tpl = set(tpl,'var','PLATFORMS', mexplatforms(1:end-2));
0813 tpl = parse(tpl,'mex','mexfile');
0814 end
0815
0816
0817 descr = '';
0818 flagsynopcont = 0;
0819 flag_seealso = 0;
0820 while 1
0821 tline = fgets(fid2);
0822 if ~ischar(tline), break, end
0823 tline = entity(fliplr(deblank(fliplr(tline))));
0824
0825 if ~isempty(strmatch('function',tline))
0826 if ~isempty(strmatch('...',fliplr(deblank(tline))))
0827 flagsynopcont = 1;
0828 end
0829
0830 elseif ~isempty(strmatch('%',tline))
0831
0832 ind = findstr(lower(tline),'see also');
0833 if ~isempty(ind) | flag_seealso
0834
0835 indsamedir = find(strcmp(mdirs{j},mdirs));
0836 hrefnames = {names{indsamedir}};
0837 r = deblank(tline);
0838 flag_seealso = 1;
0839 tline = '';
0840 while 1
0841 [t,r,q] = strtok(r,sprintf(' \t\n\r.,;%%'));
0842 tline = [tline q];
0843 if isempty(t), break, end;
0844 ii = strcmpi(hrefnames,t);
0845 if any(ii)
0846 jj = find(ii);
0847 tline = [tline sprintf(tpl_mfile_code,...
0848 [hrefnames{jj(1)} options.extension],...
0849 synopsis{indsamedir(jj(1))},t)];
0850 else
0851 tline = [tline t];
0852 end
0853 end
0854 tline = sprintf('%s\n',tline);
0855 end
0856 descr = [descr tline(2:end)];
0857 elseif isempty(tline)
0858 if ~isempty(descr), break, end;
0859 else
0860 if flagsynopcont
0861 if isempty(strmatch('...',fliplr(deblank(tline))))
0862 flagsynopcont = 0;
0863 end
0864 else
0865 break;
0866 end
0867 end
0868 end
0869 tpl = set(tpl,'var','DESCRIPTION',...
0870 horztab(descr,options.tabs));
0871
0872
0873
0874 ind = find(hrefs(j,:) == 1);
0875 tpl = set(tpl,'var','crossrefcalls','');
0876 for k=1:length(ind)
0877 if strcmp(mdirs{j},mdirs{ind(k)})
0878 tpl = set(tpl,'var','L_NAME_CALL', ...
0879 [names{ind(k)} options.extension]);
0880 else
0881 tpl = set(tpl,'var','L_NAME_CALL', ...
0882 fullurl(backtomaster(mdirs{j}), ...
0883 mdirs{ind(k)}, ...
0884 [names{ind(k)} options.extension]));
0885 end
0886 tpl = set(tpl,'var','SYNOP_CALL', synopsis{ind(k)});
0887 tpl = set(tpl,'var','NAME_CALL', names{ind(k)});
0888 tpl = set(tpl,'var','H1LINE_CALL', h1line{ind(k)});
0889 tpl = parse(tpl,'crossrefcalls','crossrefcall',1);
0890 end
0891
0892 ind = find(hrefs(:,j) == 1);
0893 tpl = set(tpl,'var','crossrefcalleds','');
0894 for k=1:length(ind)
0895 if strcmp(mdirs{j},mdirs{ind(k)})
0896 tpl = set(tpl,'var','L_NAME_CALLED', ...
0897 [names{ind(k)} options.extension]);
0898 else
0899 tpl = set(tpl,'var','L_NAME_CALLED', ...
0900 fullurl(backtomaster(mdirs{j}),...
0901 mdirs{ind(k)}, ...
0902 [names{ind(k)} options.extension]));
0903 end
0904 tpl = set(tpl,'var','SYNOP_CALLED', synopsis{ind(k)});
0905 tpl = set(tpl,'var','NAME_CALLED', names{ind(k)});
0906 tpl = set(tpl,'var','H1LINE_CALLED', h1line{ind(k)});
0907 tpl = parse(tpl,'crossrefcalleds','crossrefcalled',1);
0908 end
0909
0910
0911 tpl = set(tpl,'var',{'subf' 'onesubf'},{'' ''});
0912 if ~isempty(subroutine{j}) & options.source
0913 for k=1:length(subroutine{j})
0914 tpl = set(tpl, 'var', 'L_SUB', ['#_sub' num2str(k)]);
0915 tpl = set(tpl, 'var', 'SUB', subroutine{j}{k});
0916 tpl = parse(tpl, 'onesubf', 'onesubfunction',1);
0917 end
0918 tpl = parse(tpl,'subf','subfunction');
0919 end
0920 subname = extractname(subroutine{j});
0921
0922
0923 if options.source & ~strcmpi(names{j},'contents')
0924 fseek(fid2,0,-1);
0925 it = 1;
0926 matlabsource = '';
0927 nbsubroutine = 1;
0928
0929 indhrefnames = find(hrefs(j,:) == 1);
0930 hrefnames = {names{indhrefnames}};
0931
0932 while 1
0933 tline = fgetl(fid2);
0934 if ~ischar(tline), break, end
0935 myline = '';
0936 splitc = splitcode(entity(tline));
0937 for k=1:length(splitc)
0938 if isempty(splitc{k})
0939 elseif ~isempty(strmatch('function',splitc{k}))
0940
0941 myline = [myline ...
0942 sprintf(tpl_mfile_aname,...
0943 ['_sub' num2str(nbsubroutine-1)],splitc{k})];
0944 nbsubroutine = nbsubroutine + 1;
0945 elseif splitc{k}(1) == ''''
0946 myline = [myline ...
0947 sprintf(tpl_mfile_string,splitc{k})];
0948 elseif splitc{k}(1) == '%'
0949 myline = [myline ...
0950 sprintf(tpl_mfile_comment,deblank(splitc{k}))];
0951 elseif ~isempty(strmatch('...',splitc{k}))
0952 myline = [myline sprintf(tpl_mfile_keyword,'...')];
0953 if ~isempty(splitc{k}(4:end))
0954 myline = [myline ...
0955 sprintf(tpl_mfile_comment,splitc{k}(4:end))];
0956 end
0957 else
0958
0959 r = splitc{k};
0960 while 1
0961 [t,r,q] = strtok(r,strtok_delim);
0962 myline = [myline q];
0963 if isempty(t), break, end;
0964
0965
0966 if options.syntaxHighlighting & ...
0967 any(strcmp(matlabKeywords,t))
0968 if strcmp('end',t)
0969 rr = fliplr(deblank(fliplr(r)));
0970 icomma = strmatch(',',rr);
0971 isemicolon = strmatch(';',rr);
0972 if ~(isempty(rr) | ~isempty([icomma isemicolon]))
0973 myline = [myline t];
0974 else
0975 myline = [myline sprintf(tpl_mfile_keyword,t)];
0976 end
0977 else
0978 myline = [myline sprintf(tpl_mfile_keyword,t)];
0979 end
0980 elseif any(strcmp(hrefnames,t))
0981 indt = indhrefnames(logical(strcmp(hrefnames,t)));
0982 flink = [t options.extension];
0983 ii = ismember({mdirs{indt}},mdirs{j});
0984 if ~any(ii)
0985
0986 flink = fullurl(backtomaster(mdirs{j}),...
0987 mdirs{indt(1)}, flink);
0988 else
0989 indt = indt(logical(ii));
0990 end
0991 myline = [myline sprintf(tpl_mfile_code,...
0992 flink, synopsis{indt(1)}, t)];
0993 elseif any(strcmp(subname,t))
0994 ii = find(strcmp(subname,t));
0995 myline = [myline sprintf(tpl_mfile_code,...
0996 ['#_sub' num2str(ii)],...
0997 ['sub' subroutine{j}{ii}],t)];
0998 else
0999 myline = [myline t];
1000 end
1001 end
1002 end
1003 end
1004 matlabsource = [matlabsource sprintf(tpl_mfile_line,it,myline)];
1005 it = it + 1;
1006 end
1007 tpl = set(tpl,'var','SOURCECODE',...
1008 horztab(matlabsource,options.tabs));
1009 tpl = parse(tpl,'thesource','source');
1010 else
1011 tpl = set(tpl,'var','thesource','');
1012 end
1013 tpl = parse(tpl,'OUT','TPL_MFILE');
1014 fprintf(fid,'%s',get(tpl,'OUT'));
1015 fclose(fid2);
1016 fclose(fid);
1017 end
1018 end
1019 end
1020
1021
1022 function mfiles = getmfiles(mdirs,mfiles,recursive)
1023
1024
1025 for i=1:length(mdirs)
1026 if exist(mdirs{i}) == 2
1027 mfiles{end+1} = mdirs{i};
1028 elseif exist(mdirs{i}) == 7
1029 w = what(mdirs{i});
1030 w = w(1);
1031 for j=1:length(w.m)
1032 mfiles{end+1} = fullfile(mdirs{i},w.m{j});
1033 end
1034 if recursive
1035 d = dir(mdirs{i});
1036 d = {d([d.isdir]).name};
1037 d = {d{~ismember(d,{'.' '..'})}};
1038 for j=1:length(d)
1039 mfiles = getmfiles(cellstr(fullfile(mdirs{i},d{j})),...
1040 mfiles,recursive);
1041 end
1042 end
1043 else
1044 fprintf('Warning: Unprocessed file %s.\n',mdirs{i});
1045 end
1046 end
1047
1048
1049 function s = backtomaster(mdir)
1050
1051
1052 ldir = splitpath(mdir);
1053 s = repmat('../',1,length(ldir));
1054
1055
1056 function ldir = splitpath(p)
1057
1058
1059 ldir = {};
1060 p = deblank(p);
1061 while 1
1062 [t,p] = strtok(p,filesep);
1063 if isempty(t), break; end
1064 if ~strcmp(t,'.')
1065 ldir{end+1} = t;
1066 end
1067 end
1068 if isempty(ldir)
1069 ldir{1} = '.';
1070 end
1071
1072
1073 function name = extractname(synopsis)
1074 if ischar(synopsis), synopsis = {synopsis}; end
1075 name = cell(size(synopsis));
1076 for i=1:length(synopsis)
1077 ind = findstr(synopsis{i},'=');
1078 if isempty(ind)
1079 ind = findstr(synopsis{i},'function');
1080 s = synopsis{i}(ind(1)+8:end);
1081 else
1082 s = synopsis{i}(ind(1)+1:end);
1083 end
1084 name{i} = strtok(s,[9:13 32 '(']);
1085 end
1086 if length(name) == 1, name = name{1}; end
1087
1088
1089 function f = fullurl(varargin)
1090
1091
1092 f = strrep(fullfile(varargin{:}),'\','/');
1093
1094
1095 function str = entity(str)
1096
1097
1098 str = strrep(str,'&','&');
1099 str = strrep(str,'<','<');
1100 str = strrep(str,'>','>');
1101 str = strrep(str,'"','"');
1102
1103
1104 function str = horztab(str,n)
1105
1106
1107
1108
1109 if n > 0
1110 str = strrep(str,sprintf('\t'),blanks(n));
1111 end