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. // $Id: Table.php,v 1.4 2004/09/25 19:05:13 pmjones Exp $
  3.  
  4.  
  5. /**
  6. * This class implements a Text_Wiki_Parse to find source text marked as a
  7. * set of table rows, where a line start and ends with double-pipes (||)
  8. * and uses double-pipes to separate table cells.  The rows must be on
  9. * sequential lines (no blank lines between them) -- a blank line
  10. * indicates the beginning of a new table.
  11. *
  12. @author Paul M. Jones <pmjones@ciaweb.net>
  13. *
  14. @package Text_Wiki
  15. *
  16. */
  17.  
  18.     
  19.     
  20.     /**
  21.     * 
  22.     * The regular expression used to parse the source text and find
  23.     * matches conforming to this rule.  Used by the parse() method.
  24.     * 
  25.     * @access public
  26.     * 
  27.     * @var string 
  28.     * 
  29.     * @see parse()
  30.     * 
  31.     */
  32.     
  33.     var $regex = '/\n((\|\|).*)(\n)(?!(\|\|))/Us';
  34.     
  35.     
  36.     /**
  37.     * 
  38.     * Generates a replacement for the matched text.
  39.     * 
  40.     * Token options are:
  41.     * 
  42.     * 'type' =>
  43.     *     'table_start' : the start of a bullet list
  44.     *     'table_end'   : the end of a bullet list
  45.     *     'row_start' : the start of a number list
  46.     *     'row_end'   : the end of a number list
  47.     *     'cell_start'   : the start of item text (bullet or number)
  48.     *     'cell_end'     : the end of item text (bullet or number)
  49.     * 
  50.     * 'cols' => the number of columns in the table (for 'table_start')
  51.     * 
  52.     * 'rows' => the number of rows in the table (for 'table_start')
  53.     * 
  54.     * 'span' => column span (for 'cell_start')
  55.     * 
  56.     * 'attr' => column attribute flag (for 'cell_start')
  57.     * 
  58.     * @access public
  59.     *
  60.     * @param array &$matches The array of matches from parse().
  61.     *
  62.     * @return series of text and delimited tokens marking the different
  63.     *  table elements and cell text.
  64.     *
  65.     */
  66.     
  67.     function process(&$matches)
  68.     {
  69.         // our eventual return value
  70.         $return '';
  71.         
  72.         // the number of columns in the table
  73.         $num_cols = 0;
  74.         
  75.         // the number of rows in the table
  76.         $num_rows = 0;
  77.         
  78.         // rows are separated by newlines in the matched text
  79.         $rows explode("\n"$matches[1]);
  80.         
  81.         // loop through each row
  82.         foreach ($rows as $row{
  83.             
  84.             // increase the row count
  85.             $num_rows ++;
  86.             
  87.             // start a new row
  88.             $return .= $this->wiki->addToken(
  89.                 $this->rule,
  90.                 array('type' => 'row_start')
  91.             );
  92.             
  93.             // cells are separated by double-pipes
  94.             $cell explode("||"$row);
  95.             
  96.             // get the number of cells (columns) in this row
  97.             $last count($cell- 1;
  98.             
  99.             // is this more than the current column count?
  100.             // (we decrease by 1 because we never use cell zero)
  101.             if ($last - 1 > $num_cols{
  102.                 // increase the column count
  103.                 $num_cols $last - 1;
  104.             }
  105.             
  106.             // by default, cells span only one column (their own)
  107.             $span = 1;
  108.             
  109.             // ignore cell zero, and ignore the "last" cell; cell zero
  110.             // is before the first double-pipe, and the "last" cell is
  111.             // after the last double-pipe. both are always empty.
  112.             for ($i = 1; $i $last$i ++{
  113.                 
  114.                 // if there is no content at all, then it's an instance
  115.                 // of two sets of || next to each other, indicating a
  116.                 // span.
  117.                 if ($cell[$i== ''{
  118.                     
  119.                     // add to the span and loop to the next cell
  120.                     $span += 1;
  121.                     continue;
  122.                     
  123.                 else {
  124.                     
  125.                     // this cell has content.
  126.                     
  127.                     // find any special "attr"ibute cell markers
  128.                     if (substr($cell[$i]02== '> '{
  129.                         // right-align
  130.                         $attr 'right';
  131.                         $cell[$isubstr($cell[$i]2);
  132.                     elseif (substr($cell[$i]02== '= '{
  133.                         // center-align
  134.                         $attr 'center';
  135.                         $cell[$isubstr($cell[$i]2);
  136.                     elseif (substr($cell[$i]02== '< '{
  137.                         // left-align
  138.                         $attr 'left';
  139.                         $cell[$isubstr($cell[$i]2);
  140.                     elseif (substr($cell[$i]02== '~ '{
  141.                         $attr 'header';
  142.                         $cell[$isubstr($cell[$i]2);
  143.                     else {
  144.                         $attr = null;
  145.                     }
  146.                     
  147.                     // start a new cell...
  148.                     $return .= $this->wiki->addToken(
  149.                         $this->rule
  150.                         array (
  151.                             'type' => 'cell_start',
  152.                             'attr' => $attr,
  153.                             'span' => $span
  154.                         )
  155.                     );
  156.                     
  157.                     // ...add the content...
  158.                     $return .= trim($cell[$i]);
  159.                     
  160.                     // ...and end the cell.
  161.                     $return .= $this->wiki->addToken(
  162.                         $this->rule
  163.                         array (
  164.                             'type' => 'cell_end',
  165.                             'attr' => $attr,
  166.                             'span' => $span
  167.                         )
  168.                     );
  169.                     
  170.                     // reset the span.
  171.                     $span = 1;
  172.                 }
  173.                     
  174.             }
  175.             
  176.             // end the row
  177.             $return .= $this->wiki->addToken(
  178.                 $this->rule,
  179.                 array('type' => 'row_end')
  180.             );
  181.             
  182.         }
  183.         
  184.         // wrap the return value in start and end tokens 
  185.         $return =
  186.             $this->wiki->addToken(
  187.                 $this->rule,
  188.                 array(
  189.                     'type' => 'table_start',
  190.                     'rows' => $num_rows,
  191.                     'cols' => $num_cols
  192.                 )
  193.             )
  194.             . $return .
  195.             $this->wiki->addToken(
  196.                 $this->rule,
  197.                 array(
  198.                     'type' => 'table_end'
  199.                 )
  200.             );
  201.         
  202.         // we're done!
  203.         return "\n$return\n\n";
  204.     }
  205. }
  206. ?>

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