Source for file HTMLframesConverter.inc

Documentation is available at HTMLframesConverter.inc

  1. <?php
  2. /**
  3.  * HTML original framed output converter, modified to use Smarty Template.
  4.  * This Converter takes output from the {@link Parser} and converts it to HTML-ready output for use with {@link Smarty}.
  5.  *
  6.  * phpDocumentor :: automatic documentation generator
  7.  * 
  8.  * PHP versions 4 and 5
  9.  *
  10.  * Copyright (c) 2002-2006 Gregory Beaver
  11.  * 
  12.  * LICENSE:
  13.  * 
  14.  * This library is free software; you can redistribute it
  15.  * and/or modify it under the terms of the GNU Lesser General
  16.  * Public License as published by the Free Software Foundation;
  17.  * either version 2.1 of the License, or (at your option) any
  18.  * later version.
  19.  * 
  20.  * This library is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23.  * Lesser General Public License for more details.
  24.  * 
  25.  * You should have received a copy of the GNU Lesser General Public
  26.  * License along with this library; if not, write to the Free Software
  27.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28.  *
  29.  * @package    Converters
  30.  * @subpackage HTMLframes
  31.  * @author     Gregory Beaver <cellog@php.net>
  32.  * @copyright  2002-2006 Gregory Beaver
  33.  * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
  34.  * @version    CVS: $Id: HTMLframesConverter.inc,v 1.16 2007/04/19 20:20:57 ashnazg Exp $
  35.  * @filesource
  36.  * @link       http://www.phpdoc.org
  37.  * @link       http://pear.php.net/PhpDocumentor
  38.  * @see        parserDocBlock, parserInclude, parserPage, parserClass
  39.  * @see        parserDefine, parserFunction, parserMethod, parserVar
  40.  * @since      1.2
  41.  */
  42. /**
  43.  * HTML output converter.
  44.  * This Converter takes output from the {@link Parser} and converts it to HTML-ready output for use with {@link Smarty}.
  45.  *
  46.  * @package Converters
  47.  * @subpackage HTMLframes
  48.  * @see parserDocBlock, parserInclude, parserPage, parserClass, parserDefine, parserFunction, parserMethod, parserVar
  49.  * @author Greg Beaver <cellog@php.net>
  50.  * @since 1.2
  51.  * @version $Id: HTMLframesConverter.inc,v 1.16 2007/04/19 20:20:57 ashnazg Exp $
  52.  */
  53. {
  54.     /**
  55.      * This converter knows about the new root tree processing
  56.      * In order to fix PEAR Bug #6389
  57.      * @var boolean 
  58.      */
  59.     var $processSpecialRoots = true;
  60.     /**
  61.      * Smarty Converter wants elements sorted by type as well as alphabetically
  62.      * @see Converter::$sort_page_contents_by_type
  63.      * @var boolean 
  64.      */
  65.     var $sort_page_contents_by_type = true;
  66.     /** @var string */
  67.     var $outputformat = 'HTML';
  68.     /** @var string */
  69.     var $name = 'frames';
  70.     /**
  71.      * indexes of elements by package that need to be generated
  72.      * @var array 
  73.      */
  74.     var $leftindex = array('classes' => true'pages' => true'functions' => true'defines' => false'globals' => false);
  75.     
  76.     /**
  77.      * output directory for the current procedural page being processed
  78.      * @var string 
  79.      */
  80.     var $page_dir;
  81.     
  82.     /**
  83.      * target directory passed on the command-line.
  84.      * {@link $targetDir} is malleable, always adding package/ and package/subpackage/ subdirectories onto it.
  85.      * @var string 
  86.      */
  87.     var $base_dir;
  88.     
  89.     /**
  90.      * output directory for the current class being processed
  91.      * @var string 
  92.      */
  93.     var $class_dir;
  94.     
  95.     /**
  96.      * array of converted package page names.
  97.      * Used to link to the package page in the left index
  98.      * @var array Format: array(package => 1)
  99.      */
  100.     var $package_pages = array();
  101.     
  102.     /**
  103.      * controls formatting of parser informative output
  104.      * 
  105.      * Converter prints:
  106.      * "Converting /path/to/file.php... Procedural Page Elements... Classes..."
  107.      * Since HTMLdefaultConverter outputs files while converting, it needs to send a \n to start a new line.  However, if there
  108.      * is more than one class, output is messy, with multiple \n's just between class file output.  This variable prevents that
  109.      * and is purely cosmetic
  110.      * @var boolean 
  111.      */
  112.     var $juststarted = false;
  113.     
  114.     /**
  115.      * contains all of the template procedural page element loop data needed for the current template
  116.      * @var array 
  117.      */
  118.     var $current;
  119.     
  120.     /**
  121.      * contains all of the template class element loop data needed for the current template
  122.      * @var array 
  123.      */
  124.     var $currentclass;
  125.     var $wrote = false;
  126.     var $ric_set = array();
  127.     
  128.     /**
  129.      * sets {@link $base_dir} to $targetDir
  130.      * @see Converter()
  131.      */
  132.     function HTMLframesConverter(&$allp&$packp&$classes&$procpages$po$pp$qm$targetDir$templateDir$title)
  133.     {
  134.         Converter::Converter($allp$packp$classes$procpages,$po$pp$qm$targetDir$templateDir$title);
  135.         $this->base_dir = $targetDir;
  136.     }
  137.     
  138.     /**
  139.      * @deprecated in favor of PHP 4.3.0+ tokenizer-based source highlighting
  140.      */
  141.     function unmangle($sourcecode)
  142.     {
  143.         $sourcecode str_replace('<code>','<pre>',$sourcecode);
  144.         $sourcecode str_replace('</code>','</pre>',$sourcecode);
  145.         $sourcecode str_replace('<br />',"\n",$sourcecode);
  146.         $sourcecode str_replace('&nbsp;',' ',$sourcecode);
  147.         $sourcecode str_replace('&lt;','<',$sourcecode);
  148.         $sourcecode str_replace('&gt;','>',$sourcecode);
  149.         $sourcecode str_replace('&amp;','&',$sourcecode);
  150.         return $sourcecode;
  151.     }
  152.  
  153.     /**
  154.      * @param string full path to the source file
  155.      * @param string fully highlighted source code
  156.      */
  157.     function writeSource($path$value)
  158.     {
  159.         $templ &$this->newSmarty();
  160.         $pathinfo $this->proceduralpages->getPathInfo($path$this);
  161.         $templ->assign('source',$value);
  162.         $templ->assign('package',$pathinfo['package']);
  163.         $templ->assign('subpackage',$pathinfo['subpackage']);
  164.         $templ->assign('name',$pathinfo['name']);
  165.         $templ->assign('source_loc',$pathinfo['source_loc']);
  166.         $templ->assign('docs',$pathinfo['docs']);
  167.         $templ->assign("subdir",'../');
  168.         $templ->register_outputfilter('HTMLframes_outputfilter');
  169.         $this->setTargetDir($this->getFileSourcePath($this->base_dir));
  170.         phpDocumentor_out("\n");
  171.         $this->setSourcePaths($path);
  172.         $this->writefile($this->getFileSourceName($path).'.html',$templ->fetch('filesource.tpl'));
  173.     }
  174.     
  175.     function writeExample($title$path$source)
  176.     {
  177.         $templ &$this->newSmarty();
  178.         $templ->assign('source',$source);
  179.         if (empty($title))
  180.         {
  181.             $title 'example';
  182.             addWarning(PDERROR_EMPTY_EXAMPLE_TITLE$path$title);
  183.         }
  184.         $templ->assign('title',$title);
  185.         $templ->assign('file',$path);
  186.         $templ->assign("subdir",'../');
  187.         $templ->register_outputfilter('HTMLframes_outputfilter');
  188.         $this->setTargetDir($this->base_dir . PATH_DELIMITER '__examplesource');
  189.         phpDocumentor_out("\n");
  190.         $this->writefile('exsource_'.$path.'.html',$templ->fetch('examplesource.tpl'));
  191.     }
  192.  
  193.     function getExampleLink($path$title)
  194.     {
  195.         return $this->returnLink('../__examplesource' PATH_DELIMITER 'exsource_'.$path.'.html',$title);
  196.     }
  197.     
  198.     function getSourceLink($path)
  199.     {
  200.         return $this->returnLink('../__filesource/' .
  201.         $this->getFileSourceName($path).'.html','Source Code for this file');
  202.     }
  203.  
  204.     /**
  205.      * Retrieve a Converter-specific anchor to a segment of a source code file
  206.      * parsed via a {@tutorial tags.filesource.pkg} tag.
  207.      * @param string full path to source file
  208.      * @param string name of anchor
  209.      * @param string link text, if this is a link
  210.      * @param boolean returns either a link or a destination based on this
  211.      *                 parameter
  212.      * @return string link to an anchor, or the anchor
  213.      */
  214.     function getSourceAnchor($sourcefile,$anchor,$text '',$link false)
  215.     {
  216.         if ($link{
  217.             return $this->returnLink('../__filesource/' .
  218.                 $this->getFileSourceName($sourcefile'.html#a' $anchor$text);
  219.         else {
  220.             return '<a name="a'.$anchor.'"></a>';
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Return a line of highlighted source code with formatted line number
  226.      *
  227.      * If the $path is a full path, then an anchor to the line number will be
  228.      * added as well
  229.      * @param integer line number
  230.      * @param string highlighted source code line
  231.      * @param false|stringfull path to @filesource file this line is a part of,
  232.      *         if this is a single line from a complete file.
  233.      * @return string formatted source code line with line number
  234.      */
  235.     function sourceLine($linenumber$line$path false)
  236.     {
  237.         $extra '';
  238.         if (strlen(str_replace("\n"''$line)) == 0{
  239.             $extra '&nbsp;';
  240.         }
  241.         if ($path)
  242.         {
  243.             return '<li><div class="src-line">' $this->getSourceAnchor($path$linenumber.
  244.                    str_replace("\n",'',$line$extra .
  245.                    "</div></li>\n";
  246.         else
  247.         {
  248.             return '<li><div class="src-line">' str_replace("\n",'',$line.
  249.                 "$extra</div></li>\n";
  250.         }
  251.     }
  252.     
  253.     /**
  254.      * Used to convert the <<code>> tag in a docblock
  255.      * @param string 
  256.      * @param boolean 
  257.      * @return string 
  258.      */
  259.     function ProgramExample($example$tutorial false$inlinesourceparse null/*false*/,
  260.                             $class null/*false*/$linenum null/*false*/$filesourcepath null/*false*/)
  261.     {
  262.         return '<div class="src-code"><ol>' parent::ProgramExample($example$tutorial$inlinesourceparse$class$linenum$filesourcepath)
  263.                .'</ol></div>';
  264.     }
  265.     
  266.     /**
  267.      * @param string 
  268.      */
  269.     function TutorialExample($example)
  270.     {
  271.         $trans $this->template_options['desctranslate'];
  272.         $this->template_options['desctranslate'array();
  273.         $example '<ol>' parent::TutorialExample($example)
  274.                .'</ol>';
  275.         $this->template_options['desctranslate'$trans;
  276.         if (!isset($this->template_options['desctranslate'])) return $example;
  277.         if (!isset($this->template_options['desctranslate']['code'])) return $example;
  278.         $example $this->template_options['desctranslate']['code'$example;
  279.         if (!isset($this->template_options['desctranslate']['/code'])) return $example;
  280.         return $example $this->template_options['desctranslate']['/code'];
  281.     }
  282.     
  283.     function getCurrentPageLink()
  284.     {
  285.         return $this->curname '.html';
  286.     }
  287.  
  288.     /**
  289.      * Uses htmlspecialchars() on the input
  290.      */
  291.     function postProcess($text)
  292.     {
  293.         if ($this->highlightingSource{
  294.             return str_replace(array(' ',"\t")array('&nbsp;''&nbsp;&nbsp;&nbsp;'),
  295.                 htmlspecialchars($text));
  296.         }
  297.         return htmlspecialchars($text);
  298.     }
  299.     
  300.     /**
  301.      * Use the template tutorial_toc.tpl to generate a table of contents for HTML
  302.      * @return string table of contents formatted for use in the current output format
  303.      * @param array format: array(array('tagname' => section, 'link' => returnsee link, 'id' => anchor name, 'title' => from title tag),...)
  304.      */
  305.     function formatTutorialTOC($toc)
  306.     {
  307.         $template &$this->newSmarty();
  308.         $template->assign('toc',$toc);
  309.         return $template->fetch('tutorial_toc.tpl');
  310.     }
  311.     
  312.     function &SmartyInit(&$templ)
  313.     {
  314.         if (!isset($this->package_index))
  315.         foreach($this->all_packages as $key => $val)
  316.         {
  317.             if (isset($this->pkg_elements[$key]))
  318.             {
  319.                 if (!isset($start)) $start $key;
  320.                 $this->package_index[array('link' => "li_$key.html"'title' => $key);
  321.             }
  322.         }
  323.         $templ->assign("packageindex",$this->package_index);
  324.         $templ->assign("subdir",'');
  325.         return $templ;
  326.     }
  327.     
  328.     /**
  329.      * Writes out the template file of {@link $class_data} and unsets the template to save memory
  330.      * @see registerCurrentClass()
  331.      * @see parent::endClass()
  332.      */
  333.     function endClass()
  334.     {
  335.         $a '../';
  336.         if (!empty($this->subpackage)) $a .= '../';
  337.         if ($this->juststarted)
  338.         {
  339.             $this->juststarted = false;
  340.             phpDocumentor_out("\n");
  341.             flush();
  342.         }
  343.         $this->setTargetDir($this->base_dir . PATH_DELIMITER $this->class_dir);
  344.         $this->class_data->assign("subdir",$a);
  345.         $this->class_data->register_outputfilter('HTMLframes_outputfilter');
  346.         $this->writefile($this->class . '.html',$this->class_data->fetch('class.tpl'));
  347.         unset($this->class_data);
  348.     }
  349.     
  350.     /**
  351.      * Writes out the template file of {@link $page_data} and unsets the template to save memory
  352.      * @see registerCurrent()
  353.      * @see parent::endPage()
  354.      */
  355.     function endPage()
  356.     {
  357.         $this->package = $this->curpage->package;
  358.         $this->subpackage = $this->curpage->subpackage;
  359.         $a '../';
  360.         if (!empty($this->subpackage)) $a .= '../';
  361.         $this->setTargetDir($this->base_dir . PATH_DELIMITER $this->page_dir);
  362.         $this->page_data->assign("package",$this->package);
  363.         $this->page_data->assign("subdir",$a);
  364.         $this->page_data->register_outputfilter('HTMLframes_outputfilter');
  365.         $this->writefile($this->page . '.html',$this->page_data->fetch('page.tpl'));
  366.         unset($this->page_data);
  367.     }
  368.     
  369.     /**
  370.      * @param string 
  371.      * @param string 
  372.      * @return string &lt;a href="'.$link.'">'.$text.'</a&gt;
  373.      */
  374.     function returnLink($link,$text)
  375.     {
  376.         return '<a href="'.$link.'">'.$text.'</a>';
  377.     }
  378.     
  379.     function makeLeft()
  380.     {
  381.         foreach($this->page_elements as $package => $o1)
  382.         {
  383.             foreach($o1 as $subpackage => $links)
  384.             {
  385.                 for($i=0;$i<count($links);$i++)
  386.                 {
  387.                     $left[$package][$subpackage]['files'][=
  388.                         array("link" => $this->getId($links[$i])"title" => $links[$i]->name);
  389.                 }
  390.             }
  391.         }
  392.         $interfaces $classes false;
  393.         foreach($this->class_elements as $package => $o1)
  394.         {
  395.             foreach($o1 as $subpackage => $links)
  396.             {
  397.                 for($i=0;$i<count($links);$i++)
  398.                 {
  399.                     $class $this->classes->getClassByPackage($links[$i]->name$links[$i]->package);
  400.                     $isinterface $isclass false;
  401.                     if ($class->isInterface()) {
  402.                         $isinterface true;
  403.                         $interfaces true;
  404.                     else {
  405.                         $isclass true;
  406.                         $classes true;
  407.                     }
  408.                     if ($class && isset($class->docblock&& $class->docblock->hasaccess{
  409.                         $left[$package][$subpackage]['classes'][=
  410.                             array("link" => $this->getId($links[$i])
  411.                                   "title" => $links[$i]->name,
  412.                                   'is_interface' => $isinterface,
  413.                                   'is_class' => $isclass,
  414.                                   "access" => $class->docblock->tags['access'][0]->value,
  415.                                   "abstract" => isset ($class->docblock->tags['abstract'][0]));
  416.                     else {
  417.                         $left[$package][$subpackage]['classes'][=
  418.                             array("link" => $this->getId($links[$i])
  419.                                   "title" => $links[$i]->name,
  420.                                   'is_interface' => $isinterface,
  421.                                   'is_class' => $isclass,
  422.                                   "access" => 'public',
  423.                                   "abstract" => isset ($class->docblock->tags['abstract'][0]));
  424.                     }
  425.                 }
  426.             }
  427.         }
  428.         foreach($this->function_elements as $package => $o1)
  429.         {
  430.             foreach($o1 as $subpackage => $links)
  431.             {
  432.                 for($i=0;$i<count($links);$i++)
  433.                 {
  434.                     $left[$package][$subpackage]['functions'][=
  435.                         array("link" => $this->getId($links[$i])"title" => $links[$i]->name);
  436.                 }
  437.             }
  438.         }
  439.         $ret array();
  440.         foreach($left as $package => $r)
  441.         {
  442.             $pd 'blank';
  443.             if (isset($this->package_pages[$package])) $pd $package.'/package_'.$package.'.html';
  444.             if (!isset($r['']))
  445.             {
  446.                 $pt false;
  447.                 $ptnoa false;
  448.    &n