% TOHTML produces an html table from the plist.
% 
% CALL:
%            pl.tohtml; % table will be shown in document browser
%      txt = pl.tohtml; % stand-alone table for inclusion in html doc
% 
% M Hewitson 
%    16-09-10
% 
% $Id: tohtml.m,v 1.4 2011/04/08 08:56:21 hewitson Exp $
% 

function varargout = tohtml(pl, anchor)
  
  txt = '';
  
  name = pl.name;
  if isempty(name)
    name = 'Unknown Plist';
  end
  
  if nargin > 1 && ischar(anchor)
    txt = [txt sprintf('    <a name="%s"/>', anchor)];
  end
  txt = [txt sprintf('    <p><!-- Parameter List Table: %s -->\n', name)];
  txt = [txt sprintf('      <table cellspacing="0" class="body" cellpadding="4" summary="" width="100%%" border="2">\n')];
  txt = [txt sprintf('        <colgroup>\n')];
  txt = [txt sprintf('          <col width="15%%"/>\n')];
  txt = [txt sprintf('          <col width="20%%"/>\n')];
  txt = [txt sprintf('          <col width="20%%"/>\n')];
  txt = [txt sprintf('          <col width="45%%"/>\n')];
  txt = [txt sprintf('        </colgroup>\n')];
  
  txt = [txt sprintf('        <thead>\n')];
  txt = [txt sprintf('        	<tr valign="top">\n')];
  txt = [txt sprintf('        		<th bgcolor="#B9C6DD" colspan="4"><h3>%s</h3></th>\n',  name)];
  txt = [txt sprintf('      	  </tr>\n')];
  txt = [txt sprintf('        	<tr valign="top">\n')];
  txt = [txt sprintf('      	  	<th bgcolor="#D7D7D7">Key</th>\n')];
  txt = [txt sprintf('        		<th bgcolor="#D7D7D7">Default Value</th>\n')];
  txt = [txt sprintf('        		<th bgcolor="#D7D7D7">Options</th>\n')];
  txt = [txt sprintf('      	  	<th bgcolor="#D7D7D7">Description</th>\n')];
  txt = [txt sprintf('        	</tr>\n')];
  txt = [txt sprintf('        </thead>\n')];
  
  
  txt = [txt sprintf('        <tbody>\n')];
  for kk=1:pl.nparams
    txt = [txt sprintf('          <tr valign="top">\n')];
    txt = [txt sprintf('            <td bgcolor="#F2F2F2">%s</td>\n', pl.params(kk).key)];
    ptxt = display(pl.params(kk));
    txt = [txt sprintf('            <td bgcolor="#F2F2F2">%s</td>\n', strtrim(strrep(ptxt{3}, 'val:', '')))];
    if numel(pl.params(kk).getOptions) > 1
      opts = pl.params(kk).getOptions;
      optlist ='<ul>';
      for oo=1:numel(opts)
        optlist = [optlist '<li><font color="#1111FF">' utils.helper.val2str(opts{oo}) '</font></li>'];
      end
      optlist = [optlist '</ul>'];
      txt = [txt sprintf('            <td bgcolor="#F2F2F2">%s</td>\n',  optlist)];
    else
      txt = [txt sprintf('            <td bgcolor="#F2F2F2"><i>none</i></td>\n')];
    end
    txt = [txt sprintf('            <td bgcolor="#F2F2F2">%s</td>\n', char(strrep(pl.params(kk).desc, '\n', '<br></br>')))];
    txt = [txt sprintf('          </tr>\n')];
    
  end
  txt = [txt sprintf('        </tbody>\n')];
  txt = [txt sprintf('      </table>\n')];
  

  if nargout == 1
    varargout{1} = txt;
  else
    
    helpPath = utils.helper.getHelpPath();
    docStyleFile      = strcat('file://', helpPath, '/ug/docstyle.css');
    
    html = 'text://';
    
    % First the header table
    html = [html sprintf('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n')];
    html = [html sprintf('   "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">\n\n')];
    
    html = [html sprintf('<html lang="en">\n')];
    
    % Header definition
    html = [html sprintf('  <head>\n')];
    html = [html sprintf('    <title>Plist Report: %s</title>\n', name)];
    html = [html sprintf('    <link rel="stylesheet" type="text/css" href="file://%s">\n', docStyleFile)];
    html = [html sprintf('  </head>\n\n')];
    
    html = [html sprintf('  <body>\n\n')];
    
    html = [html txt]; % table
        
    html = [html sprintf('  </body>\n')];
    html = [html sprintf('</html>')];
    web(html, '-helpbrowser');
    
  end
  
  
end
