phpDocumentor Converters
HTMLframes
[ class tree: Converters ] [ index: Converters ] [ all elements ]

Source for file HTMLSmartyConverter.inc

Documentation is available at HTMLSmartyConverter.inc

  1. <?php
  2. /**
  3.  * HTML output converter for 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) 2000-2006 Joshua Eichorn, 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     Joshua Eichorn <jeichorn@phpdoc.org>
  32.  * @author     Greg Beaver <cellog@php.net>
  33.  * @copyright  2000-2006 Joshua Eichorn, Gregory Beaver
  34.  * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
  35.  * @version    CVS: $Id: HTMLSmartyConverter.inc,v 1.15 2007/04/19 20:20:57 ashnazg Exp $
  36.  * @filesource
  37.  * @link       http://www.phpdoc.org
  38.  * @link       http://pear.php.net/PhpDocumentor
  39.  * @see        parserDocBlock, parserInclude, parserPage, parserClass
  40.  * @see        parserDefine, parserFunction, parserMethod, parserVar
  41.  * @since      1.0rc1
  42.  */
  43. /**
  44.  * HTML output converter.
  45.  * This Converter takes output from the {@link Parser} and converts it to HTML-ready output for use with {@link Smarty}.
  46.  *
  47.  * @package Converters
  48.  * @subpackage HTMLSmarty
  49.  * @see parserDocBlock, parserInclude, parserPage, parserClass, parserDefine, parserFunction, parserMethod, parserVar
  50.  * @author Greg Beaver <cellog@php.net>
  51.  * @since 1.0rc1
  52.  * @version $Revision: 1.15 $
  53.  */
  54. {
  55.     /**
  56.      * This converter knows about the new root tree processing
  57.      * In order to fix PEAR Bug #6389
  58.      * @var boolean 
  59.      */
  60.     var $processSpecialRoots = true;
  61.     /**
  62.      * Smarty Converter wants elements sorted by type as well as alphabetically
  63.      * @see Converter::$sort_page_contents_by_type
  64.      * @var boolean 
  65.      */
  66.     var $sort_page_contents_by_type = true;
  67.     /** @var string */
  68.     var $outputformat = 'HTML';
  69.     /** @var string */
  70.     var $name = 'Smarty';
  71.     /**
  72.      * indexes of elements by package that need to be generated
  73.      * @var array 
  74.      */
  75.     var $leftindex = array('classes' => true'pages' => true'functions' => true'defines' => false'globals' => false);
  76.     
  77.     /**
  78.      * output directory for the current procedural page being processed
  79.      * @var string 
  80.      */
  81.     var $page_dir;
  82.     
  83.     /**
  84.      * target directory passed on the command-line.
  85.      * {@link $targetDir} is malleable, always adding package/ and package/subpackage/ subdirectories onto it.
  86.      * @var string 
  87.      */
  88.     var $base_dir;
  89.     
  90.     /**
  91.      * output directory for the current class being processed
  92.      * @var string 
  93.      */
  94.     var $class_dir;
  95.     
  96.     /**
  97.      * array of converted package page names.
  98.      * Used to link to the package page in the left index
  99.      * @var array Format: array(package => 1)
  100.      */
  101.     var $package_pages = array();
  102.     
  103.     /**
  104.      * controls formatting of parser informative output
  105.      * 
  106.      * Converter prints:
  107.      * "Converting /path/to/file.php... Procedural Page Elements... Classes..."
  108.      * Since HTMLdefaultConverter outputs files while converting, it needs to send a \n to start a new line.  However, if there
  109.      * is more than one class, output is messy, with multiple \n's just between class file output.  This variable prevents that
  110.      * and is purely cosmetic
  111.      * @var boolean 
  112.      */
  113.     var $juststarted = false;
  114.     
  115.     /**
  116.      * contains all of the template procedural page element loop data needed for the current template
  117.      * @var array 
  118.      */
  119.     var $current;
  120.     
  121.     /**
  122.      * contains all of the template class element loop data needed for the current template
  123.      * @var array 
  124.      */
  125.     var $currentclass;
  126.     var $wrote = false;
  127.     var $ric_set = array();
  128.     
  129.     /**
  130.      * sets {@link $base_dir} to $targetDir
  131.      * @see Converter()
  132.      */
  133.     
  134.     /**#@+
  135.      * @access private
  136.      */
  137.     var $_classleft_cache false;
  138.     var $_classcontents_cache false;
  139.     var $_pagecontents_cache false;
  140.     var $_pageleft_cache false;
  141.     var $_done_package_index false;
  142.     var $_ric_done false;
  143.     var $_wrote_tdir false;
  144.     var $ric_contents array();
  145.     /**#@-*/
  146.     
  147.     function HTMLSmartyConverter(&$allp&$packp&$classes&$procpages$po$pp$qm$targetDir$templateDir$title)
  148.     {
  149.         Converter::Converter($allp$packp$classes$procpages,$po$pp$qm$targetDir$templateDir$title);
  150.         $this->base_dir = $targetDir;
  151.     }
  152.     
  153.     function writeSource($path$value)
  154.     {
  155.         $templ &$this->newSmarty();
  156.         $pathinfo $this->proceduralpages->getPathInfo($path$this);
  157.         $templ->assign('source',$value);
  158.         $templ->assign('package',$pathinfo['package']);
  159.         $templ->assign('subpackage',$pathinfo['subpackage']);
  160.         $templ->assign('name',$pathinfo['name']);
  161.         $templ->assign('source_loc',$pathinfo['source_loc']);
  162.         $templ->assign('docs',$pathinfo['docs']);
  163.         $templ->assign("subdir",'../');
  164.         $templ->register_outputfilter('HTMLSmarty_outputfilter');
  165.         $this->setTargetDir($this->getFileSourcePath($this->base_dir));
  166.         phpDocumentor_out("\n");
  167.         $this->setSourcePaths($path);
  168.         $this->writefile($this->getFileSourceName($path).'.html',$templ->fetch('filesource.tpl'));
  169.     }
  170.     
  171.     function writeExample($title$path$source)
  172.     {
  173.         $templ &$this->newSmarty();
  174.         $templ->assign('source',$source);
  175.         if (empty($title))
  176.         {
  177.             $title 'example';
  178.             addWarning(PDERROR_EMPTY_EXAMPLE_TITLE$path$title);
  179.         }
  180.         $templ->assign('title',$title);
  181.         $templ->assign('file',$path);
  182.         $templ->assign("subdir",'../');
  183.         $templ->register_outputfilter('HTMLSmarty_outputfilter');
  184.         $this->setTargetDir($this->base_dir . PATH_DELIMITER '__examplesource');
  185.         phpDocumentor_out("\n");
  186.         $this->writefile('exsource_'.$path.'.html',$templ->fetch('examplesource.tpl'));
  187.     }
  188.  
  189.     function getExampleLink($path$title)
  190.     {
  191.         return $this->returnLink('../__examplesource' PATH_DELIMITER 'exsource_'.$path.'.html',$title);
  192.     }
  193.     
  194.     function getSourceLink($path)
  195.     {
  196.         return $this->returnLink('../__filesource/' .
  197.         $this->getFileSourceName($path).'.html','Source Code for this file');
  198.     }
  199.  
  200.     /**
  201.      * Retrieve a Converter-specific anchor to a segment of a source code file
  202.      * parsed via a {@tutorial tags.filesource.pkg} tag.
  203.      * @param string full path to source file
  204.      * @param string name of anchor
  205.      * @param string link text, if this is a link
  206.      * @param boolean returns either a link or a destination based on this
  207.      *                 parameter
  208.      * @return string link to an anchor, or the anchor
  209.      */
  210.     function getSourceAnchor($sourcefile,$anchor,$text '',$link false)
  211.     {
  212.         if ($link{
  213.             return $this->returnLink('../__filesource/' .
  214.                 $this->getFileSourceName($sourcefile'.html#a' $anchor$text);
  215.         else {
  216.             return '<a name="a'.$anchor.'"></a>';
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Return a line of highlighted source code with formatted line number
  222.      *
  223.      * If the $path is a full path, then an anchor to the line number will be
  224.      * added as well
  225.      * @param integer line number
  226.      * @param string highlighted source code line
  227.      * @param false|stringfull path to @filesource file this line is a part of,
  228.      *         if this is a single line from a complete file.
  229.      * @return string formatted source code line with line number
  230.      */
  231.     function sourceLine($linenumber$line$path false)
  232.     {
  233.         $extra '';
  234.         if (strlen(str_replace("\n"''$line)) == 0{
  235.             $extra '&nbsp;';
  236.         }
  237.         if ($path)
  238.         {
  239.             return '<li><div class="src-line">' $this->getSourceAnchor($path$linenumber.
  240.                    str_replace("\n",'',$line$extra .
  241.                    "</div></li>\n";
  242.         else
  243.         {
  244.             return '<li><div class="src-line">' str_replace("\n",'',$line"$extra</div></li>\n";
  245.         }
  246.     }
  247.     
  248.     /**
  249.      * Used to convert the <<code>> tag in a docblock
  250.      * @param string 
  251.      * @param boolean 
  252.      * @return string 
  253.      */
  254.     function ProgramExample($example$tutorial false$inlinesourceparse null/*false*/,
  255.                             $class null/*false*/$linenum null/*false*/$filesourcepath null/*false*/)
  256.     {
  257.         $trans $this->template_options['desctranslate'];
  258.         $this->template_options['desctranslate'array();
  259.         $example '<ol>' parent::ProgramExample($example$tutorial$inlinesourceparse$class$linenum$filesourcepath)
  260.                .'</ol>';
  261.         $this->template_options['desctranslate'$trans;
  262.         if (!isset($this->template_options['desctranslate'])) return $example;
  263.         if (!isset($this->template_options['desctranslate']['code'])) return $example;
  264.         $example $this->template_options['desctranslate']['code'$example;
  265.         if (!isset($this->template_options['desctranslate']['/code'])) return $example;
  266.         return $example $this->template_options['desctranslate']['/code'];
  267.     }
  268.     
  269.     /**
  270.      * @param string 
  271.      */
  272.     function TutorialExample($example)
  273.     {
  274.         $trans $this->template_options['desctranslate'];
  275.         $this->template_options['desctranslate'array();
  276.         $example '<ol>' parent::TutorialExample($example)
  277.                .'</ol>';
  278.         $this->template_options['desctranslate'$trans;
  279.         if (!isset($this->template_options['desctranslate'])) return $example;
  280.         if (!isset($this->template_options['desctranslate']['code'])) return $example;
  281.         $example $this->template_options['desctranslate']['code'$example;
  282.         if (!isset($this->template_options['desctranslate']['/code'])) return $example;
  283.         return $example $this->template_options['desctranslate']['/code'];
  284.     }
  285.     
  286.     function getCurrentPageLink()
  287.     {
  288.         return $this->curname '.html';
  289.     }
  290.  
  291.     function unmangle($sourcecode)
  292.     {
  293.         $sourcecode str_replace('&nbsp;',' ',$sourcecode);
  294.         $sourcecode str_replace('&amp;','&',$sourcecode);
  295.         $sourcecode str_replace('<br />',"<br>",$sourcecode);
  296.         $sourcecode str_replace('<code>','<pre>',$sourcecode);
  297.         $sourcecode str_replace('</code>','</pre>',$sourcecode);
  298.         $sourcecode str_replace('&lt;','<',$sourcecode);
  299.         $sourcecode str_replace('&gt;','>',$sourcecode);
  300.         return $sourcecode;
  301.     }
  302.     
  303.     /**
  304.      * Uses htmlspecialchars() on the input
  305.      */
  306.     function postProcess($text)
  307.     {
  308.         if ($this->highlightingSource{
  309.             return str_replace(array(' ',"\t")array('&nbsp;''&nbsp;&nbsp;&nbsp;'),
  310.                 htmlspecialchars($text));
  311.         }
  312.         return htmlspecialchars($text);
  313.     }
  314.     
  315.     /**
  316.      * Use the template tutorial_toc.tpl to generate a table of contents for HTML
  317.      * @return string table of contents formatted for use in the current output format
  318.      * @param array format: array(array('tagname' => section, 'link' => returnsee link, 'id' => anchor name, 'title' => from title tag),...)
  319.      */
  320.     function formatTutorialTOC($toc)
  321.     {
  322.         $template &$this->newSmarty();
  323.         $template->assign('toc',$toc);
  324.         return $template->fetch('tutorial_toc.tpl');
  325.     }
  326.     
  327.     function &SmartyInit(&$templ)
  328.     {
  329.         $this->makeLeft();
  330.         $templ->assign("ric",$this->ric_set);
  331.         $templ->assign("packageindex",$this->package_index);
  332.         $templ->assign('hastodos',count($this->todoList));
  333.         $templ->assign('todolink','todolist.html');
  334.         $templ->assign("subdir",'');
  335.         return $templ;
  336.     }
  337.     
  338.     /**
  339.      * Writes out the template file of {@link $class_data} and unsets the template to save memory
  340.      * @see registerCurrentClass()
  341.      * @see parent::endClass()
  342.      */
  343.     function endClass()
  344.     {
  345.         $a '../';
  346.         if (!empty($this->subpackage)) $a .= '../';
  347.         if ($this->juststarted)
  348.         {
  349.             $this->juststarted = false;
  350.             phpDocumentor_out("\n");
  351.             flush();
  352.         }
  353.         $this->setTargetDir($this->base_dir . PATH_DELIMITER $this->class_dir);
  354.         $classleft $this->getClassLeft();
  355.         $this->class_data->assign("compiledfileindex",$this->getPageLeft());
  356.         $this->class_data->assign("compiledclassindex",$classleft['class']);
  357.         $this->class_data->assign("compiledinterfaceindex",$classleft['interface']);
  358.         $this->class_data->assign("tutorials",$this->getTutorialList());
  359.         $this->class_data->assign("contents",$this->getClassContents());
  360.         $this->class_data->assign("packageindex",$this->package_index);
  361.         $this->class_data->assign("package",$this->package);
  362.         $this->class_data->assign("subdir",$a);
  363.         $this->class_data->register_outputfilter('HTMLSmarty_outputfilter');
  364.         $this->writefile($this->class . '.html',$this->class_data->fetch('class.tpl'));
  365.         unset($this->class_data);
  366.     }
  367.     
  368.     function getTutorialList()
  369.     {
  370.         static $cache false;
  371.         if ($cache)
  372.         {
  373.             if (isset($cache[$this->package])) return $cache[$this->package];
  374.         }
  375.         $package $this->package;
  376.         if (!isset($this->tutorials[$package])) return false;
  377.         foreach($this->tutorials[$packageas $subpackage => $blah)
  378.         {
  379.             $subpackages[$subpackage;
  380.         }
  381.         $tutes array();
  382.         foreach($subpackages as $subpackage)
  383.         {
  384.             if (isset($this->tutorial_tree&& is_array($this->tutorial_tree))
  385.             foreach($this->tutorial_tree as $root => $tr)
  386.             {
  387.                 if ($tr['tutorial']->package == $package && $tr['tutorial']->subpackage == $subpackage)
  388.                 $tutes[$tr['tutorial']->tutorial_type][$this->getTutorialTree($tr['tutorial']);
  389.             }
  390.         }
  391.         $cache[$this->package$tutes;
  392.         return $tutes;
  393.     }
  394.     
  395.     function getTutorialTree($tutorial,$k false)
  396.     {
  397.         $ret '';
  398.         if (is_object($tutorial)) $tree parent::getTutorialTree($tutorial)else $tree $tutorial;
  399.         if (!$tree)
  400.         {
  401.             $template &$this->newSmarty();
  402.             $template->assign('subtree',false);
  403.             $template->assign('name',str_replace('.','',$tutorial->name));
  404.             $template->assign('parent',false);
  405.             $template->assign('haskids',false);
  406.             $template->assign('kids','');
  407.             $link new tutorialLink;
  408.             $t $tutorial;
  409.             $link->addLink('',$t->path,$t->name,$t->package,$t->subpackage,$t->getTitle($this));
  410.             $main array('link' => $this->getId($link)'title' => $link->title);
  411.             $template->assign('main',$main);
  412.             return $template->fetch('tutorial_tree.tpl');
  413.         }
  414.         if (isset($tree['kids']))
  415.         {
  416.             foreach($tree['kids'as $subtree)
  417.             {
  418.                 $ret .= $this->getTutorialTree($subtreetrue);
  419.             }
  420.         }
  421.         $template &$this->newSmarty();
  422.         $template->assign('subtree',$k);
  423.         $template->assign('name',str_replace('.','',$tree['tutorial']->name));
  424.         $template->assign('parent',($k str_replace('.','',$tree['tutorial']->parent->namefalse));
  425.         $template->assign('haskids',strlen($ret));
  426.         $template->assign('kids',$ret);
  427.         $link new tutorialLink;
  428.         $t $tree['tutorial'];
  429.         $link->addLink('',$t->path,$t->name,$t->package,$t->subpackage,$t->getTitle($this));
  430.         $main array('link' => $this->getId($link)