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

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