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

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