home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / DocBook.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  23.1 KB  |  1,134 lines

  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | PEAR :: XML :: Transformer :: DocBook Namespace Handler                   |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
  7. // |                         Kristian K÷hntopp <kris@koehntopp.de>.            |
  8. // +---------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,           |
  10. // | that is available at http://www.php.net/license/3_0.txt.                  |
  11. // | If you did not receive a copy of the PHP license and are unable to        |
  12. // | obtain it through the world-wide-web, please send a note to               |
  13. // | license@php.net so we can mail you a copy immediately.                    |
  14. // +---------------------------------------------------------------------------+
  15. //
  16. // $Id: DocBook.php,v 1.24 2004/01/01 10:31:55 sebastian Exp $
  17. //
  18.  
  19. require_once 'XML/Transformer/Namespace.php';
  20. require_once 'XML/Util.php';
  21.  
  22. /**
  23. * DocBook Namespace Handler.
  24. *
  25. * This namespace handler provides transformations to render a subset of
  26. * the popular DocBook/XML markup (http://www.docbook.org/) into HTML.
  27. *
  28. * Transformations for the following DocBook tags are implemented:
  29. *
  30. *   * <artheader>
  31. *
  32. *   * <article>
  33. *
  34. *   * <author>
  35. *
  36. *   * <book>
  37. *
  38. *   * <chapter>
  39. *
  40. *   * <emphasis>
  41. *
  42. *   * <example>
  43. *
  44. *   * <figure>
  45. *
  46. *   * <filename>
  47. *
  48. *   * <firstname>
  49. *
  50. *   * <function>
  51. *
  52. *   * <graphic>
  53. *
  54. *   * <itemizedlist>
  55. *
  56. *   * <listitem>
  57. *
  58. *   * <orderedlist>
  59. *
  60. *   * <para>
  61. *
  62. *   * <programlisting>
  63. *
  64. *   * <section>
  65. *
  66. *   * <surname>
  67. *
  68. *   * <title>
  69. *
  70. *   * <ulink>
  71. *
  72. *   * <xref>
  73. *
  74. * Example
  75. *
  76. *   <?php
  77. *   require_once 'XML/Transformer/Driver/OutputBuffer.php';
  78. *   $t = new XML_Transformer_Driver_OutputBuffer(
  79. *     array(
  80. *       'autoload' => 'DocBook'
  81. *     )
  82. *   );
  83. *   ?>
  84. *   <article>
  85. *     <artheader>
  86. *       <title>
  87. *         An Article
  88. *       </title>
  89. *       <author>
  90. *         <firstname>
  91. *           Sebastian
  92. *         </firstname>
  93. *         <surname>
  94. *           Bergmann
  95. *         </surname>
  96. *       </author>
  97. *     </artheader>
  98. *
  99. *     <section id="foo">
  100. *       <title>
  101. *         Section One
  102. *       </title>
  103. *     </section>
  104. *
  105. *     <section id="bar">
  106. *       <title>
  107. *         Section Two
  108. *       </title>
  109. *
  110. *       <para>
  111. *         <xref linkend="foo" />
  112. *       </para>
  113. *     </section>
  114. *   </article>
  115. *
  116. * Output
  117. *
  118. *   <html>
  119. *     <head>
  120. *       <title>
  121. *         Sebastian Bergmann: An Article
  122. *       </title>
  123. *     </head>
  124. *     <body>
  125. *       <h1 class="title">
  126. *         Sebastian Bergmann: An Article
  127. *       </h1>
  128. *       <div class="section">
  129. *         <a id="foo"></a>
  130. *         <h2 class="title">
  131. *           1. Section One
  132. *         </h2>
  133. *       </div>
  134. *       <div class="section">
  135. *         <a id="bar"></a>
  136. *         <h2 class="title">
  137. *           2. Section Two
  138. *         </h2>
  139. *         <p>
  140. *           <a href="#foo">
  141. *             1. Section One
  142. *           </a>
  143. *         </p>
  144. *       </div>
  145. *     </body>
  146. *   </html>
  147. *
  148. * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  149. * @author  Kristian K÷hntopp <kris@koehntopp.de>
  150. * @version $Revision: 1.24 $
  151. * @access  public
  152. */
  153. class XML_Transformer_Namespace_DocBook extends XML_Transformer_Namespace {
  154.     // {{{ Members
  155.  
  156.     /**
  157.     * @var    string
  158.     * @access public
  159.     */
  160.     var $defaultNamespacePrefix = '&MAIN';
  161.  
  162.     /**
  163.     * @var    boolean
  164.     * @access public
  165.     */
  166.     var $secondPassRequired = true;
  167.  
  168.     /**
  169.     * @var    string
  170.     * @access private
  171.     */
  172.     var $_author = '';
  173.  
  174.     /**
  175.     * @var    array
  176.     * @access private
  177.     */
  178.     var $_context = array();
  179.  
  180.     /**
  181.     * @var    string
  182.     * @access private
  183.     */
  184.     var $_currentExampleNumber = '';
  185.  
  186.     /**
  187.     * @var    string
  188.     * @access private
  189.     */
  190.     var $_currentFigureNumber = '';
  191.  
  192.     /**
  193.     * @var    string
  194.     * @access private
  195.     */
  196.     var $_currentSectionNumber = '';
  197.  
  198.     /**
  199.     * @var    array
  200.     * @access private
  201.     */
  202.     var $_examples = array();
  203.  
  204.     /**
  205.     * @var    array
  206.     * @access private
  207.     */
  208.     var $_figures = array();
  209.  
  210.     /**
  211.     * @var    array
  212.     * @access private
  213.     */
  214.     var $_highlightColors = array(
  215.       'bg'      => '#ffffff',
  216.       'comment' => '#ba8370',
  217.       'default' => '#113d73',
  218.       'html'    => '#000000',
  219.       'keyword' => '#005500',
  220.       'string'  => '#550000'
  221.     );
  222.  
  223.     /**
  224.     * @var    array
  225.     * @access private
  226.     */
  227.     var $_ids = array();
  228.  
  229.     /**
  230.     * @var    boolean
  231.     * @access private
  232.     */
  233.     var $_roles = array();
  234.  
  235.     /**
  236.     * @var    array
  237.     * @access private
  238.     */
  239.     var $_secondPass = false;
  240.  
  241.     /**
  242.     * @var    array
  243.     * @access private
  244.     */
  245.     var $_sections = array();
  246.  
  247.     /**
  248.     * @var    string
  249.     * @access private
  250.     */
  251.     var $_title = '';
  252.  
  253.     /**
  254.     * @var    array
  255.     * @access private
  256.     */
  257.     var $_xref = '';
  258.  
  259.     // }}}
  260.     // {{{ function XML_Transformer_Namespace_DocBook($parameters = array())
  261.  
  262.     /**
  263.     * @param  array
  264.     * @access public
  265.     */
  266.     function XML_Transformer_Namespace_DocBook($parameters = array()) {
  267.         if (isset($parameters['highlightColors'])) {
  268.             $this->_highlightColors = $parameters['highlightColors'];
  269.         }
  270.  
  271.         foreach ($this->_highlightColors as $highlight => $color) {
  272.             ini_set('highlight.' . $highlight, $color);
  273.         }
  274.     }
  275.  
  276.     // }}}
  277.     // {{{ function start_artheader($attributes)
  278.  
  279.     /**
  280.     * @param  array
  281.     * @return string
  282.     * @access public
  283.     */
  284.     function start_artheader($attributes) {
  285.         if (!$this->_secondPass) {
  286.             return sprintf(
  287.               '<artheader%s>',
  288.               XML_Util::attributesToString($attributes)
  289.             );
  290.         }
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ function end_artheader($cdata)
  295.  
  296.     /**
  297.     * @param  string
  298.     * @return string
  299.     * @access public
  300.     */
  301.     function end_artheader($cdata) {
  302.         if (!$this->_secondPass) {
  303.             $cdata = $cdata . '</artheader>';
  304.  
  305.             return array(
  306.               $cdata,
  307.               false
  308.             );
  309.         }
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ function start_article($attributes)
  314.  
  315.     /**
  316.     * @param  array
  317.     * @return string
  318.     * @access public
  319.     */
  320.     function start_article($attributes) {
  321.         return $this->_startDocument('article', $attributes);
  322.     }
  323.  
  324.     // }}}
  325.     // {{{ function end_article($cdata)
  326.  
  327.     /**
  328.     * @param  string
  329.     * @return string
  330.     * @access public
  331.     */
  332.     function end_article($cdata) {
  333.         return $this->_endDocument('article', $cdata);
  334.     }
  335.  
  336.     // }}}
  337.     // {{{ function start_author($attributes)
  338.  
  339.     /**
  340.     * @param  array
  341.     * @return string
  342.     * @access public
  343.     */
  344.     function start_author($attributes) {}
  345.  
  346.     // }}}
  347.     // {{{ function end_author($cdata)
  348.  
  349.     /**
  350.     * @param  string
  351.     * @return string
  352.     * @access public
  353.     */
  354.     function end_author($cdata) {
  355.         $this->_author = trim(str_replace("\n", '', $cdata));
  356.     }
  357.  
  358.     // }}}
  359.     // {{{ function start_book($attributes)
  360.  
  361.     /**
  362.     * @param  array
  363.     * @return string
  364.     * @access public
  365.     */
  366.     function start_book($attributes) {
  367.         return $this->_startDocument('book', $attributes);
  368.     }
  369.  
  370.     // }}}
  371.     // {{{ function end_book($cdata)
  372.  
  373.     /**
  374.     * @param  string
  375.     * @return string
  376.     * @access public
  377.     */
  378.     function end_book($cdata) {
  379.         return $this->_endDocument('book', $cdata);
  380.     }
  381.  
  382.     // }}}
  383.     // {{{ function start_chapter($attributes)
  384.  
  385.     /**
  386.     * @param  array
  387.     * @return string
  388.     * @access public
  389.     */
  390.     function start_chapter($attributes) {
  391.         $id = $this->_startSection(
  392.           'chapter',
  393.           isset($attributes['id']) ? $attributes['id'] : ''
  394.         );
  395.  
  396.         return '<div class="chapter">' . $id;
  397.     }
  398.  
  399.     // }}}
  400.     // {{{ function end_chapter($cdata)
  401.  
  402.     /**
  403.     * @param  string
  404.     * @return string
  405.     * @access public
  406.     */
  407.     function end_chapter($cdata) {
  408.         $this->_endSection('chapter');
  409.  
  410.         return $cdata . '</div>';
  411.     }
  412.  
  413.     // }}}
  414.     // {{{ function start_emphasis($attributes)
  415.  
  416.     /**
  417.     * @param  array
  418.     * @return string
  419.     * @access public
  420.     */
  421.     function start_emphasis($attributes) {
  422.         $emphasisRole = isset($attributes['role']) ? $attributes['role'] : '';
  423.  
  424.         switch($emphasisRole) {
  425.             case 'bold':
  426.             case 'strong': {
  427.                 $this->_roles['emphasis'] = 'b';
  428.             }
  429.             break;
  430.  
  431.             default: {
  432.                 $this->_roles['emphasis'] = 'i';
  433.             }
  434.         }
  435.  
  436.         return '<' . $this->_roles['emphasis'] . '>';
  437.     }
  438.  
  439.     // }}}
  440.     // {{{ function end_emphasis($cdata)
  441.  
  442.     /**
  443.     * @param  string
  444.     * @return string
  445.     * @access public
  446.     */
  447.     function end_emphasis($cdata) {
  448.         $cdata = sprintf(
  449.           '%s</%s>',
  450.           $cdata,
  451.           $this->_roles['emphasis']
  452.         );
  453.  
  454.         $this->_roles['emphasis'] = '';
  455.  
  456.         return $cdata;
  457.     }
  458.  
  459.     // }}}
  460.     // {{{ function start_example($attributes)
  461.  
  462.     /**
  463.     * @param  array
  464.     * @return string
  465.     * @access public
  466.     */
  467.     function start_example($attributes) {
  468.         $id = $this->_startSection(
  469.           'example',
  470.           isset($attributes['id']) ? $attributes['id'] : ''
  471.         );
  472.  
  473.         return '<div class="example">' . $id;
  474.     }
  475.  
  476.     // }}}
  477.     // {{{ function end_example($cdata)
  478.  
  479.     /**
  480.     * @param  string
  481.     * @return string
  482.     * @access public
  483.     */
  484.     function end_example($cdata) {
  485.         $this->_endSection('example');
  486.  
  487.         return $cdata . '</div>';
  488.     }
  489.  
  490.     // }}}
  491.     // {{{ function start_figure($attributes)
  492.  
  493.     /**
  494.     * @param  array
  495.     * @return string
  496.     * @access public
  497.     */
  498.     function start_figure($attributes) {
  499.         $id = $this->_startSection(
  500.           'figure',
  501.           isset($attributes['id']) ? $attributes['id'] : ''
  502.         );
  503.  
  504.         return '<div class="figure">' . $id;
  505.     }
  506.  
  507.     // }}}
  508.     // {{{ function end_figure($cdata)
  509.  
  510.     /**
  511.     * @param  string
  512.     * @return string
  513.     * @access public
  514.     */
  515.     function end_figure($cdata) {
  516.         $this->_endSection('figure');
  517.  
  518.         return $cdata . '</div>';
  519.     }
  520.  
  521.     // }}}
  522.     // {{{ function start_filename($attributes)
  523.  
  524.     /**
  525.     * @param  array
  526.     * @return string
  527.     * @access public
  528.     */
  529.     function start_filename($attributes) {
  530.         return '<tt>';
  531.     }
  532.  
  533.     // }}}
  534.     // {{{ function end_filename($cdata)
  535.  
  536.     /**
  537.     * @param  string
  538.     * @return string
  539.     * @access public
  540.     */
  541.     function end_filename($cdata) {
  542.         return trim($cdata) . '</tt>';
  543.     }
  544.  
  545.     // }}}
  546.     // {{{ function start_firstname($attributes)
  547.  
  548.     /**
  549.     * @param  array
  550.     * @return string
  551.     * @access public
  552.     */
  553.     function start_firstname($attributes) {}
  554.  
  555.     // }}}
  556.     // {{{ function end_firstname($cdata)
  557.  
  558.     /**
  559.     * @param  string
  560.     * @return string
  561.     * @access public
  562.     */
  563.     function end_firstname($cdata) {
  564.         return trim($cdata);
  565.     }
  566.  
  567.     // }}}
  568.     // {{{ function start_function($attributes)
  569.  
  570.     /**
  571.     * @param  array
  572.     * @return string
  573.     * @access public
  574.     */
  575.     function start_function($attributes) {
  576.         return '<code><b>';
  577.     }
  578.  
  579.     // }}}
  580.     // {{{ function end_function($cdata)
  581.  
  582.     /**
  583.     * @param  string
  584.     * @return string
  585.     * @access public
  586.     */
  587.     function end_function($cdata) {
  588.         return array(
  589.           trim($cdata) . '</b></code>',
  590.           false
  591.         );
  592.     }
  593.  
  594.     // }}}
  595.     // {{{ function start_graphic($attributes)
  596.  
  597.     /**
  598.     * @param  array
  599.     * @return string
  600.     * @access public
  601.     */
  602.     function start_graphic($attributes) {
  603.         return sprintf(
  604.           '<img alt="%s" border="0" src="%s"%s%s/>',
  605.  
  606.           isset($attributes['srccredit']) ? $attributes['srccredit']                  : '',
  607.           isset($attributes['fileref'])   ? $attributes['fileref']                    : '',
  608.           isset($attributes['width'])     ? ' width="'  . $attributes['width']  . '"' : '',
  609.           isset($attributes['height'])    ? ' height="' . $attributes['height'] . '"' : ''
  610.         );
  611.     }
  612.  
  613.     // }}}
  614.     // {{{ function end_graphic($cdata)
  615.  
  616.     /**
  617.     * @param  string
  618.     * @return string
  619.     * @access public
  620.     */
  621.     function end_graphic($cdata) {
  622.         return $cdata;
  623.     }
  624.  
  625.     // }}}
  626.     // {{{ function start_itemizedlist($attributes)
  627.  
  628.     /**
  629.     * @param  array
  630.     * @return string
  631.     * @access public
  632.     */
  633.     function start_itemizedlist($attributes) {
  634.         return '<ul>';
  635.     }
  636.  
  637.     // }}}
  638.     // {{{ function end_itemizedlist($cdata)
  639.  
  640.     /**
  641.     * @param  string
  642.     * @return string
  643.     * @access public
  644.     */
  645.     function end_itemizedlist($cdata) {
  646.         return $cdata . '</ul>';
  647.     }
  648.  
  649.     // }}}
  650.     // {{{ function start_listitem($attributes)
  651.  
  652.     /**
  653.     * @param  array
  654.     * @return string
  655.     * @access public
  656.     */
  657.     function start_listitem($attributes) {
  658.         return '<li>';
  659.     }
  660.  
  661.     // }}}
  662.     // {{{ function end_listitem($cdata)
  663.  
  664.     /**
  665.     * @param  string
  666.     * @return string
  667.     * @access public
  668.     */
  669.     function end_listitem($cdata) {
  670.         return $cdata . '</li>';
  671.     }
  672.  
  673.     // }}}
  674.     // {{{ function start_orderedlist($attributes)
  675.  
  676.     /**
  677.     * @param  array
  678.     * @return string
  679.     * @access public
  680.     */
  681.     function start_orderedlist($attributes) {
  682.         return '<ol>';
  683.     }
  684.  
  685.     // }}}
  686.     // {{{ function end_orderedlist($cdata)
  687.  
  688.     /**
  689.     * @param  string
  690.     * @return string
  691.     * @access public
  692.     */
  693.     function end_orderedlist($cdata) {
  694.         return $cdata . '</ol>';
  695.     }
  696.  
  697.     // }}}
  698.     // {{{ function start_para($attributes)
  699.  
  700.     /**
  701.     * @param  array
  702.     * @return string
  703.     * @access public
  704.     */
  705.     function start_para($attributes) {
  706.         return '<p>';
  707.     }
  708.  
  709.     // }}}
  710.     // {{{ function end_para($cdata)
  711.  
  712.     /**
  713.     * @param  string
  714.     * @return string
  715.     * @access public
  716.     */
  717.     function end_para($cdata) {
  718.         return $cdata . '</p>';
  719.     }
  720.  
  721.     // }}}
  722.     // {{{ function start_programlisting($attributes)
  723.  
  724.     /**
  725.     * @param  array
  726.     * @return string
  727.     * @access public
  728.     */
  729.     function start_programlisting($attributes) {
  730.         $this->_roles['programlisting'] = isset($attributes['role']) ? $attributes['role'] : '';
  731.  
  732.         switch ($this->_roles['programlisting']) {
  733.             case 'php': {
  734.                 return '';
  735.             }
  736.             break;
  737.  
  738.             default: {
  739.                 return '<code>';
  740.             }
  741.         }
  742.     }
  743.  
  744.     // }}}
  745.     // {{{ function end_programlisting($cdata)
  746.  
  747.     /**
  748.     * @param  string
  749.     * @return mixed
  750.     * @access public
  751.     */
  752.     function end_programlisting($cdata) {
  753.         switch ($this->_roles['programlisting']) {
  754.             case 'php': {
  755.                 $cdata = array(
  756.                   str_replace(
  757.                     ' ',
  758.                     ' ',
  759.                     highlight_string($cdata, 1)
  760.                   ),
  761.                   false
  762.                 );
  763.             }
  764.             break;
  765.  
  766.             default: {
  767.                 $cdata = array(
  768.                   $cdata . '</code>',
  769.                   false
  770.                 );
  771.             }
  772.         }
  773.  
  774.         $this->_roles['programlisting'] = '';
  775.  
  776.         return $cdata;
  777.     }
  778.  
  779.     // }}}
  780.     // {{{ function start_section($attributes)
  781.  
  782.     /**
  783.     * @param  array
  784.     * @return string
  785.     * @access public
  786.     */
  787.     function start_section($attributes) {
  788.         $id = $this->_startSection(
  789.           'section',
  790.           isset($attributes['id']) ? $attributes['id'] : ''
  791.         );
  792.  
  793.         return '<div class="section">' . $id;
  794.     }
  795.  
  796.     // }}}
  797.     // {{{ function end_section($cdata)
  798.  
  799.     /**
  800.     * @param  string
  801.     * @return string
  802.     * @access public
  803.     */
  804.     function end_section($cdata) {
  805.         $this->_endSection('section');
  806.  
  807.         return $cdata . '</div>';
  808.     }
  809.  
  810.     // }}}
  811.     // {{{ function start_surname($attributes)
  812.  
  813.     /**
  814.     * @param  array
  815.     * @return string
  816.     * @access public
  817.     */
  818.     function start_surname($attributes) {}
  819.  
  820.     // }}}
  821.     // {{{ function end_surname($cdata)
  822.  
  823.     /**
  824.     * @param  string
  825.     * @return string
  826.     * @access public
  827.     */
  828.     function end_surname($cdata) {
  829.         return trim($cdata);
  830.     }
  831.  
  832.     // }}}
  833.     // {{{ function start_title($attributes)
  834.  
  835.     /**
  836.     * @param  array
  837.     * @return string
  838.     * @access public
  839.     */
  840.     function start_title($attributes) {
  841.         switch ($this->_context[sizeof($this->_context)-1]) {
  842.             case 'chapter':
  843.             case 'section': {
  844.                 return '<h2 class="title">' . $this->_currentSectionNumber . '. ';
  845.             }
  846.             break;
  847.  
  848.             case 'example': {
  849.                 return '<h3 class="title">Example ' . $this->_currentExampleNumber;
  850.             }
  851.             break;
  852.  
  853.             case 'figure': {
  854.                 return '<h3 class="title">Figure ' . $this->_currentFigureNumber;
  855.             }
  856.             break;
  857.         }
  858.     }
  859.  
  860.     // }}}
  861.     // {{{ function end_title($cdata)
  862.  
  863.     /**
  864.     * @param  string
  865.     * @return string
  866.     * @access public
  867.     */
  868.     function end_title($cdata) {
  869.         $cdata = trim($cdata);
  870.  
  871.         if (!empty($this->_ids[sizeof($this->_ids)-1])) {
  872.             $this->_xref[$this->_ids[sizeof($this->_ids)-1]] = strip_tags($cdata);
  873.         }
  874.  
  875.         switch ($this->_context[sizeof($this->_context)-1]) {
  876.             case 'article':
  877.             case 'book': {
  878.                 $this->_title = $cdata;
  879.             }
  880.             break;
  881.  
  882.             case 'chapter':
  883.             case 'section': {
  884.                 return $cdata . '</h2>';
  885.             }
  886.             break;
  887.  
  888.             case 'example':
  889.             case 'figure': {
  890.                 return $cdata . '</h3>';
  891.             }
  892.             break;
  893.  
  894.             default: {
  895.                 return $cdata;
  896.             }
  897.         }
  898.     }
  899.  
  900.     // }}}
  901.     // {{{ function start_ulink($attributes)
  902.  
  903.     /**
  904.     * @param  array
  905.     * @return string
  906.     * @access public
  907.     */
  908.     function start_ulink($attributes) {
  909.         return '<a href="' . $attributes['url'] . '">';
  910.     }
  911.  
  912.     // }}}
  913.     // {{{ function end_ulink($cdata)
  914.  
  915.     /**
  916.     * @param  string
  917.     * @return string
  918.     * @access public
  919.     */
  920.     function end_ulink($cdata) {
  921.         return $cdata . '</a>';
  922.     }
  923.  
  924.     // }}}
  925.     // {{{ function start_xref($attributes)
  926.  
  927.     /**
  928.     * @param  array
  929.     * @return string
  930.     * @access public
  931.     */
  932.     function start_xref($attributes) {
  933.         if ($this->_secondPass) {
  934.             return sprintf(
  935.               '<a href="#%s">%s</a>',
  936.  
  937.               isset($attributes['linkend'])               ? $attributes['linkend']               : '',
  938.               isset($this->_xref[$attributes['linkend']]) ? $this->_xref[$attributes['linkend']] : ''
  939.             );
  940.         } else {
  941.             return sprintf(
  942.               '<xref%s>',
  943.               XML_Util::attributesToString($attributes)
  944.             );
  945.         }
  946.     }
  947.  
  948.     // }}}
  949.     // {{{ function end_xref($cdata)
  950.  
  951.     /**
  952.     * @param  string
  953.     * @return string
  954.     * @access public
  955.     */
  956.     function end_xref($cdata) {
  957.         if (!$this->_secondPass) {
  958.             $cdata = $cdata . '</xref>';
  959.         }
  960.  
  961.         return array(
  962.           $cdata,
  963.           false
  964.         );
  965.     }
  966.  
  967.     // }}}
  968.     // {{{ function _startDocument($type, $attributes)
  969.  
  970.     /**
  971.     * @param  string
  972.     * @param  array
  973.     * @return string
  974.     * @access private
  975.     */
  976.     function _startDocument($type, $attributes) {
  977.         if (!$this->_secondPass) {
  978.             $id = $this->_startSection(
  979.               $type,
  980.               isset($attributes['id']) ? $attributes['id'] : ''
  981.             );
  982.  
  983.             return sprintf(
  984.               '<%s>%s',
  985.  
  986.               $type,
  987.               $id
  988.             );
  989.         } else {
  990.             return sprintf(
  991.               '<html><head><title>%s: %s</title><body><h1 class="title">%s: %s</h1>',
  992.  
  993.               $this->_author,
  994.               $this->_title,
  995.               $this->_author,
  996.               $this->_title
  997.             );
  998.         }
  999.     }
  1000.  
  1001.     // }}}
  1002.     // {{{ function _endDocument($type, $cdata)
  1003.  
  1004.     /**
  1005.     * @param  string
  1006.     * @param  string
  1007.     * @return string
  1008.     * @access private
  1009.     */
  1010.     function _endDocument($type, $cdata) {
  1011.         if (!$this->_secondPass) {
  1012.             $this->_endSection($type);
  1013.  
  1014.             $this->_secondPass = true;
  1015.  
  1016.             $cdata = sprintf(
  1017.               '%s</%s>',
  1018.  
  1019.               $cdata,
  1020.               $type
  1021.             );
  1022.         } else {
  1023.             $cdata = $cdata . '</body></html>';
  1024.         }
  1025.  
  1026.         return array(
  1027.           $cdata,
  1028.           false
  1029.         );
  1030.     }
  1031.  
  1032.     // }}}
  1033.     // {{{ function _startSection($type, $id)
  1034.  
  1035.     /**
  1036.     * @param  string
  1037.     * @return string
  1038.     * @access private
  1039.     */
  1040.     function _startSection($type, $id) {
  1041.         array_push($this->_context, $type);
  1042.         array_push($this->_ids,     $id);
  1043.  
  1044.         switch ($type) {
  1045.             case 'article':
  1046.             case 'book':
  1047.             case 'chapter':
  1048.             case 'section': {
  1049.                 $this->_currentSectionNumber = '';
  1050.  
  1051.                 if (!isset($this->_sections[$type]['open'])) {
  1052.                     $this->_sections[$type]['open'] = 1;
  1053.                 } else {
  1054.                     $this->_sections[$type]['open']++;
  1055.                 }
  1056.  
  1057.                 if (!isset($this->_sections[$type]['id'][$this->_sections[$type]['open']])) {
  1058.                     $this->_sections[$type]['id'][$this->_sections[$type]['open']] = 1;
  1059.                 } else {
  1060.                     $this->_sections[$type]['id'][$this->_sections[$type]['open']]++;
  1061.                 }
  1062.  
  1063.                 for ($i = 1; $i <= $this->_sections[$type]['open']; $i++) {
  1064.                     if (!empty($this->_currentSectionNumber)) {
  1065.                         $this->_currentSectionNumber .= '.';
  1066.                     }
  1067.  
  1068.                     $this->_currentSectionNumber .= $this->_sections[$type]['id'][$i];
  1069.                 }
  1070.             }
  1071.             break;
  1072.  
  1073.             case 'example': {
  1074.                 if (!isset($this->_examples[$this->_currentSectionNumber])) {
  1075.                     $this->_examples[$this->_currentSectionNumber] = 1;
  1076.                 } else {
  1077.                     $this->_examples[$this->_currentSectionNumber]++;
  1078.                 }
  1079.  
  1080.                 $this->_currentExampleNumber =
  1081.                 $this->_currentSectionNumber . '.' . $this->_examples[$this->_currentSectionNumber];
  1082.             }
  1083.             break;
  1084.  
  1085.             case 'figure': {
  1086.                 if (!isset($this->_figures[$this->_currentFigureNumber])) {
  1087.                     $this->_figures[$this->_currentSectionNumber] = 1;
  1088.                 } else {
  1089.                     $this->_figures[$this->_currentSectionNumber]++;
  1090.                 }
  1091.  
  1092.                 $this->_currentFigureNumber =
  1093.                 $this->_currentSectionNumber . '.' . $this->_figures[$this->_currentSectionNumber];
  1094.             }
  1095.             break;
  1096.         }
  1097.  
  1098.         if (!empty($id)) {
  1099.             $id = '<a id="' . $id . '" />';
  1100.         }
  1101.  
  1102.         return $id;
  1103.     }
  1104.  
  1105.     // }}}
  1106.     // {{{ function _endSection($type)
  1107.  
  1108.     /**
  1109.     * @param  string
  1110.     * @access private
  1111.     */
  1112.     function _endSection($type) {
  1113.         array_pop($this->_context);
  1114.  
  1115.         switch ($type) {
  1116.             case 'article':
  1117.             case 'book':
  1118.             case 'chapter':
  1119.             case 'section': {
  1120.                 $this->_sections[$type]['open']--;
  1121.             }
  1122.             break;
  1123.         }
  1124.     }
  1125.  
  1126.     // }}}
  1127. }
  1128.  
  1129. /*
  1130.  * vim600:  et sw=2 ts=2 fdm=marker
  1131.  * vim<600: et sw=2 ts=2
  1132.  */
  1133. ?>
  1134.