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.1 2004/06/06 15:44:34 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.     * 'colspan' => column span (for a cell)
  51.     * 
  52.     * @access public
  53.     *
  54.     * @param array &$matches The array of matches from parse().
  55.     *
  56.     * @return series of text and delimited tokens marking the different
  57.     *  table elements and cell text.
  58.     *
  59.     */
  60.     
  61.     function process(&$matches)
  62.     {
  63.         // out eventual return value
  64.         $return '';
  65.         
  66.         // start a new table
  67.         $return .= $this->wiki->addToken(
  68.             $this->rule,
  69.             array('type' => 'table_start')
  70.         );
  71.         
  72.         // rows are separated by newlines in the matched text
  73.         $rows explode("\n"$matches[1]);
  74.         
  75.         // loop through each row
  76.         foreach ($rows as $row{
  77.             
  78.             // start a new row
  79.             $return .= $this->wiki->addToken(
  80.                 $this->rule,
  81.                 array('type' => 'row_start')
  82.             );
  83.             
  84.             // cells are separated by double-pipes
  85.             $cell explode("||"$row);
  86.             
  87.             // get the last cell number
  88.             $last count($cell- 1;
  89.             
  90.             // by default, cells span only one column (their own)
  91.             $span = 1;
  92.             
  93.             // ignore cell zero, and ignore the "last" cell; cell zero
  94.             // is before the first double-pipe, and the "last" cell is
  95.             // after the last double-pipe. both are always empty.
  96.             for ($i = 1; $i $last$i ++{
  97.                 
  98.                 // if there is no content at all, then it's an instance
  99.                 // of two sets of || next to each other, indicating a
  100.                 // colspan.
  101.                 if ($cell[$i== ''{
  102.                     
  103.                     // add to the span and loop to the next cell
  104.                     $span += 1;
  105.                     continue;
  106.                     
  107.                 else {
  108.                     
  109.                     // this cell has content.
  110.                     
  111.                     // find the alignment, if any.
  112.                     if (substr($cell[$i]02== '> '{
  113.                         // set to right-align and strip the tag
  114.                         $align 'right';
  115.                         $cell[$isubstr($cell[$i]2);
  116.                     elseif (substr($cell[$i]02== '= '{
  117.                         // set to center-align and strip the tag
  118.                         $align 'center';
  119.                         $cell[$isubstr($cell[$i]2);
  120.                     elseif (substr($cell[$i]02== '< '{
  121.                         // set to left-align and strip the tag
  122.                         $align 'left';
  123.                         $cell[$isubstr($cell[$i]2);
  124.                     else {
  125.                         $align = null;
  126.                     }
  127.                     
  128.                     // start a new cell...
  129.                     $return .= $this->wiki->addToken(
  130.                         $this->rule
  131.                         array (
  132.                             'type' => 'cell_start',
  133.                             'align' => $align,
  134.                             'colspan' => $span
  135.                         )
  136.                     );
  137.                     
  138.                     // ...add the content...
  139.                     $return .= trim($cell[$i]);
  140.                     
  141.                     // ...and end the cell.
  142.                     $return .= $this->wiki->addToken(
  143.                         $this->rule
  144.                         array (
  145.                             'type' => 'cell_end',
  146.                             'align' => $align,
  147.                             'colspan' => $span
  148.                         )
  149.                     );
  150.                     
  151.                     // reset the colspan.
  152.                     $span = 1;
  153.                 }
  154.                     
  155.             }
  156.             
  157.             // end the row
  158.             $return .= $this->wiki->addToken(
  159.                 $this->rule,
  160.                 array('type' => 'row_end')
  161.             );
  162.             
  163.         }
  164.         
  165.         // end the table
  166.         $return .= $this->wiki->addToken(
  167.             $this->rule,
  168.             array('type' => 'table_end')
  169.         );
  170.         
  171.         // we're done!
  172.         return "\n$return\n\n";
  173.     }
  174. }
  175. ?>

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