home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Parse / Default / Table.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.6 KB  |  226 lines

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