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