Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file table.php

Documentation is available at table.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: table.php,v 1.2 2004/03/27 14:47:32 pmjones Exp $
  20.  
  21.  
  22. /**
  23. * This class implements a Text_Wiki_Rule to find source text marked as a
  24. * set of table rows, where a line start and ends with double-pipes (||)
  25. * and uses double-pipes to separate table cells.  The rows must be on
  26. * sequential lines (no blank lines between them) -- a blank line
  27. * indicates the beginning of a new table.
  28. *
  29. @author Paul M. Jones <pmjones@ciaweb.net>
  30. *
  31. @package Text_Wiki
  32. *
  33. */
  34.  
  35.     
  36.     
  37.     /**
  38.     * 
  39.     * The regular expression used to parse the source text and find
  40.     * matches conforming to this rule.  Used by the parse() method.
  41.     * 
  42.     * @access public
  43.     * 
  44.     * @var string 
  45.     * 
  46.     * @see parse()
  47.     * 
  48.     */
  49.     
  50.     var $regex = '/\n((\|\|).*)(\n)(?!(\|\|))/Us';
  51.     
  52.     
  53.     /**
  54.     * 
  55.     * Generates a replacement for the matched text.  Token options are:
  56.     * 
  57.     * 'type' =>
  58.     *     'table_start' : the start of a bullet list
  59.     *     'table_end'   : the end of a bullet list
  60.     *     'row_start' : the start of a number list
  61.     *     'row_end'   : the end of a number list
  62.     *     'cell_start'   : the start of item text (bullet or number)
  63.     *     'cell_end'     : the end of item text (bullet or number)
  64.     * 
  65.     * 'colspan' => column span (for a cell)
  66.     * 
  67.     * @access public
  68.     *
  69.     * @param array &$matches The array of matches from parse().
  70.     *
  71.     * @return series of text and delimited tokens marking the different
  72.     *  table elements and cell text.
  73.     *
  74.     */
  75.     
  76.     function process(&$matches)
  77.     {
  78.         // out eventual return value
  79.         $return '';
  80.         
  81.         // start a new table
  82.         $return .= $this->addToken(array('type' => 'table_start'));
  83.         
  84.         // rows are separated by newlines in the matched text
  85.         $rows explode("\n"$matches[1]);
  86.         
  87.         // loop through each row
  88.         foreach ($rows as $row{
  89.             
  90.             // start a new row
  91.             $return .= $this->addToken(array('type' => 'row_start'));
  92.             
  93.             // cells are separated by double-pipes
  94.             $cell explode("||"$row);
  95.             
  96.             // get the last cell number
  97.             $last count($cell- 1;
  98.             
  99.             // by default, cells span only one column (their own)
  100.             $span = 1;
  101.             
  102.             // ignore cell zero, and ignore the "last" cell; cell zero
  103.             // is before the first double-pipe, and the "last" cell is
  104.             // after the last double-pipe. both are always empty.
  105.             for ($i = 1; $i $last$i ++{
  106.                 
  107.                 // if there is no content at all, then it's an instance
  108.                 // of two sets of || next to each other, indicating a
  109.                 // colspan.
  110.                 if ($cell[$i== ''{
  111.                     
  112.                     // add to the span and loop to the next cell
  113.                     $span += 1;
  114.                     continue;
  115.                     
  116.                 else {
  117.                     
  118.                     // this cell has content.
  119.                     
  120.                     // find the alignment, if any.
  121.                     if (substr($cell[$i]02== '> '{
  122.                         // set to right-align and strip the tag
  123.                         $align 'right';
  124.                         $cell[$isubstr($cell[$i]2);
  125.                     elseif (substr($cell[$i]02== '= '{
  126.                         // set to center-align and strip the tag
  127.                         $align 'center';
  128.                         $cell[$isubstr($cell[$i]2);
  129.                     elseif (substr($cell[$i]02== '< '{
  130.                         // set to left-align and strip the tag
  131.                         $align 'left';
  132.                         $cell[$isubstr($cell[$i]2);
  133.                     else {
  134.                         $align = null;
  135.                     }
  136.                     
  137.                     // start a new cell...
  138.                     $return .= $this->addToken(
  139.                         array (
  140.                             'type' => 'cell_start',
  141.                             'align' => $align,
  142.                             'colspan' => $span
  143.                         )
  144.                     );
  145.                     
  146.                     // ...add the content...
  147.                     $return .= trim($cell[$i]);
  148.                     
  149.                     // ...and end the cell.
  150.                     $return .= $this->addToken(
  151.                         array (
  152.                             'type' => 'cell_end',
  153.                             'align' => $align,
  154.                             'colspan' => $span
  155.                         )
  156.                     );
  157.                     
  158.                     // reset the colspan.
  159.                     $span = 1;
  160.                 }
  161.                     
  162.             }
  163.             
  164.             // end the row
  165.             $return .= $this->addToken(array('type' => 'row_end'));
  166.             
  167.         }
  168.         
  169.         // end the table
  170.         $return .= $this->addToken(array('type' => 'table_end'));
  171.         
  172.         // we're done!
  173.         return "\n$return";
  174.     }
  175.     
  176.     
  177.     /**
  178.     * 
  179.     * Renders a token into text matching the requested format.
  180.     * 
  181.     * @access public
  182.     * 
  183.     * @param array $options The "options" portion of the token (second
  184.     *  element).
  185.     * 
  186.     * @return string The text rendered from the token options.
  187.     * 
  188.     */
  189.     
  190.     function renderXhtml($options)
  191.     {
  192.         // make nice variable names (type, align, colspan)
  193.         extract($options);
  194.         
  195.         $pad '    ';
  196.         
  197.         $border (isset($this->_conf['border']))
  198.             ? $this->_conf['border''1';
  199.             
  200.         $spacing (isset($this->_conf['spacing']))
  201.             ? $this->_conf['spacing''0';
  202.         
  203.         $padding (isset($this->_conf['padding']))
  204.             ? $this->_conf['padding''4';
  205.         
  206.         switch ($type{
  207.         
  208.         case 'table_start':
  209.             return "<table border=\"$border\" " .
  210.                 "cellspacing=\"$spacing\" " .
  211.                 "cellpadding=\"$padding\">\n";
  212.             break;
  213.         
  214.         case 'table_end':
  215.             return "</table>\n";
  216.             break;
  217.         
  218.         case 'row_start':
  219.             return "$pad<tr>\n";
  220.             break;
  221.         
  222.         case 'row_end':
  223.             return "$pad</tr>\n";
  224.             break;
  225.         
  226.         case 'cell_start':
  227.             $tmp $pad $pad '<td';
  228.             if ($colspan > 1{
  229.                 $tmp .= " colspan=\"$colspan\"";
  230.             }
  231.             if ($align{
  232.                 $tmp .= " align=\"$align\"";
  233.             }
  234.             return $tmp '>';
  235.             break;
  236.         
  237.         case 'cell_end':
  238.             return "</td>\n";
  239.             break;
  240.         
  241.         default:
  242.             return '';
  243.         
  244.         }
  245.     }
  246. }
  247. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.