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

Source for file Converter.inc

Documentation is available at Converter.inc

  1. <?php
  2. /**
  3.  * Base class for all Converters
  4.  *
  5.  * phpDocumentor :: automatic documentation generator
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * Copyright (c) 2001-2006 Gregory Beaver
  10.  * 
  11.  * LICENSE:
  12.  * 
  13.  * This library is free software; you can redistribute it
  14.  * and/or modify it under the terms of the GNU Lesser General
  15.  * Public License as published by the Free Software Foundation;
  16.  * either version 2.1 of the License, or (at your option) any
  17.  * later version.
  18.  * 
  19.  * This library is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22.  * Lesser General Public License for more details.
  23.  * 
  24.  * You should have received a copy of the GNU Lesser General Public
  25.  * License along with this library; if not, write to the Free Software
  26.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27.  *
  28.  * @package    Converters
  29.  * @author     Greg Beaver <cellog@php.net>
  30.  * @copyright  2001-2006 Gregory Beaver
  31.  * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
  32.  * @version    CVS: $Id: Converter.inc,v 1.39 2007/12/19 02:16:49 ashnazg Exp $
  33.  * @filesource
  34.  * @link       http://www.phpdoc.org
  35.  * @link       http://pear.php.net/PhpDocumentor
  36.  * @see        parserDocBlock, parserInclude, parserPage, parserClass
  37.  * @see        parserDefine, parserFunction, parserMethod, parserVar
  38.  * @since      1.0rc1
  39.  */
  40. /**
  41.  * Smarty template files
  42.  */
  43. include_once("phpDocumentor/Smarty-2.6.0/libs/Smarty.class.php");
  44. /**
  45.  * Base class for all output converters.
  46.  *
  47.  * The Converter marks the final stage in phpDocumentor.  phpDocumentor works
  48.  * in this order:
  49.  *
  50.  * <pre>Parsing => Intermediate Parsing organization => Conversion to output</pre>
  51.  *
  52.  * A Converter takes output from the {@link phpDocumentor_IntermediateParser} and
  53.  * converts it to output.  With version 1.2, phpDocumentor includes a variety
  54.  * of output converters:
  55.  * <ul>
  56.  *  <li>{@link HTMLframesConverter}</li>
  57.  *  <li>{@link HTMLSmartyConverter}</li>
  58.  *  <li>{@link PDFdefaultConverter}</li>
  59.  *  <li>{@link CHMdefaultConverter}</li>
  60.  *  <li>{@link CSVdia2codeConverter}</li>
  61.  *  <li>{@link XMLDocBookConverter}</li>
  62.  * </ul>
  63.  * {@internal 
  64.  * The converter takes output directly from {@link phpDocumentor_IntermediateParser}
  65.  * and using {@link walk()} or {@link walk_everything} (depending on the value of
  66.  * {@link $sort_absolutely_everything}) it "walks" over an array of phpDocumentor elements.}}}
  67.  *
  68.  * @package Converters
  69.  * @abstract
  70.  * @author Greg Beaver <cellog@php.net>
  71.  * @since 1.0rc1
  72.  * @version $Id: Converter.inc,v 1.39 2007/12/19 02:16:49 ashnazg Exp $
  73.  */
  74. class Converter
  75. {
  76.     /**
  77.      * This converter knows about the new root tree processing
  78.      * In order to fix PEAR Bug #6389
  79.      * @var boolean 
  80.      */
  81.     var $processSpecialRoots = false;
  82.     /**
  83.      * output format of this converter
  84.      *
  85.      * in Child converters, this will match the first part of the -o command-line
  86.      * as in -o HTML:frames:default "HTML"
  87.      * @tutorial phpDocumentor.howto.pkg#using.command-line.output
  88.      * @var string 
  89.      */
  90.     var $outputformat = 'Generic';
  91.     /**
  92.      * package name currently being converted
  93.      * @var string 
  94.      */
  95.     var $package = 'default';
  96.     /**
  97.      * subpackage name currently being converted
  98.      * @var string 
  99.      */
  100.     var $subpackage = '';
  101.     /**
  102.      * set to a classname if currently parsing a class, false if not
  103.      * @var string|false
  104.      */
  105.     var $class = false;
  106.     /**#@+
  107.      * @access private
  108.      */
  109.     /**
  110.      * the workhorse of linking.
  111.      *
  112.      * This array is an array of link objects of format:
  113.      * [package][subpackage][eltype][elname] = descendant of {@link abstractLink}
  114.      * eltype can be page|function|define|class|method|var
  115.      * if eltype is method or var, the array format is:
  116.      * [package][subpackage][eltype][class][elname]
  117.      * @var array 
  118.      * @see functionLink, pageLink, classLink, defineLink, methodLink, varLink, globalLink
  119.      */
  120.     var $links array();
  121.  
  122.     /**
  123.      * the workhorse of linking, with allowance for support of multiple
  124.      * elements in different files.
  125.      *
  126.      * This array is an array of link objects of format:
  127.      * [package][subpackage][eltype][file][elname] = descendant of {@link abstractLink}
  128.      * eltype can be function|define|class|method|var
  129.      * if eltype is method or var, the array format is:
  130.      * [package][subpackage][eltype][file][class][elname]
  131.      * @var array 
  132.      * @see functionLink, pageLink, classLink, defineLink, methodLink, varLink, globalLink
  133.     */
  134.     var $linkswithfile array();
  135.     /**#@-*/
  136.     /**
  137.      * set to value of -po commandline
  138.      * @tutorial phpDocumentor.howto.pkg#using.command-line.packageoutput
  139.      * @var mixed 
  140.      */
  141.     var $package_output;
  142.  
  143.     /**
  144.      * name of current page being converted
  145.      * @var string 
  146.      */
  147.     var $page;
  148.  
  149.     /**
  150.      * path of current page being converted
  151.      * @var string 
  152.      */
  153.     var $path;
  154.  
  155.     /**
  156.      * template for the procedural page currently being processed
  157.      * @var Smarty 
  158.      */
  159.     var $page_data;
  160.  
  161.     /**
  162.      * template for the class currently being processed
  163.      * @var Smarty 
  164.      */
  165.     var $class_data;
  166.  
  167.     /**
  168.      * current procedural page being processed
  169.      * @var parserPage 
  170.      */
  171.     var $curpage;
  172.     /**
  173.      * alphabetical index of all elements sorted by package, subpackage, page,
  174.      * and class.
  175.      * @var array Format: array(package => array(subpackage => array('page'|'class' => array(path|classname => array(element, element,...)))))
  176.      * @uses $sort_absolutely_everything if true, then $package_elements is used,
  177.      *        otherwise, the {@link ParserData::$classelements} and
  178.      *        {@link ParserData::$pageelements} variables are used
  179.      */
  180.     var $package_elements = array();
  181.     /**
  182.      * alphabetical index of all elements
  183.      *
  184.      * @var array Format: array(first letter of element name => array({@link parserElement} or {@link parserPage},...))
  185.      * @see formatIndex(), HTMLframesConverter::formatIndex()
  186.      */
  187.     var $elements = array();
  188.     /**
  189.      * alphabetized index of procedural pages by package
  190.      *
  191.      * @see $leftindex
  192.      * @var array Format: array(package => array(subpackage => array({@link pageLink} 1,{@link pageLink} 2,...)
  193.      */
  194.     var $page_elements = array();
  195.     /**
  196.      * alphabetized index of defines by package
  197.      *
  198.      * @see $leftindex
  199.      * @var array Format: array(package => array(subpackage => array({@link defineLink} 1,{@link defineLink} 2,...)
  200.      */
  201.     var $define_elements = array();
  202.     /**
  203.      * alphabetized index of classes by package
  204.      *
  205.      * @see $leftindex
  206.      * @var array Format: array(package => array(subpackage => array({@link classLink} 1,{@link classLink} 2,...)
  207.      */
  208.     var $class_elements = array();
  209.     /**
  210.      * alphabetized index of global variables by package
  211.      *
  212.      * @see $leftindex
  213.      * @var array Format: array(package => array(subpackage => array({@link globalLink} 1,{@link globalLink} 2,...)
  214.      */
  215.     var $global_elements = array();
  216.     /**
  217.      * alphabetized index of functions by package
  218.      *
  219.      * @see $leftindex
  220.      * @var array Format: array(package => array(subpackage => array({@link functionLink} 1,{@link functionLink} 2,...)
  221.      */
  222.     var $function_elements = array();
  223.     /**
  224.      * alphabetical index of all elements, indexed by package/subpackage
  225.      *
  226.      * @var array Format: array(first letter of element name => array({@link parserElement} or {@link parserPage},...))
  227.      * @see formatPkgIndex(), HTMLframesConverter::formatPkgIndex()
  228.      */
  229.     var $pkg_elements = array();
  230.  
  231.     /**
  232.      * alphabetical index of all elements on a page by package/subpackage
  233.      *
  234.      * The page itself has a link under ###main
  235.      * @var array Format: array(package => array(subpackage => array(path => array({@link abstractLink} descendant 1, ...)))
  236.      * @see formatLeftIndex()
  237.      */
  238.     var $page_contents = array();
  239.  
  240.     /**
  241.      * This determines whether the {@link $page_contents} array should be sorted by element type as well as alphabetically by name
  242.      * @see sortPageContentsByElementType()
  243.      * @var boolean 
  244.      */
  245.     var $sort_page_contents_by_type = false;
  246.     /**
  247.      * This is used if the content must be passed in the order it should be read, i.e. by package, procedural then classes
  248.      *
  249.      * This fixes bug 637921, and is used by {@link PDFdefaultConverter}
  250.      */
  251.     var $sort_absolutely_everything = false;
  252.     /**
  253.      * alphabetical index of all methods and vars in a class by package/subpackage
  254.      *
  255.      * The class itself has a link under ###main
  256.      * @var array 
  257.      *  Format:<pre>
  258.      *  array(package =>
  259.      *        array(subpackage =>
  260.      *              array(path =>
  261.      *                    array(class =>
  262.      *                          array({@link abstractLink} descendant 1, ...
  263.      *                         )
  264.      *                   )
  265.      *             )
  266.      *       )</pre>
  267.      * @see formatLeftIndex()
  268.      */
  269.     var $class_contents = array();
  270.     /**
  271.      * controls processing of elements marked private with @access private
  272.      *
  273.      * defaults to false.  Set with command-line --parseprivate or -pp
  274.      * @var bool 
  275.      */
  276.     var $parseprivate;
  277.     /**
  278.      * controls display of progress information while parsing.
  279.      *
  280.      * defaults to false.  Set to true for cron jobs or other situations where no visual output is necessary
  281.      * @var bool 
  282.      */
  283.     var $quietmode;
  284.  
  285.     /**
  286.      * directory that output is sent to. -t command-line sets this.
  287.      * @tutorial phpDocumentor.howto.pkg#using.command-line.target
  288.      */
  289.     var $targetDir = '';
  290.  
  291.     /**
  292.      * Directory that the template is in, relative to phpDocumentor root directory
  293.      * @var string 
  294.      */
  295.     var $templateDir = '';
  296.  
  297.     /**
  298.      * Directory that the smarty templates are in
  299.      * @var string 
  300.      */
  301.     var $smarty_dir = '';
  302.  
  303.     /**
  304.      * Name of the template, from last part of -o
  305.      * @tutorial phpDocumentor.howto.pkg#using.command-line.output
  306.      * @var string 
  307.      */
  308.     var $templateName = '';
  309.  
  310.     /**
  311.      * full path of the current file being converted
  312.      */
  313.     var $curfile;
  314.  
  315.     /**
  316.      * All class information, organized by path, and by package
  317.      * @var Classes 
  318.      */
  319.     var $classes;
  320.  
  321.     /**
  322.      * Flag used to help converters determine whether to do special source highlighting
  323.      * @var boolean 
  324.      */
  325.     var $highlightingSource = false;
  326.  
  327.     /**
  328.      * Hierarchy of packages
  329.      *
  330.      * Every package that contains classes may have parent or child classes
  331.      * in other packages.  In other words, this code is legal:
  332.      *
  333.      * <code>
  334.      * /**
  335.      *  * @package one
  336.      *  * /
  337.      * class one {}
  338.      *
  339.      * /**
  340.      *  * @package two
  341.      *  * /
  342.      * class two extends one {}
  343.      * </code>
  344.      *
  345.      * In this case, package one is a parent of package two
  346.      * @var array 
  347.      * @see phpDocumentor_IntermediateParser::$package_parents
  348.      */
  349.     var $package_parents;
  350.  
  351.     /**
  352.      * Packages associated with categories
  353.      *
  354.      * Used by the XML:DocBook/peardoc2 converter, and available to others, to
  355.      * group many packages into categories
  356.      * @see phpDocumentor_IntermediateParser::$packagecategories
  357.      * @var array 
  358.      */
  359.     var $packagecategories;
  360.  
  361.     /**
  362.      * All packages encountered in parsing
  363.      * @var array 
  364.      * @see phpDocumentor_IntermediateParser::$all_packages
  365.      */
  366.     var $all_packages;
  367.  
  368.     /**
  369.      * A list of files that have had source code generated
  370.      * @var array 
  371.      */
  372.     var $sourcePaths = array();
  373.  
  374.     /**
  375.      * Controls which of the one-element-only indexes are generated.
  376.      *
  377.      * Generation of these indexes for large packages is time-consuming.  This is an optimization feature.  An
  378.      * example of how to use this is in {@link HTMLframesConverter::$leftindex}, and in {@link HTMLframesConverter::formatLeftIndex()}.
  379.      * These indexes are intended for use as navigational aids through documentation, but can be used for anything by converters.
  380.      * @see $class_elements, $page_elements, $function_elements, $define_elements, $global_elements
  381.      * @see formatLeftIndex()
  382.      * @var array 
  383.      */
  384.     var $leftindex = array('classes' => true'pages' => true'functions' => true'defines' => true'globals' => true);
  385.  
  386.     /** @access private */
  387.     var $killclass false;
  388.     /**
  389.      * @var string 
  390.      * @see phpDocumentor_IntermediateParser::$title
  391.      */
  392.     var $title = 'Generated Documentation';
  393.  
  394.     /**
  395.      * Options for each template, parsed from the options.ini file in the template base directory
  396.      * @tutorial phpDocumentor/tutorials.pkg#conversion.ppage
  397.      * @var array 
  398.      */
  399.     var $template_options;
  400.  
  401.     /**
  402.      * Tutorials and Extended Documentation parsed from a tutorials/package[/subpackage] directory
  403.      * @tutorial tutorials.pkg
  404.      * @access private
  405.      */
  406.     var $tutorials array();
  407.  
  408.     /**
  409.      * tree-format structure of tutorials and their child tutorials, if any
  410.      * @var array 
  411.      * @access private
  412.      */
  413.     var $tutorial_tree false;
  414.  
  415.     /**
  416.      * list of tutorials that have already been processed. Used by @link _setupTutorialTree()
  417.      * @var array 
  418.      * @access private
  419.      */
  420.     var $processed_tutorials;
  421.  
  422.     /**
  423.      * List of all @todo tags and a link to the element with the @todo
  424.      *
  425.      * Format: array(package => array(link to element, array(todo {@link parserTag},...)),...)
  426.      * @tutorial tags.todo.pkg
  427.      * @var array 
  428.      */
  429.     var $todoList = array();
  430.  
  431.     /**
  432.      * Directory where compiled templates go - will be deleted on exit
  433.      *
  434.      * @var string 
  435.      * @access private
  436.      */
  437.      var $_compiledDir array();
  438.  
  439.     /**
  440.      * Initialize Converter data structures
  441.      * @param array {@link $all_packages} value
  442.      * @param array {@link $package_parents} value
  443.      * @param Classes {@link $classes} value
  444.      * @param ProceduralPages {@link $proceduralpages} value
  445.      * @param array {@link $package_output} value
  446.      * @param boolean {@link $parseprivate} value
  447.      * @param boolean {@link $quietmode} value
  448.      * @param string {@link $targetDir} value
  449.      * @param string {@link $templateDir} value
  450.      * @param string (@link $title} value
  451.      */
  452.     function Converter(&$allp&$packp&$classes&$procpages$po$pp$qm$targetDir$template$title)
  453.     {
  454.         $this->all_packages = $allp;
  455.         $this->package_parents = $packp;
  456.         $this->package = $GLOBALS['phpDocumentor_DefaultPackageName'];
  457.         $this->proceduralpages &$procpages;
  458.         $this->package_output = $po;
  459.         if (is_array($po))
  460.         {
  461.             $a $po[0];
  462.             $this->all_packages = array_flip($po);
  463.             $this->all_packages[$a1;
  464.         }
  465.         $this->parseprivate = $pp;
  466.         $this->quietmode = $qm;
  467.         $this->classes = &$classes;
  468.         $this->roots $classes->getRoots($this->processSpecialRoots);
  469.         $this->title = $title;
  470.         $this->setTemplateDir($template);
  471.         $this->setTargetdir($targetDir);
  472.     }
  473.  
  474.     /**
  475.      * Called by IntermediateParser after creation
  476.      * @access private
  477.      */
  478.     function setTutorials($tutorials)
  479.     {
  480.         $this->tutorials $tutorials;
  481.     }
  482.  
  483.     /**
  484.      * @param pkg|cls|procthe tutorial type to search for
  485.      * @param tutorial name
  486.      * @param string package name
  487.      * @param string subpackage name, if any
  488.      * @return false|parserTutorialif the tutorial exists, return it
  489.      */
  490.     function hasTutorial($type$name$package$subpackage '')
  491.     {
  492.         if (isset($this->tutorials[$package][$subpackage][$type][$name '.' $type]))
  493.             return $this->tutorials[$package][$subpackage][$type][$name '.' $type];
  494.         return false;
  495.     }
  496.  
  497.     /**
  498.      * Called by {@link walk()} while converting, when the last class element
  499.      * has been parsed.
  500.      *
  501.      * A Converter can use this method in any way it pleases. HTMLframesConverter
  502.      * uses it to complete the template for the class and to output its
  503.      * documentation
  504.      * @see HTMLframesConverter::endClass()
  505.      * @abstract
  506.      */
  507.     function endClass()
  508.     {
  509.     }
  510.  
  511.     /**
  512.     * Called by {@link walk()} while converting, when the last procedural page
  513.     * element has been parsed.
  514.     *
  515.     * A Converter can use this method in any way it pleases. HTMLframesConverter
  516.     * uses it to complete the template for the procedural page and to output its
  517.     * documentation
  518.     * @see HTMLframesConverter::endClass()
  519.     * @abstract
  520.     */
  521.     function endPage()
  522.     {
  523.     }
  524.  
  525.     /**
  526.     * Called by {@link walk()} while converting.
  527.     *
  528.     * This method is intended to be the place that {@link $pkg_elements} is
  529.     * formatted for output.
  530.     * @see HTMLframesConverter::formatPkgIndex()
  531.     * @abstract
  532.     */
  533.     function formatPkgIndex()
  534.     {
  535.     }
  536.  
  537.     /**
  538.     * Called by {@link walk()} while converting.
  539.     *
  540.     * This method is intended to be the place that {@link $elements} is
  541.     * formatted for output.
  542.     * @see HTMLframesConverter::formatIndex()
  543.     * @abstract
  544.     */
  545.     function formatIndex()
  546.     {
  547.     }
  548.  
  549.     /**
  550.     * Called by {@link walk()} while converting.
  551.     *
  552.     * This method is intended to be the place that any of
  553.     * {@link $class_elements, $function_elements, $page_elements},
  554.     * {@link $define_elements}, and {@link $global_elements} is formatted for
  555.     * output, depending on the value of {@link $leftindex}
  556.     * @see HTMLframesConverter::formatLeftIndex()
  557.     * @abstract
  558.     */
  559.     function formatLeftIndex()
  560.     {
  561.     }
  562.  
  563.     /**
  564.      * Called by {@link parserSourceInlineTag::stringConvert()} to allow
  565.      * converters to format the source code the way they'd like.
  566.      *
  567.      * default returns it unchanged (html with xhtml tags)
  568.      * @param string output from highlight_string() - use this function to
  569.      *  reformat the returned data for Converter-specific output
  570.      * @return string 
  571.      * @deprecated in favor of tokenizer-based highlighting.  This will be
  572.      *              removed for 2.0
  573.      */
  574.     function unmangle($sourcecode)
  575.     {
  576.         return $sourcecode;
  577.     }
  578.  
  579.     /**
  580.      * Initialize highlight caching
  581.      */
  582.     function startHighlight()
  583.     {
  584.         $this->_highlightCache array(falsefalse);
  585.         $this->_appendHighlight '';
  586.     }
  587.  
  588.     function getHighlightState()
  589.     {
  590.         return $this->_highlightCache;
  591.     }
  592.  
  593.     function _setHighlightCache($type$token)
  594.     {
  595.         $test ($this->_highlightCache[0=== $type && $this->_highlightCache[1== $token);
  596.         if (!$test{
  597.             $this->_appendHighlight $this->flushHighlightCache();
  598.         else {
  599.             $this->_appendHighlight '';
  600.         }
  601.         $this->_highlightCache array($type$token);
  602.         return $test;
  603.     }
  604.  
  605.     /**
  606.      * Return the close text for the current token
  607.      * @return string 
  608.      */
  609.     function flushHighlightCache()
  610.     {
  611.         $hc $this->_highlightCache;
  612.         $this->_highlightCache array(falsefalse);
  613.         if ($hc[0]{
  614.             if (!isset($this->template_options[$hc[0]]['/'.$hc[1]])) {
  615.                 return '';
  616.             }
  617.             return $this->template_options[$hc[0]]['/'.$hc[1]];
  618.         }
  619.         return '';
  620.     }
  621.  
  622.     /**
  623.      * Used to allow converters to format the source code the way they'd like.
  624.      *
  625.      * default returns it unchanged.  Mainly used by the {@link HighlightParser}
  626.      * {@internal 
  627.      * The method takes information from options.ini, the template options
  628.      * file, specifically the [highlightSourceTokens] and [highlightSource]
  629.      * sections, and uses them to enclose tokens.
  630.      *
  631.      * {@source } }
  632.      * @param integer token value from {@link PHP_MANUAL#tokenizer tokenizer constants}
  633.      * @param string contents of token
  634.      * @param boolean whether the contents are preformatted or need modification
  635.      * @return string 
  636.      */
  637.     function highlightSource($token$word$preformatted false)
  638.     {
    <