home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / CHMdefaultConverter.inc < prev    next >
Encoding:
Text File  |  2004-03-24  |  65.9 KB  |  1,683 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | phpDocumentor                                                          |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2000-2003 Joshua Eichorn, Gregory Beaver                 |
  7. // | Email         jeichorn@phpdoc.org, cellog@phpdoc.org                   |
  8. // | Web           http://www.phpdoc.org                                    |
  9. // | Mirror        http://phpdocu.sourceforge.net/                          |
  10. // | PEAR          http://pear.php.net/package-info.php?pacid=137           |
  11. // +------------------------------------------------------------------------+
  12. // | This source file is subject to version 3.00 of the PHP License,        |
  13. // | that is available at http://www.php.net/license/3_0.txt.               |
  14. // | If you did not receive a copy of the PHP license and are unable to     |
  15. // | obtain it through the world-wide-web, please send a note to            |
  16. // | license@php.net so we can mail you a copy immediately.                 |
  17. // +------------------------------------------------------------------------+
  18. //
  19. /**
  20.  * CHM (Compiled Help Manual) output converter for Smarty Template.
  21.  *
  22.  * @package Converters
  23.  * @subpackage CHMdefault
  24.  * @author Greg Beaver <cellog@users.sourceforge.net>
  25.  * @since 1.0rc1
  26.  * @version $Id: CHMdefaultConverter.inc,v 1.33.2.8 2003/07/30 00:39:45 CelloG Exp $
  27.  */
  28. /**
  29.  * Generates files that MS HTML Help Worshop can use to create a MS Windows
  30.  * compiled help file (CHM)
  31.  *
  32.  * The free MS HTML Help compiler takes the project file (phpdoc.hhp) and reads
  33.  * the table of contents file specified in the project (which is always contents.hhc
  34.  * in phpDocumentor).  When the converter reaches stable state, it will also
  35.  * output an index file index.hhk.  The free download for MS HTML Help Workshop
  36.  * is available below
  37.  * @link http://www.microsoft.com/downloads/release.asp?releaseid=33071 MS HTML Help Workshop download
  38.  * @package Converters
  39.  * @subpackage CHMdefault
  40.  * @author Greg Beaver <cellog@users.sourceforge.net>
  41.  * @since 1.0rc1
  42.  * @version $Revision: 1.33.2.8 $
  43.  */
  44. class CHMdefaultConverter extends Converter
  45. {
  46.     /**
  47.      * CHMdefaultConverter wants elements sorted by type as well as alphabetically
  48.      * @see Converter::$sort_page_contents_by_type
  49.      * @var boolean
  50.      */
  51.     var $sort_page_contents_by_type = true;
  52.     /** @var string */
  53.     var $outputformat = 'CHM';
  54.     /** @var string */
  55.     var $name = 'default';
  56.     /**
  57.      * indexes of elements by package that need to be generated
  58.      * @var array
  59.      */
  60.     var $leftindex = array('classes' => true, 'pages' => true, 'functions' => true, 'defines' => false, 'globals' => false);
  61.     
  62.     /**
  63.      * output directory for the current procedural page being processed
  64.      * @var string
  65.      */
  66.     var $page_dir;
  67.     
  68.     /**
  69.      * target directory passed on the command-line.
  70.      * {@link $targetDir} is malleable, always adding package/ and package/subpackage/ subdirectories onto it.
  71.      * @var string
  72.      */
  73.     var $base_dir;
  74.     
  75.     /**
  76.      * output directory for the current class being processed
  77.      * @var string
  78.      */
  79.     var $class_dir;
  80.     
  81.     /**
  82.      * array of converted package page names.
  83.      * Used to link to the package page in the left index
  84.      * @var array Format: array(package => 1)
  85.      */
  86.     var $package_pages = array();
  87.     
  88.     /**
  89.      * controls formatting of parser informative output
  90.      * 
  91.      * Converter prints:
  92.      * "Converting /path/to/file.php... Procedural Page Elements... Classes..."
  93.      * Since CHMdefaultConverter outputs files while converting, it needs to send a \n to start a new line.  However, if there
  94.      * is more than one class, output is messy, with multiple \n's just between class file output.  This variable prevents that
  95.      * and is purely cosmetic
  96.      * @var boolean
  97.      */
  98.     var $juststarted = false;
  99.     
  100.     /**
  101.      * contains all of the template procedural page element loop data needed for the current template
  102.      * @var array
  103.      */
  104.     var $current;
  105.     
  106.     /**
  107.      * contains all of the template class element loop data needed for the current template
  108.      * @var array
  109.      */
  110.     var $currentclass;
  111.     var $wrote = false;
  112.     var $ric_set = array();
  113.     /**
  114.      * Table of Contents entry for index.hhk
  115.      * @var array
  116.      */
  117.     var $KLinks = array();
  118.  
  119.     /**
  120.      * sets {@link $base_dir} to $targetDir
  121.      * @see Converter()
  122.      */
  123.     function CHMdefaultConverter(&$allp, &$packp, &$classes, &$procpages, $po, $pp, $qm, $targetDir, $templateDir, $title)
  124.     {
  125.         Converter::Converter($allp, $packp, $classes, $procpages,$po, $pp, $qm, $targetDir, $templateDir, $title);
  126.         $this->base_dir = $targetDir;
  127.     }
  128.     
  129.     /**
  130.      * @deprecated in favor of PHP 4.3.0+ tokenizer-based source highlighting
  131.      */
  132.     function unmangle($sourcecode)
  133.     {
  134.         $sourcecode = str_replace('<code>','<pre>',$sourcecode);
  135.         $sourcecode = str_replace('</code>','</pre>',$sourcecode);
  136.         $sourcecode = str_replace('<br />',"\n",$sourcecode);
  137.         $sourcecode = str_replace(' ',' ',$sourcecode);
  138.         $sourcecode = str_replace('<','<',$sourcecode);
  139.         $sourcecode = str_replace('>','>',$sourcecode);
  140.         $sourcecode = str_replace('&','&',$sourcecode);
  141.         return $sourcecode;
  142.     }
  143.  
  144.     /**
  145.      * @param string full path to the source file
  146.      * @param string fully highlighted source code
  147.      */
  148.     function writeSource($path, $value)
  149.     {
  150.         $templ = &$this->newSmarty();
  151.         $pathinfo = $this->proceduralpages->getPathInfo($path, $this);
  152.         $templ->assign('source',$value);
  153.         $templ->assign('package',$pathinfo['package']);
  154.         $templ->assign('subpackage',$pathinfo['subpackage']);
  155.         $templ->assign('name',$pathinfo['name']);
  156.         $templ->assign('source_loc',$pathinfo['source_loc']);
  157.         $templ->assign('docs',$pathinfo['docs']);
  158.         $templ->assign("subdir",'../');
  159.         $templ->register_outputfilter('CHMdefault_outputfilter');
  160.         $this->setTargetDir($this->getFileSourcePath($this->base_dir));
  161.         $this->addSourceTOC($pathinfo['name'],$this->getFileSourceName($path),$pathinfo['package'],$pathinfo['subpackage'], true);
  162.         phpDocumentor_out("\n");
  163.         $this->setSourcePaths($path);
  164.         $this->writefile($this->getFileSourceName($path).'.html',$templ->fetch('filesource.tpl'));
  165.     }
  166.     
  167.     function writeExample($title, $path, $source)
  168.     {
  169.         $templ = &$this->newSmarty();
  170.         $templ->assign('source',$source);
  171.         if (empty($title))
  172.         {
  173.             $title = 'example';
  174.             addWarning(PDERROR_EMPTY_EXAMPLE_TITLE, $path, $title);
  175.         }
  176.         $templ->assign('title',$title);
  177.         $templ->assign('file',$path);
  178.         $templ->assign("subdir",'../');
  179.         $templ->register_outputfilter('CHMdefault_outputfilter');
  180.         $pathinfo = $this->proceduralpages->getPathInfo($path, $this);
  181.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . '__examplesource');
  182.         $this->addSourceTOC($title,'exsource_'.$path,$pathinfo['package'],$pathinfo['subpackage'], false);
  183.         phpDocumentor_out("\n");
  184.         $this->writefile('exsource_'.$path.'.html',$templ->fetch('examplesource.tpl'));
  185.     }
  186.     
  187.     function getExampleLink($path, $title)
  188.     {
  189.         return $this->returnLink('{$subdir}__examplesource' . PATH_DELIMITER . 'exsource_'.$path.'.html',$title);
  190.     }
  191.     
  192.     function getSourceLink($path)
  193.     {
  194.         return $this->returnLink($this->getFileSourcePath('{$subdir}') .
  195.         PATH_DELIMITER . $this->getFileSourceName($path).'.html','Source Code for this file');
  196.     }
  197.     
  198.     /**
  199.      * Used to convert the <<code>> tag in a docblock
  200.      * @param string
  201.      * @param boolean
  202.      * @return string
  203.      */
  204.     function ProgramExample($example, $tutorial = false, $inlinesourceparse = null/*false*/,
  205.                             $class = null/*false*/, $linenum = null/*false*/, $filesourcepath = null/*false*/)
  206.     {
  207.         return $this->PreserveWhiteSpace(parent::ProgramExample($example, $tutorial, $inlinesourceparse, $class, $linenum, $filesourcepath));
  208.     }
  209.     
  210.     /**
  211.      * Retrieve a Converter-specific anchor to a segment of a source code file
  212.      * parsed via a {@tutorial tags.filesource.pkg} tag.
  213.      * @param string full path to source file
  214.      * @param string name of anchor
  215.      * @param string link text, if this is a link
  216.      * @param boolean returns either a link or a destination based on this
  217.      *                parameter
  218.      * @return string link to an anchor, or the anchor
  219.      */
  220.     function getSourceAnchor($sourcefile,$anchor,$text = '',$link = false)
  221.     {
  222.         if ($link)
  223.         return $this->returnLink($this->getFileSourcePath('{$subdir}') .
  224.         PATH_DELIMITER . $this->getFileSourceName($sourcefile).'.html#a'.$anchor, $text);
  225.         else
  226.         return '<a name="a'.$anchor.'"></a>';
  227.     }
  228.     
  229.     function getCurrentPageLink()
  230.     {
  231.         return $this->curname . '.html';
  232.     }
  233.  
  234.     /**
  235.      * Uses htmlspecialchars() on the input
  236.      */
  237.     function postProcess($text)
  238.     {
  239.         return htmlspecialchars($text);
  240.     }
  241.     
  242.     /**
  243.      * Use the template tutorial_toc.tpl to generate a table of contents for HTML
  244.      * @return string table of contents formatted for use in the current output format
  245.      * @param array format: array(array('tagname' => section, 'link' => returnsee link, 'id' => anchor name, 'title' => from title tag),...)
  246.      */
  247.     function formatTutorialTOC($toc)
  248.     {
  249.         $template = &$this->newSmarty();
  250.         $template->assign('toc',$toc);
  251.         return $template->fetch('tutorial_toc.tpl');
  252.     }
  253.     
  254.     function &SmartyInit(&$templ)
  255.     {
  256.         if (!isset($this->package_index))
  257.         foreach($this->all_packages as $key => $val)
  258.         {
  259.             if (isset($this->pkg_elements[$key]))
  260.             {
  261.                 if (!isset($start)) $start = $key;
  262.                 $this->package_index[] = array('link' => "li_$key.html", 'title' => $key);
  263.             }
  264.         }
  265.         $templ->assign("packageindex",$this->package_index);
  266.         $templ->assign("subdir",'');
  267.         return $templ;
  268.     }
  269.     
  270.     
  271.     /**
  272.      * Writes out the template file of {@link $class_data} and unsets the template to save memory
  273.      * @see registerCurrentClass()
  274.      * @see parent::endClass()
  275.      */
  276.     function endClass()
  277.     {
  278.         $a = '../';
  279.         if (!empty($this->subpackage)) $a .= '../';
  280.         if ($this->juststarted)
  281.         {
  282.             $this->juststarted = false;
  283.             phpDocumentor_out("\n");
  284.             flush();
  285.         }
  286.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->class_dir);
  287.         $this->class_data->assign("subdir",$a);
  288.         $this->class_data->register_outputfilter('CHMdefault_outputfilter');
  289.         $this->addTOC($this->class,$this->class,$this->package,$this->subpackage, true);
  290.         $this->writefile($this->class . '.html',$this->class_data->fetch('class.tpl'));
  291.         unset($this->class_data);
  292.     }
  293.     
  294.     /**
  295.      * Writes out the template file of {@link $page_data} and unsets the template to save memory
  296.      * @see registerCurrent()
  297.      * @see parent::endPage()
  298.      */
  299.     function endPage()
  300.     {
  301.         $this->package = $this->curpage->package;
  302.         $this->subpackage = $this->curpage->subpackage;
  303.         $a = '../';
  304.         if (!empty($this->subpackage)) $a .= '../';
  305.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  306.         $this->page_data->assign("package",$this->package);
  307.         $this->page_data->assign("subdir",$a);
  308.         $this->page_data->register_outputfilter('CHMdefault_outputfilter');
  309.         $this->addTOC($this->curpage->file,$this->page,$this->package,$this->subpackage);
  310.         $this->writefile($this->page . '.html',$this->page_data->fetch('page.tpl'));
  311.         unset($this->page_data);
  312.     }
  313.     
  314.     /**
  315.      * @param string
  316.      * @param string
  317.      * @return string <a href="'.$link.'">'.$text.'</a>
  318.      */
  319.     function returnLink($link,$text)
  320.     {
  321.         return '<a href="'.$link.'">'.$text.'</a>';
  322.     }
  323.     
  324.     /**
  325.      * CHMdefaultConverter chooses to format both package indexes and the complete index here
  326.      *
  327.      * This function formats output for the elementindex.html and pkgelementindex.html template files.  It then
  328.      * writes them to the target directory
  329.      * @see generateElementIndex(), generatePkgElementIndex()
  330.      */
  331.     function formatPkgIndex()
  332.     {
  333.         list($package_indexes,$packages,$mletters) = $this->generatePkgElementIndexes();
  334.         for($i=0;$i<count($package_indexes);$i++)
  335.         {
  336.             $template = &$this->newSmarty();
  337.             $this->package = $package_indexes[$i]['package'];
  338.             $this->subpackage = '';
  339.             $template->assign("index",$package_indexes[$i]['pindex']);
  340.             $template->assign("package",$package_indexes[$i]['package']);
  341.             $template->assign("letters",$mletters[$package_indexes[$i]['package']]);
  342.             $template->assign("title","Package ".$package_indexes[$i]['package']." Element Index");
  343.             $template->assign("subdir",'../');
  344.             $template->register_outputfilter('CHMdefault_outputfilter');
  345.             $this->setTargetDir($this->base_dir . PATH_DELIMITER . $package_indexes[$i]['package']);
  346.             $this->addTOC($package_indexes[$i]['package']." Alphabetical Index",'elementindex_'.$package_indexes[$i]['package'],$package_indexes[$i]['package'],'');
  347.             $this->writefile('elementindex_'.$package_indexes[$i]['package'].'.html',$template->fetch('pkgelementindex.tpl'));
  348.         }
  349.         phpDocumentor_out("\n");
  350.         flush();
  351.         }
  352.     
  353.     /**
  354.      * CHMdefaultConverter uses this function to format template index.html and packages.html
  355.      *
  356.      * This function generates the package list from {@link $all_packages}, eliminating any
  357.      * packages that don't have any entries in their package index (no files at all, due to @ignore
  358.      * or other factors).  Then it uses the default package name as the first package index to display.
  359.      * It sets the right pane to be either a blank file with instructions on making package-level docs,
  360.      * or the package-level docs for the default package.
  361.      * @global string Used to set the starting package to display
  362.      */
  363.     function formatIndex()
  364.     {
  365.         global $phpDocumentor_DefaultPackageName;
  366.         list($elindex,$mletters) = $this->generateElementIndex();
  367.         $template = &$this->newSmarty();
  368.         $template->assign("index",$elindex);
  369.         $template->assign("letters",$mletters);
  370.         $template->assign("title","Element Index");
  371.         $template->assign("date",date("r",time()));
  372.         $template->register_outputfilter('CHMdefault_outputfilter');
  373.         phpDocumentor_out("\n");
  374.         flush();
  375.         $this->setTargetDir($this->base_dir);
  376.         $this->addTOC("Alphabetical Index Of All Elements",'elementindex',"Index",'');
  377.         $this->writefile('elementindex.html',$template->fetch('elementindex.tpl'));
  378.         usort($this->package_index,"CHMdefault_pindexcmp");
  379.         $index = &$this->newSmarty();
  380.         foreach($this->all_packages as $key => $val)
  381.         {
  382.             if (isset($this->pkg_elements[$key]))
  383.             {
  384.                 if (!isset($start)) $start = $key;
  385.                 if (!isset($this->package_pages[$key])) $this->writeNewPPage($key);
  386.             }
  387.         }
  388.         $this->setTargetDir($this->base_dir);
  389.         // Created index.html
  390.         if (isset($this->pkg_elements[$phpDocumentor_DefaultPackageName])) $start = $phpDocumentor_DefaultPackageName;
  391.         $this->package = $start;
  392.         $this->subpackage = '';
  393.         $setalready = false;
  394.         if (isset($this->tutorials[$start]['']['pkg']))
  395.         {
  396.             foreach($this->tutorials[$start]['']['pkg'] as $tute)
  397.             {
  398.                 if ($tute->name == $start . '.pkg')
  399.                 {
  400.                     $setalready = true;
  401.                        $this->addTOC("Start page",$start.'/tutorial_'.$tute->name,"Index",'');
  402.                 }
  403.             }
  404.         }
  405.         if (!$setalready)
  406.         {
  407.             if (isset($this->package_pages[$start]))
  408.             {
  409.                    $this->addTOC("Start page",'package_'.$start,"Index",'');
  410.             }
  411.             else
  412.             {
  413.                 $index->assign("blank","blank");
  414.                 $blank = &$this->newSmarty();
  415.                 $blank->assign('package',$phpDocumentor_DefaultPackageName);
  416.                 $this->addTOC("Start page",'blank',"Index",'');
  417.                 $this->writefile("blank.html",$blank->fetch('blank.tpl'));
  418.                 Converter::writefile('index.html',$blank->fetch('tutorial.tpl'));
  419.             }
  420.         }
  421.         phpDocumentor_out("\n");
  422.         flush();
  423.  
  424.         unset($index);
  425.     }
  426.     
  427.     function writeNewPPage($key)
  428.     {
  429.         return;
  430.         $template = &$this->newSmarty();
  431.         $this->package = $key;
  432.         $this->subpackage = '';
  433.         $template->assign("date",date("r",time()));
  434.         $template->assign("title",$this->title);
  435.         $template->assign("package",$key);
  436.         $template->register_outputfilter('CHMdefault_outputfilter');
  437.         phpDocumentor_out("\n");
  438.         flush();
  439.         $this->setTargetDir($this->base_dir);
  440.     
  441.         $this->addTOC("$key Index","li_$key",$key,'');
  442.         $this->writefile("li_$key.html",$template->fetch('index.tpl'));
  443.         unset($template);
  444.     }
  445.     
  446.     /**
  447.      * Generate indexes for li_package.html and classtree output files
  448.      *
  449.      * This function generates the li_package.html files from the template file left.html.  It does this by
  450.      * iterating through each of the $page_elements, $class_elements and  $function_elements arrays to retrieve
  451.      * the pre-sorted {@link abstractLink} descendants needed for index generation.  Conversion of these links to
  452.      * text is done by {@link returnSee()}.  The {@link $local} parameter is set to false to ensure that paths are correct.
  453.      * 
  454.      * Then it uses {@link generateFormattedClassTrees()} to create class trees from the template file classtrees.html.  Output
  455.      * filename is classtrees_packagename.html.  This function also unsets {@link $elements} and {@link $pkg_elements} to free
  456.      * up the considerable memory these two class vars use
  457.      * @see $page_elements, $class_elements, $function_elements
  458.      */
  459.     function formatLeftIndex()
  460.     {
  461.         phpDocumentor_out("\n");
  462.         flush();
  463.         $this->setTargetDir($this->base_dir);
  464.         if (0)//!isset($this->left))
  465.         {
  466.             debug("Nothing parsed, check the command-line");
  467.             die();
  468.         }
  469.         foreach($this->all_packages as $package => $rest)
  470.         {
  471.             if (!isset($this->pkg_elements[$package])) continue;
  472.             
  473.             // Create class tree page
  474.             $template = &$this->newSmarty();
  475.             $template->assign("classtrees",$this->generateFormattedClassTrees($package));
  476.             $template->assign("package",$package);
  477.             $template->assign("date",date("r",time()));
  478.             $template->register_outputfilter('CHMdefault_outputfilter');
  479.             $this->addTOC("$package Class Trees","classtrees_$package",$package,'');
  480.             $this->writefile("classtrees_$package.html",$template->fetch('classtrees.tpl'));
  481.             phpDocumentor_out("\n");
  482.             flush();
  483.         }
  484.         // free up considerable memory
  485.         unset($this->elements);
  486.         unset($this->pkg_elements);
  487.     }
  488.     
  489.     /**
  490.      * This function takes an {@link abstractLink} descendant and returns an html link
  491.      *
  492.      * @param abstractLink a descendant of abstractlink should be passed, and never text
  493.      * @param string text to display in the link
  494.      * @param boolean this parameter is not used, and is deprecated
  495.      * @param boolean determines whether the returned text is enclosed in an <a> tag
  496.      */
  497.     function returnSee(&$element, $eltext = false, $with_a = true)
  498.     {
  499.         if (!$element) return false;
  500.         if (!$with_a) return $this->getId($element, false);
  501.         if (!$eltext)
  502.         {
  503.             $eltext = '';
  504.             switch($element->type)
  505.             {
  506.                 case 'tutorial' :
  507.                 $eltext = strip_tags($element->title);
  508.                 break;
  509.                 case 'method' :
  510.                 case 'var' :
  511.                 $eltext .= $element->class.'::';
  512.                 case 'page' :
  513.                 case 'define' :
  514.                 case 'class' :
  515.                 case 'function' :
  516.                 case 'global' :
  517.                 default :
  518.                 $eltext .= $element->name;
  519.                 if ($element->type == 'function' || $element->type == 'method') $eltext .= '()';
  520.                 break;
  521.             }
  522.         }
  523.         return '<a href="'.$this->getId($element).'">'.$eltext.'</a>';
  524.     }
  525.     
  526.     function getId($element, $fullpath = true)
  527.     {
  528.         if (get_class($element) == 'parserdata')
  529.         {
  530.             $element = $this->addLink($element->parent);
  531.             $elp = $element->parent;
  532.         } elseif (is_a($element, 'parserbase'))
  533.         {
  534.             $elp = $element;
  535.             $element = $this->addLink($element);
  536.         }
  537.         $c = '';
  538.         if (!empty($element->subpackage))
  539.         {
  540.             $c = '/'.$element->subpackage;
  541.         }
  542.         $b = '{$subdir}';
  543.         switch ($element->type)
  544.         {
  545.             case 'page' :
  546.             if ($fullpath)
  547.             return $b.$element->package.$c.'/'.$element->fileAlias.'.html';
  548.             return 'top';
  549.             break;
  550.             case 'define' :
  551.             case 'global' :
  552.             case 'function' :
  553.             if ($fullpath)
  554.             return $b.$element->package.$c.'/'.$element->fileAlias.'.html#'.$element->type.$element->name;
  555.             return $element->type.$element->name;
  556.             break;
  557.             case 'class' :
  558.             if ($fullpath)
  559.             return $b.$element->package.$c.'/'.$element->name.'.html';
  560.             return 'top';
  561.             break;
  562.             case 'method' :
  563.             case 'var' :
  564.             if ($fullpath)
  565.             return $b.$element->package.$c.'/'.$element->class.'.html#'.$element->type.$element->name;
  566.             return $element->type.$element->name;
  567.             break;
  568.             case 'tutorial' :
  569.             $d = '';
  570.             if ($element->section)
  571.             {
  572.                 $d = '#'.$element->section;
  573.             }
  574.             return $b.$element->package.$c.'/tutorial_'.$element->name.'.html'.$d;
  575.         }
  576.     }
  577.     
  578.     function ConvertTodoList()
  579.     {
  580.         $todolist = array();
  581.         foreach($this->todoList as $package => $alltodos)
  582.         {
  583.             foreach($alltodos as $todos)
  584.             {
  585.                 $converted = array();
  586.                 $converted['link'] = $this->returnSee($todos[0]);
  587.                 if (!is_array($todos[1]))
  588.                 {
  589.                     $converted['todos'][] = $todos[1]->Convert($this);
  590.                 } else
  591.                 {
  592.                     foreach($todos[1] as $todo)
  593.                     {
  594.                         $converted['todos'][] = $todo->Convert($this);
  595.                     }
  596.                 }
  597.                 $todolist[$package][] = $converted;
  598.             }
  599.         }
  600.         $templ = &$this->newSmarty();
  601.         $templ->assign('todos',$todolist);
  602.         $templ->register_outputfilter('CHMdefault_outputfilter');
  603.         $this->setTargetDir($this->base_dir);
  604.         $this->addTOC('Todo List','todolist','Index','',false,true);
  605.         $this->addKLink('Todo List', 'todolist', '', 'Development');
  606.         $this->writefile('todolist.html',$templ->fetch('todolist.tpl'));
  607.     }
  608.     
  609.     /**
  610.      * Convert README/INSTALL/CHANGELOG file contents to output format
  611.      * @param README|INSTALL|CHANGELOG
  612.      * @param string contents of the file
  613.      */
  614.     function Convert_RIC($name, $contents)
  615.     {
  616.         $template = &$this->newSmarty();
  617.         $template->assign('contents',$contents);
  618.         $template->assign('name',$name);
  619.         $this->setTargetDir($this->base_dir);
  620.         $this->addTOC($name,'ric_'.$name,'Index','',false,true);
  621.         $this->addKLink($name, 'ric_'.$name, '', 'Development');
  622.         $this->writefile('ric_'.$name . '.html',$template->fetch('ric.tpl'));
  623.         $this->ric_set[$name] = true;
  624.     }
  625.     
  626.     /**
  627.      * Create errors.html template file output
  628.      *
  629.      * This method takes all parsing errors and warnings and spits them out ordered by file and line number.
  630.      * @global ErrorTracker We'll be using it's output facility
  631.      */
  632.     function ConvertErrorLog()
  633.     {
  634.         global $phpDocumentor_errors;
  635.         $allfiles = array();
  636.         $files = array();
  637.         $warnings = $phpDocumentor_errors->returnWarnings();
  638.         $errors = $phpDocumentor_errors->returnErrors();
  639.         $template = &$this->newSmarty();
  640.         foreach($warnings as $warning)
  641.         {
  642.             $file = '##none';
  643.             $linenum = 'Warning';
  644.             if ($warning->file)
  645.             {
  646.                 $file = $warning->file;
  647.                 $allfiles[$file] = 1;
  648.                 $linenum .= ' on line '.$warning->linenum;
  649.             }
  650.             $files[$file]['warnings'][] = array('name' => $linenum, 'listing' => $warning->data);
  651.         }
  652.         foreach($errors as $error)
  653.         {
  654.             $file = '##none';
  655.             $linenum = 'Error';
  656.             if ($error->file)
  657.             {
  658.                 $file = $error->file;
  659.                 $allfiles[$file] = 1;
  660.                 $linenum .= ' on line '.$error->linenum;
  661.             }
  662.             $files[$file]['errors'][] = array('name' => $linenum, 'listing' => $error->data);
  663.         }
  664.         $i=1;
  665.         $af = array();
  666.         foreach($allfiles as $file => $num)
  667.         {
  668.             $af[$i++] = $file;
  669.         }
  670.         $allfiles = $af;
  671.         usort($allfiles,'strnatcasecmp');
  672.         $allfiles[0] = "Post-parsing";
  673.         foreach($allfiles as $i => $a)
  674.         {
  675.             $allfiles[$i] = array('file' => $a);
  676.         }
  677.         $out = array();
  678.         foreach($files as $file => $data)
  679.         {
  680.             if ($file == '##none') $file = 'Post-parsing';
  681.             $out[$file] = $data;
  682.         }
  683.         $template->assign("files",$allfiles);
  684.         $template->assign("all",$out);
  685.         $template->assign("title","phpDocumentor Parser Errors and Warnings");
  686.         $this->setTargetDir($this->base_dir);
  687.         $this->writefile("errors.html",$template->fetch('errors.tpl'));
  688.         unset($template);
  689.         phpDocumentor_out("\n\nTo view errors and warnings, look at ".$this->base_dir. PATH_DELIMITER . "errors.html\n");
  690.         flush();
  691.     }
  692.     
  693.     function getCData($value)
  694.     {
  695.         return '<pre>'.htmlentities($value).'</pre>';
  696.     }
  697.     
  698.     function getTutorialId($package,$subpackage,$tutorial,$id)
  699.     {
  700.         return $id;
  701.     }
  702.  
  703.     /**
  704.      * Converts package page and sets its package as used in {@link $package_pages}
  705.      * @param parserPackagePage
  706.      */
  707.     function convertPackagepage(&$element)
  708.     {
  709.         phpDocumentor_out("\n");
  710.         flush();
  711.         $this->package = $element->package;
  712.         $this->subpackage = '';
  713.         $contents = $element->Convert($this);
  714.         $this->package_pages[$element->package] = str_replace('{$subdir}','../',$contents);
  715.         phpDocumentor_out("\n");
  716.         flush();
  717.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $element->package);
  718.         $this->addTOC($element->package." Tutorial",'package_'.$element->package,$element->package,'');
  719.         $this->writeFile('package_'.$element->package.'.html',str_replace('{$subdir}','../',$contents));
  720.         $this->setTargetDir($this->base_dir);
  721.         Converter::writefile('index.html',str_replace('{$subdir}','',$contents));
  722.         $this->addKLink($element->package." Tutorial", 'package_'.$element->package, '', 'Tutorials');
  723.     }
  724.     
  725.     /**
  726.      * @param parserTutorial
  727.      */
  728.     function convertTutorial(&$element)
  729.     {
  730.         phpDocumentor_out("\n");
  731.         flush();
  732.         $template = &parent::convertTutorial($element);
  733.         $a = '../';
  734.         if ($element->subpackage) $a .= '../';
  735.         $template->assign('subdir',$a);
  736.         $template->register_outputfilter('CHMdefault_outputfilter');
  737.         $contents = $template->fetch('tutorial.tpl');
  738.         if ($element->package == $GLOBALS['phpDocumentor_DefaultPackageName'] && empty($element->subpackage) && ($element->name == $element->package . '.pkg'))
  739.         {
  740.             $template->assign('subdir','');
  741.             $this->setTargetDir($this->base_dir);
  742.             Converter::writefile('index.html',$template->fetch('tutorial.tpl'));
  743.         }
  744.         $a = '';
  745.         if ($element->subpackage) $a = PATH_DELIMITER . $element->subpackage;
  746.         phpDocumentor_out("\n");
  747.         flush();
  748.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $element->package . $a);
  749.         $this->addTOC($a = strip_tags($element->getTitle($this)), 'tutorial_'.$element->name,
  750.             $element->package, $element->subpackage, false, true);
  751.         $this->writeFile('tutorial_'.$element->name.'.html',$contents);
  752.         $this->addKLink($element->getTitle($this), $element->package . $a . PATH_DELIMITER . 'tutorial_'.$element->name,
  753.             '', 'Tutorials');
  754.     }
  755.     
  756.     /**
  757.      * Converts class for template output
  758.      * @see prepareDocBlock(), generateChildClassList(), generateFormattedClassTree(), getFormattedConflicts()
  759.      * @see getFormattedInheritedMethods(), getFormattedInheritedVars()
  760.      * @param parserClass
  761.      */
  762.     function convertClass(&$element)
  763.     {
  764.         parent::convertClass($element);
  765.         $this->class_dir = $element->docblock->package;
  766.         if (!empty($element->docblock->subpackage)) $this->class_dir .= PATH_DELIMITER . $element->docblock->subpackage;
  767.         $a = '../';
  768.         if ($element->docblock->subpackage != '') $a = "../$a";
  769.         
  770.         $this->class_data->assign('subdir',$a);
  771.         $this->class_data->assign("title","Docs For Class " . $element->getName());
  772.         $this->class_data->assign("page",$element->getName() . '.html');
  773.         $this->addKLink($element->name, $this->class_dir . PATH_DELIMITER . $this->class, '', 'Classes');
  774.     }
  775.     
  776.  
  777.     /**
  778.      * Converts class variables for template output
  779.      * @see prepareDocBlock(), getFormattedConflicts()
  780.      * @param parserDefine
  781.      */
  782.     function convertVar(&$element)
  783.     {
  784.         parent::convertVar($element, array('var_dest' => $this->getId($element,false)));
  785.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->class_dir);
  786.         $this->addKLink($element->name, $this->class_dir . PATH_DELIMITER .$this->class, $this->getId($element,false), $element->class.' Properties');
  787.     }
  788.  
  789.     /**
  790.      * Converts class methods for template output
  791.      * @see prepareDocBlock(), getFormattedConflicts()
  792.      * @param parserDefine
  793.      */
  794.     function convertMethod(&$element)
  795.     {
  796.         parent::convertMethod($element, array('method_dest' => $this->getId($element,false)));
  797.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->class_dir);
  798.         $this->addKLink($element->name, $this->class_dir . PATH_DELIMITER .$this->class, $this->getId($element,false), $element->class.' Methods');
  799.     }
  800.     
  801.     /**
  802.      * Converts function for template output
  803.      * @see prepareDocBlock(), parserFunction::getFunctionCall(), getFormattedConflicts()
  804.      * @param parserFunction
  805.      */
  806.     function convertFunction(&$element)
  807.     {
  808.         $funcloc = $this->getId($this->addLink($element));
  809.         parent::convertFunction($element,array('function_dest' => $this->getId($element,false)));
  810.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  811.         $this->addKLink($element->name, $this->page_dir . PATH_DELIMITER . $this->page, $this->getId($element,false), 'Functions');
  812.     }
  813.     
  814.     /**
  815.      * Converts include elements for template output
  816.      * @see prepareDocBlock()
  817.      * @param parserInclude
  818.      */
  819.     function convertInclude(&$element)
  820.     {
  821.         parent::convertInclude($element, array('include_file'    => '_'.strtr($element->getValue(),array('"' => '', "'" => '','.' => '_'))));
  822.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  823.         $this->addKLink(str_replace('"', '', $element->getValue()), $this->page_dir . PATH_DELIMITER . $this->page, '', ucfirst($element->name));
  824.     }
  825.     
  826.     /**
  827.      * Converts defines for template output
  828.      * @see prepareDocBlock(), getFormattedConflicts()
  829.      * @param parserDefine
  830.      */
  831.     function convertDefine(&$element)
  832.     {
  833.         parent::convertDefine($element, array('define_link' => $this->getId($element,false)));
  834.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  835.         $this->addKLink($element->name, $this->page_dir . PATH_DELIMITER . $this->page, $this->getId($element,false), 'Constants');
  836.     }
  837.     
  838.     /**
  839.      * Converts global variables for template output
  840.      * @param parserGlobal
  841.      */
  842.     function convertGlobal(&$element)
  843.     {
  844.         parent::convertGlobal($element, array('global_link' => $this->getId($element,false)));
  845.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  846.         $this->addKLink($element->name, $this->page_dir . PATH_DELIMITER . $this->page, $this->getId($element,false), 'Global Variables');
  847.     }
  848.     
  849.     /**
  850.      * converts procedural pages for template output
  851.      * @see prepareDocBlock(), getClassesOnPage()
  852.      * @param parserData
  853.      */
  854.     function convertPage(&$element)
  855.     {
  856.         parent::convertPage($element);
  857.         $this->juststarted = true;
  858.         $this->page_dir = $element->parent->package;
  859.         if (!empty($element->parent->subpackage)) $this->page_dir .= PATH_DELIMITER . $element->parent->subpackage;
  860.         // registering stuff on the template
  861.         $this->page_data->assign("page",$this->getPageName($element) . '.html');
  862.         $this->page_data->assign("title","Docs for page ".$element->parent->getFile());
  863.         $this->setTargetDir($this->base_dir . PATH_DELIMITER . $this->page_dir);
  864.         $this->addKLink($element->parent->file, $this->page_dir . PATH_DELIMITER . $this->page, '', 'Files');
  865.     }
  866.     
  867.     function getPageName(&$element)
  868.     {
  869.         if (get_class($element) == 'parserpage') return '_'.$element->getName();
  870.         return '_'.$element->parent->getName();
  871.     }
  872.  
  873.     /**
  874.      * returns an array containing the class inheritance tree from the root object to the class
  875.      *
  876.      * @param parserClass    class variable
  877.      * @return array Format: array(root,child,child,child,...,$class)
  878.      * @uses parserClass::getParentClassTree()
  879.      */
  880.     
  881.     function generateFormattedClassTree($class)
  882.     {
  883.         $tree = $class->getParentClassTree($this);
  884.         $out = '';
  885.         if (count($tree) - 1)
  886.         {
  887.             $result = array($class->getName());
  888.             $parent = $tree[$class->getName()];
  889.             $distance[] = '';
  890.             while ($parent)
  891.             {
  892.                 $subpackage = $parent->docblock->subpackage;
  893.                 $package = $parent->docblock->package;
  894.                 $x = $parent;
  895.                 if (is_object($parent))
  896.                 $x = $parent->getLink($this);
  897.                 if (!$x) $x = $parent->getName();
  898.                 $result[] = 
  899.                     $x;
  900.                 $distance[] =
  901.                     "\n%s|\n" .
  902.                     "%s--";
  903.                 if (is_object($parent))
  904.                 $parent = $tree[$parent->getName()];
  905.                 elseif (isset($tree[$parent]))
  906.                 $parent = $tree[$parent];
  907.             }
  908.             $nbsp = '   ';
  909.             for($i=count($result) - 1;$i>=0;$i--)
  910.             {
  911.                 $my_nbsp = '';
  912.                 for($j=0;$j<count($result) - $i;$j++) $my_nbsp .= $nbsp;
  913.                 $distance[$i] = sprintf($distance[$i],$my_nbsp,$my_nbsp);
  914.             }
  915.             return array('classes'=>array_reverse($result),'distance'=>array_reverse($distance));
  916.         } else
  917.         {
  918.             return array('classes'=>$class->getName(),'distance'=>array(''));
  919.         }
  920.     }
  921.     
  922.     /** @access private */
  923.     function sortVar($a, $b)
  924.     {
  925.         return strnatcasecmp($a->getName(),$b->getName());
  926.     }
  927.     
  928.     /** @access private */
  929.     function sortMethod($a, $b)
  930.     {
  931.         if ($a->isConstructor) return -1;
  932.         if ($b->isConstructor) return 1;
  933.         return strnatcasecmp($a->getName(),$b->getName());
  934.     }
  935.  
  936.     /**
  937.      * returns a template-enabled array of class trees
  938.      * 
  939.      * @param    string    $package    package to generate a class tree for
  940.      * @see $roots, HTMLConverter::getRootTree()
  941.      */
  942.     function generateFormattedClassTrees($package)
  943.     {
  944.         if (!isset($this->roots[$package])) return array();
  945.         $roots = $trees = array();
  946.         $roots = $this->roots[$package];
  947.         for($i=0;$i<count($roots);$i++)
  948.         {
  949.             $trees[] = array('class' => $roots[$i],'class_tree' => "<ul>\n".$this->getRootTree($this->getSortedClassTreeFromClass($roots[$i],$package,''),$package)."</ul>\n");
  950.         }
  951.         return $trees;
  952.     }
  953.     
  954.     /**
  955.      * return formatted class tree for the Class Trees page
  956.      *
  957.      * @param array $tree output from {@link getSortedClassTreeFromClass()}
  958.      * @see Classes::$definitechild, generateFormattedClassTrees()
  959.      * @return string
  960.      */
  961.     function getRootTree($tree,$package)
  962.     {
  963.         if (!$tree) return '';
  964.         $my_tree = '';
  965.         $cur = '#root';
  966.         $lastcur = array(false);
  967.         $kids = array();
  968.         $dopar = false;
  969.         if ($tree[$cur]['parent'])
  970.         {
  971.             $dopar = true;
  972.             if (!is_object($tree[$cur]['parent']))
  973.             {
  974. //                debug("parent ".$tree[$cur]['parent']." not found");
  975.                 $my_tree .= '<li>' . $tree[$cur]['parent'] .'<ul>';
  976.             }
  977.             else
  978.             {
  979. //                        debug("parent ".$this->returnSee($tree[$cur]['parent'])." in other package");
  980.                 $my_tree .= '<li>' . $this->returnSee($tree[$cur]['parent']);
  981.                 if ($tree[$cur]['parent']->package != $package) $my_tree .= ' <b>(Different package)</b><ul>';
  982.             }
  983.         }
  984.         do
  985.         {
  986. //            fancy_debug($cur,$lastcur,$kids);
  987.             if (count($tree[$cur]['children']))
  988.             {
  989. //                debug("$cur has children");
  990.                 if (!isset($kids[$cur]))
  991.                 {
  992. //                    debug("set $cur kids");
  993.                     $kids[$cur] = 1;
  994.                     $my_tree .= '<li>'.$this->returnSee($tree[$cur]['link']);
  995.                     $my_tree .= '<ul>'."\n";
  996.                 }
  997.                 array_push($lastcur,$cur);
  998.                 list(,$cur) = each($tree[$cur]['children']);
  999. //                var_dump('listed',$cur);
  1000.                 if ($cur)
  1001.                 {
  1002.                     $cur = $cur['package'] . '#' . $cur['class'];
  1003. //                    debug("set cur to child $cur");
  1004. //                    $my_tree .= '<li>'.$this->returnSee($tree[$cur]['link']);
  1005.                     continue;
  1006.                 } else
  1007.                 {
  1008. //                    debug("end of children for $cur");
  1009.                     $cur = array_pop($lastcur);
  1010.                     $cur = array_pop($lastcur);
  1011.                     $my_tree .= '</ul></li>'."\n";
  1012.                     if ($dopar && ($cur == '#root' || !$cur)) $my_tree .= '</ul></li>';
  1013.                 }
  1014.             } else 
  1015.             {
  1016. //                debug("$cur has no children");
  1017.                 $my_tree .= '<li>'.$this->returnSee($tree[$cur]['link'])."</li>";
  1018.                 if ($dopar && $cur == '#root') $my_tree .= '</ul></li>';
  1019.                 $cur = array_pop($lastcur);
  1020.             }
  1021.         } while ($cur);
  1022.         return $my_tree;
  1023.     }
  1024.         /**
  1025.          * Generate indexing information for given element
  1026.          * 
  1027.          * @param parserElement descendant of parserElement
  1028.          * @see generateElementIndex()
  1029.          * @return array
  1030.          */
  1031.         function getIndexInformation($elt)
  1032.         {
  1033.             $Result['type'] = $elt->type;
  1034.             $Result['file_name'] = $elt->file;
  1035.             $Result['path'] = $elt->getPath();
  1036.             
  1037.             if (isset($elt->docblock))
  1038.             {
  1039.                 $Result['description'] = $elt->docblock->getSDesc($this);
  1040.  
  1041.                 if ($elt->docblock->hasaccess)
  1042.                     $Result['access'] = $elt->docblock->tags['access'][0]->value;
  1043.                 else
  1044.                     $Result['access'] = 'public';
  1045.                 }
  1046.             else
  1047.                 $Result['description'] = '';
  1048.             
  1049.             $aa = $Result['description'];
  1050.             if (!empty($aa)) $aa = "<br>    $aa";
  1051.  
  1052.             switch($elt->type)
  1053.             {
  1054.                     case 'class':
  1055.                             $Result['name'] = $elt->getName();
  1056.                             $Result['title'] = 'Class';
  1057.                             $Result['link'] = $this->getClassLink($elt->getName(),
  1058.                                                                   $elt->docblock->package,
  1059.                                                                   $elt->getPath(),
  1060.                                                                   $elt->getName());
  1061.                             $Result['listing'] = 'in file '.$elt->file.', class '.$Result['link']."$aa";
  1062.                     break;
  1063.                     case 'define':
  1064.                             $Result['name'] = $elt->getName();
  1065.                             $Result['title'] = 'Constant';
  1066.                             $Result['link'] = $this->getDefineLink($elt->getName(),
  1067.                                                                    $elt->docblock->package,
  1068.                                                                    $elt->getPath(),
  1069.                                                                    $elt->getName());
  1070.                             $Result['listing'] = 'in file '.$elt->file.', constant '.$Result['link']."$aa";
  1071.                     break;
  1072.                     case 'global':
  1073.                             $Result['name'] = $elt->getName();
  1074.                             $Result['title'] = 'Global';
  1075.                             $Result['link'] = $this->getGlobalLink($elt->getName(),
  1076.                                                                    $elt->docblock->package,
  1077.                                                                    $elt->getPath(),
  1078.                                                                    $elt->getName());
  1079.                             $Result['listing'] = 'in file '.$elt->file.', global variable '.$Result['link']."$aa";
  1080.                     break;
  1081.                     case 'function':
  1082.                             $Result['name'] = $elt->getName();
  1083.                             $Result['title'] = 'Function';
  1084.                             $Result['link'] = $this->getFunctionLink($elt->getName(),
  1085.                                                                      $elt->docblock->package,
  1086.                                                                      $elt->getPath(),
  1087.                                                                      $elt->getName().'()');
  1088.                             $Result['listing'] = 'in file '.$elt->file.', function '.$Result['link']."$aa";
  1089.                     break;
  1090.                     case 'method':
  1091.                             $Result['name'] = $elt->getName();
  1092.                             $Result['title'] = 'Method';
  1093.                             $Result['link'] = $this->getMethodLink($elt->getName(),
  1094.                                                                    $elt->class,
  1095.                                                                    $elt->docblock->package,
  1096.                                                                    $elt->getPath(),
  1097.                                                                    $elt->class.'::'.$elt->getName().'()'
  1098.                                                                              );
  1099.                                                         if ($elt->isConstructor) $Result['constructor'] = 1;
  1100.                             $Result['listing'] = 'in file '.$elt->file.', method '.$Result['link']."$aa";
  1101.                     break;
  1102.                     case 'var':
  1103.                             $Result['name'] = $elt->getName();
  1104.                             $Result['title'] = 'Variable';
  1105.                             $Result['link'] = $this->getVarLink($elt->getName(),
  1106.                                                                 $elt->class,
  1107.                                                                 $elt->docblock->package,
  1108.                                                                 $elt->getPath(),
  1109.                                                                 $elt->class.'::'.$elt->getName());
  1110.                             $Result['listing'] = 'in file '.$elt->file.', variable '.$Result['link']."$aa";
  1111.                     break;
  1112.                     case 'page':
  1113.                             $Result['name'] = $elt->getFile();
  1114.                             $Result['title'] = 'Page';
  1115.                             $Result['link'] = $this->getPageLink($elt->getFile(),
  1116.                                                                  $elt->package,
  1117.                                                                  $elt->getPath(),
  1118.                                                                  $elt->getFile());
  1119.                             $Result['listing'] = 'procedural page '.$Result['link'];
  1120.                     break;
  1121.                     case 'include':
  1122.                             $Result['name'] = $elt->getName();
  1123.                             $Result['title'] = 'Include';
  1124.                             $Result['link'] = $elt->getValue();
  1125.                             $Result['listing'] = 'include '.$Result['name'];
  1126.                     break;
  1127.             }
  1128.  
  1129.             return $Result;
  1130.         }
  1131.     /**
  1132.      * Generate alphabetical index of all elements
  1133.      *
  1134.      * @see $elements, walk()
  1135.      */
  1136.     function generateElementIndex()
  1137.     {
  1138.         $elementindex = array();
  1139.         $letters = array();
  1140.         $used = array();
  1141.         foreach($this->elements as $letter => $nutoh)
  1142.         {
  1143.             foreach($this->elements[$letter] as $i => $yuh)
  1144.             {
  1145.                 if ($this->elements[$letter][$i]->type != 'include')
  1146.                 {
  1147.                     if (!isset($used[$letter]))
  1148.                     {
  1149.                         $letters[]['letter'] = $letter;
  1150.                         $elindex['letter'] = $letter;
  1151.                         $used[$letter] = 1;
  1152.                     }
  1153.  
  1154.                     $elindex['index'][] = $this->getIndexInformation($this->elements[$letter][$i]);
  1155.                 }
  1156.             }
  1157.             if (isset($elindex['index']))
  1158.             {
  1159.                 $elementindex[] = $elindex;
  1160.             } else
  1161.             {
  1162.                 unset($letters[count($letters) - 1]);
  1163.             }
  1164.             $elindex = array();
  1165.         }
  1166.         return array($elementindex,$letters);
  1167.     }
  1168.     
  1169.     function setTemplateDir($dir)
  1170.     {
  1171.         Converter::setTemplateDir($dir);
  1172.         $this->smarty_dir = $this->templateDir;
  1173.     }
  1174.     
  1175.     function copyMediaRecursively($media,$targetdir,$subdir = '')
  1176.     {
  1177.         foreach($media as $dir => $files)
  1178.         {
  1179.             if ($dir === '/')
  1180.             {
  1181.                 $this->copyMediaRecursively($files,$targetdir);
  1182.             } else
  1183.             {
  1184.                 if (!is_numeric($dir))
  1185.                 {
  1186.                     // create the subdir
  1187.                     phpDocumentor_out("creating $targetdir/$dir\n");
  1188.                     Converter::setTargetDir($targetdir . PATH_DELIMITER . $dir);
  1189.                     if (!empty($subdir)) $subdir .= PATH_DELIMITER;
  1190.                     $this->copyMediaRecursively($files,"$targetdir/$dir",$subdir . $dir);
  1191.                 } else
  1192.                 {
  1193.                     // copy the file
  1194.                     phpDocumentor_out("copying $targetdir/".$files['file']."\n");
  1195.                     $this->copyFile($files['file'],$subdir);
  1196.                 }
  1197.             }
  1198.         }
  1199.     }
  1200.     
  1201.     /**
  1202.      * calls the converter setTargetDir, and then copies any template images and the stylesheet if they haven't been copied
  1203.      * @see Converter::setTargetDir()
  1204.      */
  1205.     function setTargetDir($dir)
  1206.     {
  1207.         Converter::setTargetDir($dir);
  1208.         if ($this->wrote) return;
  1209.         $this->wrote = true;
  1210.         $template_images = array();
  1211.         $stylesheets = array();
  1212.         $tdir = $dir;
  1213.         $dir = $this->templateDir;
  1214.         $this->templateDir = $this->templateDir.'templates/';
  1215.         $info = new Io;
  1216.         $this->copyMediaRecursively($info->getDirTree($this->templateDir.'media',$this->templateDir),$tdir);
  1217.     }
  1218.     
  1219.     /**
  1220.      * Generate alphabetical index of all elements by package and subpackage
  1221.      *
  1222.      * @param string $package name of a package
  1223.      * @see $pkg_elements, walk(), generatePkgElementIndexes()
  1224.      */
  1225.     function generatePkgElementIndex($package)
  1226.     {
  1227. //        var_dump($this->pkg_elements[$package]);
  1228.         $elementindex = array();
  1229.         $letters = array();
  1230.         $letterind = array();
  1231.         $used = array();
  1232.         $subp = '';
  1233.         foreach($this->pkg_elements[$package] as $subpackage => $els)
  1234.         {
  1235.             if (empty($els)) continue;
  1236.             if (!empty($subpackage)) $subp = " (<b>subpackage:</b> $subpackage)"; else $subp = '';
  1237.             foreach($els as $letter => $yuh)
  1238.             {
  1239.                 foreach($els[$letter] as $i => $yuh)
  1240.                 {
  1241.                     if ($els[$letter][$i]->type != 'include')
  1242.                     {
  1243.                         if (!isset($used[$letter]))
  1244.                         {
  1245.                             $letters[]['letter'] = $letter;
  1246.                             $letterind[$letter] = count($letters) - 1;
  1247.                             $used[$letter] = 1;
  1248.                         }
  1249.                         $elindex[$letter]['letter'] = $letter;
  1250.  
  1251.                         $elindex[$letter]['index'][] = $this->getIndexInformation($els[$letter][$i]);
  1252.                     }
  1253.                 }
  1254.             }
  1255.         }
  1256.         ksort($elindex);
  1257.         usort($letters,'CHMdefault_lettersort');
  1258.         if (isset($elindex))
  1259.         {
  1260.             while(list($letter,$tempel) = each($elindex))
  1261.             {
  1262.                 if (!isset($tempel))
  1263.                 {
  1264.                     unset($letters[$letterind[$tempel['letter']]]);
  1265.                 } else
  1266.                 $elementindex[] = $tempel;
  1267.             }
  1268.         } else $letters = array();
  1269.         return array($elementindex,$letters);
  1270.     }
  1271.     
  1272.     /**
  1273.      *
  1274.      * @see generatePkgElementIndex()
  1275.      */
  1276.     function generatePkgElementIndexes()
  1277.     {
  1278.         $packages = array();
  1279.         $package_names = array();
  1280.         $pkg = array();
  1281.         $letters = array();
  1282.         foreach($this->pkg_elements as $package => $trash)
  1283.         {
  1284.             $pkgs['package'] = $package;
  1285.             $pkg['package'] = $package;
  1286.             list($pkg['pindex'],$letters[$package]) = $this->generatePkgElementIndex($package);
  1287.             if (count($pkg['pindex']))
  1288.             {
  1289.                 $packages[] = $pkg;
  1290.                 $package_names[] = $pkgs;
  1291.             }
  1292.             unset($pkgs);
  1293.             unset($pkg);
  1294.         }
  1295.         foreach($packages as $i => $package)
  1296.         {
  1297.             $pnames = array();
  1298.             for($j=0;$j<count($package_names);$j++)
  1299.             {
  1300.                 if ($package_names[$j]['package'] != $package['package']) $pnames[] = $package_names[$j];
  1301.             }
  1302.             $packages[$i]['packageindexes'] = $pnames;
  1303.         }
  1304.         return array($packages,$package_names,$letters);
  1305.     }
  1306.     
  1307.     /**
  1308.      * @param string name of class
  1309.      * @param string package name
  1310.      * @param string full path to look in (used in index generation)
  1311.      * @param boolean deprecated
  1312.      * @param boolean return just the URL, or enclose it in an html a tag
  1313.      * @return mixed false if not found, or an html a link to the class's documentation
  1314.      * @see parent::getClassLink()
  1315.      */
  1316.     function getClassLink($expr,$package, $file = false,$text = false, $with_a = true)
  1317.     {
  1318.         $a = Converter::getClassLink($expr,$package,$file);
  1319.         if (!$a) return false;
  1320.         return $this->returnSee($a, $text, $with_a);
  1321.     }
  1322.  
  1323.     /**
  1324.      * @param string name of function
  1325.      * @param string package name
  1326.      * @param string full path to look in (used in index generation)
  1327.      * @param boolean deprecated
  1328.      * @param boolean return just the URL, or enclose it in an html a tag
  1329.      * @return mixed false if not found, or an html a link to the function's documentation
  1330.      * @see parent::getFunctionLink()
  1331.      */
  1332.     function getFunctionLink($expr,$package, $file = false,$text = false)
  1333.     {
  1334.         $a = Converter::getFunctionLink($expr,$package,$file);
  1335.         if (!$a) return false;
  1336.         return $this->returnSee($a, $text);
  1337.     }
  1338.  
  1339.     /**
  1340.      * @param string name of define
  1341.      * @param string package name
  1342.      * @param string full path to look in (used in index generation)
  1343.      * @param boolean deprecated
  1344.      * @param boolean return just the URL, or enclose it in an html a tag
  1345.      * @return mixed false if not found, or an html a link to the define's documentation
  1346.      * @see parent::getDefineLink()
  1347.      */
  1348.     function getDefineLink($expr,$package, $file = false,$text = false)
  1349.     {
  1350.         $a = Converter::getDefineLink($expr,$package,$file);
  1351.         if (!$a) return false;
  1352.         return $this->returnSee($a, $text);
  1353.     }
  1354.  
  1355.     /**
  1356.      * @param string name of global variable
  1357.      * @param string package name
  1358.      * @param string full path to look in (used in index generation)
  1359.      * @param boolean deprecated
  1360.      * @param boolean return just the URL, or enclose it in an html a tag
  1361.      * @return mixed false if not found, or an html a link to the global variable's documentation
  1362.      * @see parent::getGlobalLink()
  1363.      */
  1364.     function getGlobalLink($expr,$package, $file = false,$text = false)
  1365.     {
  1366.         $a = Converter::getGlobalLink($expr,$package,$file);
  1367.         if (!$a) return false;
  1368.         return $this->returnSee($a, $text);
  1369.     }
  1370.  
  1371.     /**
  1372.      * @param string name of procedural page
  1373.      * @param string package name
  1374.      * @param string full path to look in (used in index generation)
  1375.      * @param boolean deprecated
  1376.      * @param boolean return just the URL, or enclose it in an html a tag
  1377.      * @return mixed false if not found, or an html a link to the procedural page's documentation
  1378.      * @see parent::getPageLink()
  1379.      */
  1380.     function getPageLink($expr,$package, $path = false,$text = false)
  1381.     {
  1382.         $a = Converter::getPageLink($expr,$package,$path);
  1383.         if (!$a) return false;
  1384.         return $this->returnSee($a, $text);
  1385.     }
  1386.  
  1387.     /**
  1388.      * @param string name of method
  1389.      * @param string class containing method
  1390.      * @param string package name
  1391.      * @param string full path to look in (used in index generation)
  1392.      * @param boolean deprecated
  1393.      * @param boolean return just the URL, or enclose it in an html a tag
  1394.      * @return mixed false if not found, or an html a link to the method's documentation
  1395.      * @see parent::getMethodLink()
  1396.      */
  1397.     function getMethodLink($expr,$class,$package, $file = false,$text = false)
  1398.     {
  1399.         $a = Converter::getMethodLink($expr,$class,$package,$file);
  1400.         if (!$a) return false;
  1401.         return $this->returnSee($a, $text);
  1402.     }
  1403.  
  1404.     /**
  1405.      * @param string name of var
  1406.      * @param string class containing var
  1407.      * @param string package name
  1408.      * @param string full path to look in (used in index generation)
  1409.      * @param boolean deprecated
  1410.      * @param boolean return just the URL, or enclose it in an html a tag
  1411.      * @return mixed false if not found, or an html a link to the var's documentation
  1412.      * @see parent::getVarLink()
  1413.      */
  1414.     function getVarLink($expr,$class,$package, $file = false,$text = false)
  1415.     {
  1416.         $a = Converter::getVarLink($expr,$class,$package,$file);
  1417.         if (!$a) return false;
  1418.         return $this->returnSee($a, $text);
  1419.     }
  1420.     
  1421.     /**
  1422.      * does a nat case sort on the specified second level value of the array
  1423.      *
  1424.      * @param    mixed    $a
  1425.      * @param    mixed    $b
  1426.      * @return    int
  1427.      */
  1428.     function rcNatCmp ($a, $b)
  1429.     {
  1430.         $aa = strtoupper($a[$this->rcnatcmpkey]);
  1431.         $bb = strtoupper($b[$this->rcnatcmpkey]);
  1432.         
  1433.         return strnatcasecmp($aa, $bb);
  1434.     }
  1435.     
  1436.     /**
  1437.      * does a nat case sort on the specified second level value of the array.
  1438.      * this one puts constructors first
  1439.      *
  1440.      * @param    mixed    $a
  1441.      * @param    mixed    $b
  1442.      * @return    int
  1443.      */
  1444.     function rcNatCmp1 ($a, $b)
  1445.     {
  1446.         $aa = strtoupper($a[$this->rcnatcmpkey]);
  1447.         $bb = strtoupper($b[$this->rcnatcmpkey]);
  1448.         
  1449.         if (strpos($aa,'CONSTRUCTOR') === 0)
  1450.         {
  1451.             return -1;
  1452.         }
  1453.         if (strpos($bb,'CONSTRUCTOR') === 0)
  1454.         {
  1455.             return 1;
  1456.         }
  1457.         if (strpos($aa,strtoupper($this->class)) === 0)
  1458.         {
  1459.             return -1;
  1460.         }
  1461.         if (strpos($bb,strtoupper($this->class)) === 0)
  1462.         {
  1463.             return -1;
  1464.         }
  1465.         return strnatcasecmp($aa, $bb);
  1466.     }
  1467.     
  1468.     /**
  1469.      * Write a file to disk, and add it to the {@link $hhp_files} list of files
  1470.      * to include in the generated CHM
  1471.      *
  1472.      * {@source}
  1473.      */
  1474.     function writefile($file,$contents)
  1475.     {
  1476.         $this->addHHP($this->targetDir . PATH_DELIMITER . $file);
  1477.         Converter::writefile($file,$contents);
  1478.     }
  1479.     
  1480.     /**
  1481.      * @uses $hhp_files creates the array by adding parameter $file
  1482.      */
  1483.     function addHHP($file)
  1484.     {
  1485.         $file = str_replace('\\',PATH_DELIMITER,$file);
  1486.         $file = str_replace('//',PATH_DELIMITER,$file);
  1487.         $file = str_replace(PATH_DELIMITER,'\\',$file);
  1488.         $this->hhp_files[]['name'] = $file;
  1489.     }
  1490.     
  1491.     function generateTOC()
  1492.     {
  1493.         $comppack = '';
  1494.         $templ = &$this->newSmarty();
  1495.         foreach($this->TOC as $package => $TOC1)
  1496.         {
  1497.             $comp_subs = '';
  1498.             $comp_subs1 = false;
  1499.             foreach($TOC1 as $subpackage => $types)
  1500.             {
  1501.                 $comp_types = '';
  1502.                 foreach($types as $type => $files)
  1503.                 {
  1504.                     $comp = '';
  1505.                     $templ1 = &$this->newSmarty();
  1506.                     $templ1->assign('entry', array());
  1507.                     foreach($files as $file)
  1508.                     {
  1509.                     // use book icon for classes
  1510.                         if ($type == 'Classes') {
  1511.                             $templ1->append('entry', array('paramname' => $file[0],'outputfile' => $file[1],'isclass' => 1));
  1512.                         } else {
  1513.                             $templ1->append('entry', array('paramname' => $file[0],'outputfile' => $file[1]));
  1514.                         }
  1515.                     }
  1516.                     $templ = &$this->newSmarty();
  1517.                     $templ->assign('tocsubentries',$templ1->fetch('tocentry.tpl'));
  1518.                     $templ->assign('entry', array(array('paramname' => $type)));
  1519.                     $comp_types .= $templ->fetch('tocentry.tpl');
  1520.                 }
  1521.                 if (!empty($subpackage))
  1522.                 {
  1523.                     $templ = &$this->newSmarty();
  1524.                     $templ->assign('tocsubentries',$comp_types);
  1525.                     $templ->assign('entry', array(array('paramname' => $subpackage)));
  1526.                     $comp_subs .= $templ->fetch('tocentry.tpl');
  1527.                 } else
  1528.                 {
  1529.                     $comp_subs1 = $comp_types;
  1530.                 }
  1531.             }
  1532.             if ($comp_subs1)
  1533.             $templ->assign('tocsubentries',$comp_subs1);
  1534.             if (!empty($comp_subs))
  1535.             $templ->assign('entry', array(array('paramname' => $package, 'tocsubentries' => $comp_subs)));
  1536.             else
  1537.             $templ->assign('entry', array(array('paramname' => $package)));
  1538.             $comppack .= $templ->fetch('tocentry.tpl');
  1539.         }
  1540.         return $comppack;
  1541.     }
  1542.     
  1543.     function addSourceTOC($name, $file, $package, $subpackage, $source = false)
  1544.     {
  1545.         $file = $this->targetDir . PATH_DELIMITER . $file . '.html';
  1546.         $file = str_replace('\\',PATH_DELIMITER,$file);
  1547.         $file = str_replace('//',PATH_DELIMITER,$file);
  1548.         $file = str_replace(PATH_DELIMITER,'\\',$file);
  1549.         $sub = $source ? 'Source Code' : 'Examples';
  1550.         $this->TOC[$package][$subpackage][$sub][] = array($name, $file);
  1551.     }
  1552.     
  1553.     function addTOC($name,$file,$package,$subpackage,$class = false,$tutorial = false)
  1554.     {
  1555.         $file = $this->targetDir . PATH_DELIMITER . $file . '.html';
  1556.         $file = str_replace('\\',PATH_DELIMITER,$file);
  1557.         $file = str_replace('//',PATH_DELIMITER,$file);
  1558.         $file = str_replace(PATH_DELIMITER,'\\',$file);
  1559.         $sub = $class ? 'Classes' : 'Files';
  1560.         if ($tutorial) $sub = 'Manual';
  1561.         $this->TOC[$package][$subpackage][$sub][] = array($name,$file);
  1562.     }
  1563.     
  1564.     /**
  1565.      * Add an item to the index.hhk file
  1566.      * @param string $name index entry name
  1567.      * @param string $file filename containing index
  1568.      * @param string $bookmark html anchor of location in file, if any
  1569.      * @param string $group group this entry with a string
  1570.      * @uses $KLinks tracks the index
  1571.      * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  1572.      */
  1573.     function addKLink($name, $file, $bookmark='', $group='')
  1574.     {
  1575.         $file = $this->base_dir . PATH_DELIMITER . $file . '.html';
  1576.         $file = str_replace('\\',PATH_DELIMITER,$file);
  1577.         $file = str_replace('//',PATH_DELIMITER,$file);
  1578.         $file = str_replace(PATH_DELIMITER,'\\',$file);
  1579. //        debug("added $name, $file, $bookmark, $group ");
  1580.         $link = $file;
  1581.         $link .= $bookmark ? "#$bookmark" :'';
  1582.         if ($group) {
  1583.             $this->KLinks[$group]['grouplink'] = $file;
  1584.             $this->KLinks[$group][] = array($name,$link);
  1585.         }
  1586.         $this->KLinks[] = array($name,$link);
  1587.     }
  1588.  
  1589.     /**
  1590.      * Get the table of contents for index.hhk
  1591.      * @return string contents of tocentry.tpl generated from $KLinks
  1592.      * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  1593.      */
  1594.     function generateKLinks()
  1595.     {
  1596.         $templ = &$this->newSmarty();
  1597.         $templ->assign('entry', array());
  1598.         foreach($this->KLinks as $group=>$link)
  1599.         {
  1600.             if (isset($group['grouplink'])) {
  1601.                 $templg = &$this->newSmarty();
  1602.                 $templg->assign('entry', array());
  1603.                 foreach($link as $k=>$sublink)
  1604.                 {
  1605.                     if ($k != 'grouplink') {
  1606.                         $templg->append('entry', array('paramname' => $sublink[0],'outputfile' => $sublink[1]));
  1607.                     }
  1608.                 }
  1609.                 $templ->append('entry', array('paramname' => $group, 'outputfile' => $link['grouplink'], 'tocsubentries' => $templg->fetch('tocentry.tpl') ));
  1610.             } else {
  1611.                 $templ->append('entry', array('paramname' => $link[0],'outputfile' => $link[1]));
  1612.             }
  1613.         }
  1614.         return $templ->fetch('tocentry.tpl');
  1615.     }
  1616.     
  1617.     /**
  1618.      * Create the phpdoc.hhp, contents.hhc files needed by MS HTML Help Compiler
  1619.      * to create a CHM file
  1620.      *
  1621.      * The output function generates the table of contents (contents.hhc)
  1622.      * and file list (phpdoc.hhp) files used to create a .CHM by the
  1623.      * free MS HTML Help compiler.
  1624.      * {@internal
  1625.      * Using {@link $hhp_files}, a list of all separate .html files
  1626.      * is created in CHM format, and written to phpdoc.hhp.  This list was
  1627.      * generated by {@link writefile}.
  1628.      *
  1629.      * Next, a call to the table of contents:
  1630.      *
  1631.      * {@source 12 2}
  1632.      *
  1633.      * finishes things off}}
  1634.      * @todo use to directly call html help compiler hhc.exe
  1635.      * @link http://www.microsoft.com/downloads/release.asp?releaseid=33071
  1636.      * @uses generateTOC() assigns to the toc template variable
  1637.      */
  1638.     function Output()
  1639.     {
  1640.         $templ = &$this->newSmarty();
  1641.         $file = $this->base_dir . PATH_DELIMITER;
  1642.         $file = str_replace('\\',PATH_DELIMITER,$file);
  1643.         $file = str_replace('//',PATH_DELIMITER,$file);
  1644.         $file = str_replace(PATH_DELIMITER,'\\',$file);
  1645.         $templ->assign('files',$this->hhp_files);
  1646.         $this->setTargetDir($this->base_dir);
  1647.         Converter::writefile('phpdoc.hhp',$templ->fetch('hhp.tpl'));
  1648.         $templ = &$this->newSmarty();
  1649.         $templ->assign('toc',$this->generateTOC());
  1650.         Converter::writefile('contents.hhc',$templ->fetch('contents.hhc.tpl'));
  1651.         $templ->assign('klinks',$this->generateKLinks());
  1652.         Converter::writefile('index.hhk',$templ->fetch('index.hhk.tpl'));
  1653.         phpDocumentor_out("NOTE: to create the documentation.chm file, you must now run Microsoft Help Workshop on phpdoc.hhp\n");
  1654.         phpDocumentor_out("To get the free Microsoft Help Workshop, browse to: http://go.microsoft.com/fwlink/?LinkId=14498\n");
  1655.         flush();
  1656.     }
  1657. }
  1658.  
  1659. /**
  1660.  * @access private
  1661.  * @global string name of the package to set as the first package
  1662.  */
  1663. function CHMdefault_pindexcmp($a, $b)
  1664. {
  1665.     global $phpDocumentor_DefaultPackageName;
  1666.     if ($a['title'] == $phpDocumentor_DefaultPackageName) return -1;
  1667.     if ($b['title'] == $phpDocumentor_DefaultPackageName) return 1;
  1668.     return strnatcasecmp($a['title'],$b['title']);
  1669. }
  1670.  
  1671. /** @access private */
  1672. function CHMdefault_lettersort($a, $b)
  1673. {
  1674.     return strnatcasecmp($a['letter'],$b['letter']);
  1675. }
  1676.  
  1677. /** @access private */
  1678. function CHMdefault_outputfilter($src, &$smarty)
  1679. {
  1680.     return str_replace('{$subdir}',$smarty->_tpl_vars['subdir'],$src);
  1681. }
  1682. ?>
  1683.