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 243077 2007-09-28 17:14:58Z mic $
  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.             // remove first and last (optional) pipe
  98.             $row substr($row1);
  99.             if ($row[strlen($row- 1== '|'{
  100.                 $row substr($row0-1);
  101.             }
  102.  
  103.             // cells are separated by pipes
  104.             $cells explode("|"$row);
  105.             
  106.             if (count($cells== 1 && $cells[0][0== '=' && ($num_rows == 1 || $num_rows == count($rows)) && isset($caption)) {
  107.                 $caption trim(trim($cells[0]'='));
  108.             
  109.                 // start the caption...
  110.                 $return .= $this->wiki->addToken(
  111.                     $this->rule,
  112.                     array ('type' => 'caption_start')
  113.                 );
  114.  
  115.                 // ...add the content...
  116.                 $return .= $caption;
  117.  
  118.                 // ...and end the caption.
  119.                 $return .= $this->wiki->addToken(
  120.                     $this->rule,
  121.                     array ('type' => 'caption_end')
  122.                 );
  123.             }
  124.             else {
  125.  
  126.                 // update the column count
  127.                 if (count($cells$num_cols{
  128.                     $num_cols count($cells);
  129.                 }
  130.  
  131.                 // start a new row
  132.                 $return .= $this->wiki->addToken(
  133.                     $this->rule,
  134.                     array('type' => 'row_start')
  135.                 );
  136.  
  137.                 for ($i = 0; $i count($cells)$i++{
  138.                     $cell $cells[$i];
  139.  
  140.                     // by default, cells span only one column (their own)
  141.                     $span = 1;
  142.                     $attr '';
  143.                     
  144.                     while ($i + 1 < count($cells&& strlen($cells[$i + 1])) {
  145.                         $i++;
  146.                         $span++;
  147.                     }
  148.  
  149.                     if (strlen($cell> 0 && $cell[0== '='{
  150.                         $attr 'header';
  151.                         $cell trim($cell'=');
  152.                     }
  153.  
  154.                     // start a new cell...
  155.                     $return .= $this->wiki->addToken(
  156.                         $this->rule,
  157.                         array (
  158.                             'type' => 'cell_start',
  159.                             'attr' => $attr,
  160.                             'span' => $span
  161.                         )
  162.                     );
  163.  
  164.                     // ...add the content...
  165.                     $return .= trim($cell);
  166.  
  167.                     // ...and end the cell.
  168.                     $return .= $this->wiki->addToken(
  169.                         $this->rule,
  170.                         array (
  171.                             'type' => 'cell_end',
  172.                             'attr' => $attr,
  173.                             'span' => $span
  174.                         )
  175.                     );
  176.                 }
  177.  
  178.                 // end the row
  179.                 $return .= $this->wiki->addToken(
  180.                     $this->rule,
  181.                     array('type' => 'row_end')
  182.                 );
  183.             }
  184.         }
  185.  
  186.         // we're done!
  187.         return
  188.             "\n\n".
  189.             $this->wiki->addToken(
  190.                 $this->rule,
  191.                 array(
  192.                     'type' => 'table_start',
  193.                     'rows' => $num_rows,
  194.                     'cols' => $num_cols
  195.                 )
  196.             ).
  197.             $return.
  198.             $this->wiki->addToken(
  199.                 $this->rule,
  200.                 array(
  201.                     'type' => 'table_end'
  202.                 )
  203.             ).
  204.             "\n\n";
  205.     }
  206. }
  207. ?>

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