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

Source for file XMLDocBookpeardoc2Converter.inc

Documentation is available at XMLDocBookpeardoc2Converter.inc

  1. <?php
  2. /**
  3.  * Outputs documentation in XML DocBook format, in the version expected by
  4.  * pear.php.net's documentation team
  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 XMLDocBook
  31.  * @author     Greg 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: XMLDocBookpeardoc2Converter.inc,v 1.8 2007/04/24 21:32:15 ashnazg Exp $
  35.  * @filesource
  36.  * @link       http://www.phpdoc.org
  37.  * @link       http://pear.php.net/PhpDocumentor
  38.  * @since      1.2
  39.  */
  40. /**
  41.  * XML DocBook converter.
  42.  * This Converter takes output from the {@link Parser} and converts it to DocBook
  43.  * output for PEAR documentation.
  44.  *
  45.  * This Converter differs from the parent DocBook Converter in that it does not
  46.  * recognize the possibility of procedural pages or of functions!  All functions
  47.  * must be defined as static methods for namespace purposes.  In addition, all
  48.  * constants and global variables for a package are grouped together as per
  49.  * peardoc2 requirements.  Include statements are not documented.  If you want
  50.  * to document a normal project, don't use the peardoc2 converter, use the
  51.  * DocBook converter.
  52.  * @package Converters
  53.  * @subpackage XMLDocBook
  54.  * @author Greg Beaver <cellog@php.net>
  55.  * @since 1.2
  56.  * @version $Id: XMLDocBookpeardoc2Converter.inc,v 1.8 2007/04/24 21:32:15 ashnazg Exp $
  57.  */
  58. {
  59.     /**
  60.      * This converter knows about the new root tree processing
  61.      * In order to fix PEAR Bug #6389
  62.      * @var boolean 
  63.      */
  64.     var $processSpecialRoots = true;
  65.     /**
  66.      * XMLDocBookConverter wants elements sorted by type as well as alphabetically
  67.      * @see Converter::$sort_page_contents_by_type
  68.      * @var boolean 
  69.      */
  70.     var $sort_page_contents_by_type = true;
  71.     /** @var string */
  72.     var $outputformat = 'XML';
  73.     /** @var string */
  74.     var $name = 'DocBook/peardoc2';
  75.     /**
  76.      * indexes of elements by package that need to be generated
  77.      * @var array 
  78.      */
  79.     var $leftindex = array('classes' => true'pages' => false'functions' => false'defines' => true'globals' => true);
  80.     /**
  81.      * whether a @see is going to be in the {@link $base_dir}, or in a package/subpackage subdirectory of $base_dir
  82.      * @var boolean 
  83.      */
  84.     var $local = true;
  85.     
  86.     /**
  87.      * name of current page being converted
  88.      * @var string 
  89.      */
  90.     var $page;
  91.     
  92.     /**
  93.      * path of current page being converted
  94.      * @var string 
  95.      */
  96.     var $path;
  97.     
  98.     /**
  99.      * name of current class being converted
  100.      * @var string 
  101.      */
  102.     var $class;
  103.     
  104.     /**
  105.      * template for the procedural page currently being processed
  106.      * @var Template 
  107.      */
  108.     var $page_data;
  109.     
  110.     /**
  111.      * output directory for the current procedural page being processed
  112.      * @var string 
  113.      */
  114.     var $page_dir;
  115.  
  116.     /**
  117.      * Constants, used for constants.tpl
  118.      * @var array 
  119.      */
  120.     var $_peardoc2_constants = false;
  121.     
  122.     /**
  123.      * Global Variables, used for globals.tpl
  124.      * @var array 
  125.      */
  126.     var $_peardoc2_globals = false;
  127.     
  128.     /**
  129.      * target directory passed on the command-line.
  130.      * {@link $targetDir} is malleable, always adding package/ and package/subpackage/ subdirectories onto it.
  131.      * @var string 
  132.      */
  133.     var $base_dir;
  134.     
  135.     /**
  136.      * output directory for the current class being processed
  137.      * @var string 
  138.      */
  139.     var $class_dir;
  140.     
  141.     /**
  142.      * template for the class currently being processed
  143.      * @var Template 
  144.      */
  145.     var $class_data;
  146.     
  147.     /**
  148.      * array of converted package page names.
  149.      * Used to link to the package page in the left index
  150.      * @var array Format: array(package => 1)
  151.      */
  152.     var $package_pages = array();
  153.     
  154.     /**
  155.      * Contents of the packagename.xml file are stored in this template variable
  156.      * @var Smarty 
  157.      */
  158.     var $packagexml;
  159.     /**
  160.      * controls formatting of parser informative output
  161.      * 
  162.      * Converter prints:
  163.      * "Converting /path/to/file.php... Procedural Page Elements... Classes..."
  164.      * Since HTMLdefaultConverter outputs files while converting, it needs to send a \n to start a new line.  However, if there
  165.      * is more than one class, output is messy, with multiple \n's just between class file output.  This variable prevents that
  166.      * and is purely cosmetic
  167.      * @var boolean 
  168.      */
  169.     var $juststarted = false;
  170.     
  171.     /**
  172.      * contains all of the template procedural page element loop data needed for the current template
  173.      * @var array 
  174.      */
  175.     var $current;
  176.     
  177.     /**
  178.      * contains all of the template class element loop data needed for the current template
  179.      * @var array 
  180.      */
  181.     var $currentclass;
  182.     
  183.     /** 
  184.      * Pass elements by package, simplifies generation of package.xml/category.xml
  185.      */
  186.     var $sort_absolutely_everything = true;
  187.     /**
  188.      * template options.  Currently only 1 recognized option usepear
  189.      *
  190.      * usepear tells the getLink() function to return a package link to PEAR and PEAR_ERROR if possible, and to link directly
  191.      * to the fully-delimited link package#class.method or package#file.method in PEAR style, if possible, even if the
  192.      * package is not parsed.  This will allow parsing of separate PEAR packages without parsing the entire thing at once!
  193.      * @var array 
  194.      */
  195.     var $template_options = array('usepear' => false);
  196.     
  197.     var $function_data = array();
  198.     var $method_data = array();
  199.     var $_write_constants_xml = array();
  200.     var $_write_globals_xml = array();
  201.     var $sourceloc = '';
  202.     /**
  203.      * peardoc2 Category
  204.      * @var string 
  205.      */
  206.     var $category;
  207.     /**
  208.      * Used to re-format output so that it's easy for translators to handle
  209.      * 
  210.      * @var XML_Beautifier|false
  211.      * @access private
  212.      */
  213.     var $_beautifier false;
  214.  
  215.     /**
  216.      * sets {@link $base_dir} to $targetDir
  217.      * @see Converter()
  218.      */
  219.     function XMLDocBookpeardoc2Converter(&$allp&$packp&$classes&$procpages$po$pp$qm$targetDir$templateDir$title)
  220.     {
  221.         if (!class_exists('XML_Beautifier')) {
  222.             @include_once 'XML/Beautifier.php';
  223.         }
  224.         Converter::Converter($allp$packp$classes$procpages,$po$pp$qm$targetDir$templateDir$title);
  225.         if (class_exists('XML_Beautifier')) {
  226.             require_once 'phpDocumentor/Converters/XML/DocBook/peardoc2/Beautifier.php';
  227.             $this->_beautifier new phpDocumentor_peardoc2_XML_Beautifier;
  228.             $this->_beautifier->setOption('indent'' ');
  229.         }
  230.         $this->base_dir = $targetDir;
  231.     }
  232.     
  233.     /**
  234.      * do that stuff in $template_options
  235.      */
  236.     function &getLink($expr$package false$packages false)
  237.     {
  238.         return Converter::getLink($expr$package$packages);
  239.     }
  240.     
  241.     function unmangle($s,$sourcecode)
  242.     {
  243.         return '<programlisting role="php"><![CDATA[
  244. '.$sourcecode.']]></programlisting>';
  245.     }
  246.  
  247.     /**
  248.      * Writes a file to target dir, beautify any .xml files first
  249.      * @param string filename
  250.      * @param string file contents
  251.      * @param boolean true if the data is binary and not text
  252.      */
  253.     function writeFile($file,$data,$binary false)
  254.     {
  255.         if ($this->_beautifier && substr($file-4== '.xml'{
  256.             $ret $this->_beautifier->formatString($data);
  257.             if (PEAR::isError($ret)) {
  258.                 addWarning(PDERROR_BEAUTIFYING_FAILED$ret->getMessage());
  259.                 $ret $data;
  260.             }
  261.             $data $ret;
  262.         }
  263.         return parent::writeFile($file$data$binary);
  264.     }
  265.     
  266.     /**
  267.      * Used to convert the {@}example} inline tag in a docblock.
  268.      * 
  269.      * By default, this just wraps ProgramExample
  270.      * @see XMLDocBookpeardoc2Converter::exampleProgramExample
  271.      * @param string 
  272.      * @param boolean true if this is to highlight a tutorial <programlisting>
  273.      * @return string 
  274.      */
  275.     function exampleProgramExample($example$tutorial false$inlinesourceparse null/*false*/,
  276.                             $class null/*false*/$linenum null/*false*/$filesourcepath null/*false*/)
  277.     {
  278.         return '<example><title>Example</title><programlisting role="php"><![CDATA[' .
  279.          $example ']]></programlisting></example>';
  280.         $this->ProgramExample($example$tutorial$inlinesourceparse$class$linenum$filesourcepath)
  281.         . '</example>';
  282.     }
  283.     
  284.     function writeExample($title$path$source)
  285.     {
  286.         $this->_save_example array($title$source);
  287.     }
  288.     
  289.     function getExampleLink($unused$title)
  290.     {
  291.         $source $this->_save_example[1];
  292.         return '<para><example><title>' $title '</title>' $source '</example></para>';
  293.     }
  294.     
  295.     function type_adjust($typename)
  296.     {
  297.         if (isset($this->template_options['typechanging'][trim($typename)]))
  298.         return $this->template_options['typechanging'][trim($typename)];
  299.         $a $this->getLink($typename);
  300.         if (is_object($a))
  301.         {
  302.             if (phpDocumentor_get_class($a== 'classlink')
  303.             return '<classname>'.$typename.'</classname>';
  304.             if (phpDocumentor_get_class($a== 'functionlink' || phpDocumentor_get_class($a== 'methodlink')
  305.             return '<function>'.$typename.'</function>';
  306.             if (phpDocumentor_get_class($a== 'definelink')
  307.             return '<constant>'.$typename.'</constant>';
  308.             if (phpDocumentor_get_class($a== 'varlink')
  309.             return '<varname>'.$typename.'</varname>';
  310.         }
  311.         return $typename;
  312.     }
  313.     
  314.     /**
  315.      * Writes out the template file of {@link $class_data} and unsets the template to save memory
  316.      * @see registerCurrentClass()
  317.      * @see parent::endClass()
  318.      * @todo move class summary into an array to be written out at the end
  319.      *        of parsing each package
  320.      */
  321.     function endClass()
  322.     {
  323.         $a '../';
  324.         if (!empty($this->subpackage)) $a .= '../';
  325.         if ($this->juststarted)
  326.         {
  327.             $this->juststarted = false;
  328.             phpDocumentor_out("\n");
  329.             flush();
  330.         }
  331.         foreach($this->method_data as $func)
  332.         {
  333.             $func[0]->assign("phpdocversion",PHPDOCUMENTOR_VER);
  334.             $func[0]->assign("phpdocwebsite",PHPDOCUMENTOR_WEBSITE);
  335.             $this->setTargetDir($this->base_dir . PATH_DELIMITER strtolower($this->categoryPATH_DELIMITER strtolower($this->class_dir . PATH_DELIMITER str_replace(array('_','.'),array('-','--'),$this->class)));
  336.             $this->writefile(strtolower($func[1)'.xml','<!-- $' "Revision$ -->\n" $func[0]->fetch('method.tpl'));
  337.         }
  338.         // code below is in packagename.xml handling, see Output()
  339. /*        $this->setTargetDir($this->base_dir . PATH_DELIMITER . strtolower($this->category) . PATH_DELIMITER . strtolower($this->class_dir));
  340.         $this->writefile(str_replace(array('_','.'),array('-','--'),strtolower($this->class)) . '.xml',$this->class_data->fetch('class.tpl'));*/
  341.         unset($this->class_data);
  342.     }
  343.     
  344.     function addSummaryToPackageXml($template_output)
  345.     {
  346.         $this->packagexml->append('ids',$template_output);
  347.     }
  348.     
  349.     /**
  350.      * @param parserClass|false$element is false if this is the end of all conversion
  351.      */
  352.     function flushPackageXml($element)
  353.     {
  354.         if (isset($this->packagexml))
  355.         {
  356.             if (!$element || $element->docblock->package != $this->package// finished with package
  357.             {
  358.                 if (isset($this->_write_constants_xml[$this->category][$this->package]&&
  359.                     $this->_write_constants_xml[$this->category][$this->package])
  360.                 {
  361.                     $this->packagexml->append('ids',
  362.                         '&package.' .
  363.                          strtolower($this->category.'.' .
  364.                          str_replace(array('_','.'),array('-','--'),$this->package).'.constants;'));
  365.                     $this->_write_constants_xml[$this->category][$this->packagefalse;
  366.                 }
  367.                 if (isset($this->_write_globals_xml[$this->category][$this->package]&&
  368.                     $this->_write_globals_xml[$this->category][$this->package])
  369.                 {
  370.                     $this->packagexml->append('ids',
  371.                             '&package.'.strtolower($this->category.'.' 
  372.                              str_replace(array('_','.'),array('-','--'),$this->package).'.globals;'));
  373.                     $this->_write_globals_xml[$this->category][$this->packagefalse;
  374.                 }
  375.                 $this->setTargetDir($this->base_dir . PATH_DELIMITER strtolower($this->category));
  376.                 $this->writefile(str_replace('_','-',strtolower($this->package)).'.xml',
  377.                     '<!-- $' "Revision$ -->\n" $this->packagexml->fetch('package.tpl'));
  378.                 $this->packagexml->clear_all_assign();
  379.                 if ($element{
  380.                     $this->packagexml->assign('package',$element->docblock->package);
  381.                     $this->packagexml->assign('ids',array());
  382.                     $this->packagexml->assign('id',$this->getId($elementtrue));
  383.                 }
  384.             }
  385.         else
  386.         {
  387.             $this->packagexml = $this->newSmarty();
  388.             $this->packagexml->assign('package',$element->docblock->package);
  389.             $this->packagexml->assign('ids',array());
  390.             $this->packagexml->assign('id',$this->getId($elementtrue));
  391.         }
  392.     }
  393.     
  394.     /**
  395.      * @param string 
  396.      * @param string 
  397.      * @return string <ulink url="'.$link.'">'.$text.'</ulink>
  398.      */
  399.     function returnLink($link,$text)
  400.     {
  401.         return '<ulink url="'.$link.'">'.$text.'</ulink>';
  402.     }
  403.     
  404.     function makeLeft()
  405.     {
  406.     }
  407.     
  408.     /**
  409.      * Does nothing
  410.      */
  411.     function formatPkgIndex()
  412.     {
  413.     }
  414.     
  415.     /**
  416.      * Does nothing
  417.      */
  418.     function formatIndex()
  419.     {
  420.     }
  421.  
  422.     /**
  423.      * Does nothing
  424.      */
  425.     function writeNewPPage($key)
  426.     {
  427.     }
  428.     
  429.     /**
  430.      * Does nothing
  431.      */
  432.     function writeSource()
  433.     {
  434.     }
  435.     
  436.     /**
  437.      * Creates package/lang/categoryname/packagename.xml for each package
  438.      */
  439.     function formatLeftIndex()
  440.     {
  441.         $this->makeLeft();
  442.     }
  443.     
  444.     /**
  445.      * This function takes an {@link abstractLink} descendant and returns an html link
  446.      *
  447.      * @param abstractLink a descendant of abstractlink should be passed, and never text
  448.      * @param string text to display in the link
  449.      * @param boolean this parameter is not used, and is deprecated
  450.      * @param boolean determines whether the returned text is enclosed in an <link> tag
  451.      */
  452.     function returnSee(&$element$eltext false$local true$with_a true)
  453.     {
  454.         if (!$elementreturn false;
  455.         if (!$eltext)
  456.         {
  457.             $eltext '';
  458.             switch($element->type)
  459.             {
  460.                 case 'tutorial' :
  461.                 $eltext $element->title;
  462.                 break;
  463.                 case 'class' :
  464.                 $eltext '<classname>'.$element->name.'</classname>';
  465.                 break;
  466.                 case 'method' :
  467.                 $eltext .= '<function>';
  468.                 case 'var' :
  469.                 if ($element->type == 'var'$eltext .= '<varname>';
  470.                 $eltext .= $element->class.'::';
  471.                 case 'page' :
  472.                 case 'define' :
  473.                 if ($element->type == 'define')
  474.                 $eltext .= '<constant>';
  475.                 case 'function' :
  476.                 if ($element->type == 'function')
  477.                 $eltext .= '<function>';
  478.                 case 'global' :
  479.                 default :
  480.                 $eltext .= $element->name;
  481.                 if ($element->type == 'function' || $element->type == 'method'$eltext .= '</function>';
  482.                 if ($element->type == 'var'$eltext .= '</varname>';
  483.                 if ($element->type == 'define'$eltext .= '</constant>';
  484.                 break;
  485.             }
  486.         elseif (!is_object($element)) {
  487.             return false;
  488.         elseif ($element->type == 'method')
  489.         {
  490.             $eltext str_replace($element->name '()'$element->name$eltext);
  491.         }
  492.  
  493.         if ($element->type == 'page' || $element->type == 'function' || $element->type == 'var')
  494.         // we ignore all procedural pages, instead, constant, function and
  495.    &n