Subversion Repositories Applications.papyrus

Rev

Rev 173 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

<?php
// $Id: Table.php,v 1.3 2006-04-28 12:41:27 florian Exp $


/**
* 
* This class implements a Text_Wiki_Parse to find source text marked as a
* set of table rows, where a line start and ends with double-pipes (||)
* and uses double-pipes to separate table cells.  The rows must be on
* sequential lines (no blank lines between them) -- a blank line
* indicates the beginning of a new table.
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @package Text_Wiki
*
*/

class Text_Wiki_Parse_Table extends Text_Wiki_Parse {
    
    
    /**
    * 
    * The regular expression used to parse the source text and find
    * matches conforming to this rule.  Used by the parse() method.
    * 
    * @access public
    * 
    * @var string
    * 
    * @see parse()
    * 
    */
    
    var $regex = '/\n(\[\|(.*)\n)(.+)(\n\|\])/Ums';
    
    
    /**
    * 
    * Generates a replacement for the matched text.
    * 
    * Token options are:
    * 
    * 'type' =>
    *     'table_start' : the start of a bullet list
    *     'table_end'   : the end of a bullet list
    *     'row_start' : the start of a number list
    *     'row_end'   : the end of a number list
    *     'cell_start'   : the start of item text (bullet or number)
    *     'cell_end'     : the end of item text (bullet or number)
    * 
    * 'cols' => the number of columns in the table (for 'table_start')
    * 
    * 'rows' => the number of rows in the table (for 'table_start')
    * 
    * 'span' => column span (for 'cell_start')
    * 
    * 'attr' => column attribute flag (for 'cell_start')
    * 
    * @access public
    *
    * @param array &$matches The array of matches from parse().
    *
    * @return A series of text and delimited tokens marking the different
    * table elements and cell text.
    *
    */
    
    function process(&$matches)
    {
        // our eventual return value
        $return = '';
        
        // the number of columns in the table
        $num_cols = 0;
        
        // the number of rows in the table
        $num_rows = 0;
        
        // rows are separated by newlines in the matched text
        $rows = explode("\n", $matches[3]);
        
        // loop through each row
        foreach ($rows as $row) {
            
            // increase the row count
            $num_rows ++;
            
            // cells are separated by double-pipes
            $cell = explode("|", $row);
            
            // get the number of cells (columns) in this row
            $last = count($cell) - 1;
            
            // is this more than the current column count?
            // (we decrease by 1 because we never use cell zero)
            if ($last - 1 > $num_cols) {
                // increase the column count
                $num_cols = $last - 1;
            }
            
            // Les attributs de la ligne
            $morceaux='';
            if (preg_match('/^!(.*)!$/U', $cell[0], $morceaux)) {
                $attr = $morceaux[1];
            } else {
                $attr = '';
            }
            
            // start a new row
            $return .= $this->wiki->addToken(
                $this->rule,
                array(  'type' => 'row_start',
                        'attr' => $attr,
                        'span' => 1)
            );
            
            // ignore cell zero, and ignore the "last" cell; cell zero
            // is before the first double-pipe, and the "last" cell is
            // after the last double-pipe. both are always empty.
            for ($i = 1; $i < $last; $i ++) {
                // if there is no content at all, then it's an instance
                // of two sets of || next to each other, indicating a
                // span.
                if ($cell[$i] == '') {
                    continue;
                } else {
                    // this cell has content.
                    // find any special "attr"ibute cell markers
                    if (substr($cell[$i], 0, 1) == ' ' && substr($cell[$i], -1, 1) != ' ') {
                        // right-align
                        $attr = 'align="right"';
                        $cell[$i] = substr($cell[$i], 1);
                    } elseif (substr($cell[$i], 0, 1) == ' ' && substr($cell[$i], -1, 1) == ' ') {
                        // center-align
                        $attr = 'align="center"';
                        $cell[$i] = substr(substr($cell[$i], 1), 0, -1);
                    } elseif (substr($cell[$i], 0, 1) != ' ' && substr($cell[$i], -1, 1) == ' ') {
                        // left-align
                        $attr = 'align="left"';
                        $cell[$i] = substr($cell[$i], 0, -1);
                    } else {
                        $attr = null;
                    }
                    
                    if (substr($cell[$i], 0, 1) == '!') {
                        // Les attributs de la cellule
                        preg_match('/^!(.*)!(.*)$/U', $cell[$i], $morceaux);
                        $attr .= ' '.$morceaux[1];
                        $cell[$i] = $morceaux[2];
                    }
                    
                    // start a new cell...
                    $return .= $this->wiki->addToken(
                        $this->rule, 
                        array (
                            'type' => 'cell_start',
                            'attr' => $attr,
                            'span' => 1
                        ),
                        false
                    );
                    
                    // ...add the content...
                    $return .= trim($cell[$i]);
                    
                    // ...and end the cell.
                    $return .= $this->wiki->addToken(
                        $this->rule, 
                        array (
                            'type' => 'cell_end',
                            'attr' => $attr
                        ),
                        false
                    );
                }
            }
            
            // end the row
            $return .= $this->wiki->addToken(
                $this->rule,
                array('type' => 'row_end')
            );
            
        }
        
        // wrap the return value in start and end tokens 
        $return =
            $this->wiki->addToken(
                $this->rule,
                array(
                    'type' => 'table_start',
                    'rows' => $num_rows,
                    'cols' => $num_cols,
                    'attr' => $matches[2]
                )
            )
            . $return .
            $this->wiki->addToken(
                $this->rule,
                array(
                    'type' => 'table_end'
                )
            );
        
        // we're done!
        return "\n$return\n\n";
    }
}
?>