[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> table.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4                                                        |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2003 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available through the world-wide-web at                              |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id: table.php,v 1.3 2004/12/02 10:54:32 nohn Exp $
  20  
  21  
  22  /**
  23  * 
  24  * This class implements a Text_Wiki_Rule to find source text marked as a
  25  * set of table rows, where a line start and ends with double-pipes (||)
  26  * and uses double-pipes to separate table cells.  The rows must be on
  27  * sequential lines (no blank lines between them) -- a blank line
  28  * indicates the beginning of a new table.
  29  *
  30  * @author Paul M. Jones <pmjones@ciaweb.net>
  31  *
  32  * @package Text_Wiki
  33  *
  34  */
  35  
  36  class Text_Wiki_Rule_table extends Text_Wiki_Rule {
  37      
  38      
  39      /**
  40      * 
  41      * The regular expression used to parse the source text and find
  42      * matches conforming to this rule.  Used by the parse() method.
  43      * 
  44      * @access public
  45      * 
  46      * @var string
  47      * 
  48      * @see parse()
  49      * 
  50      */
  51      
  52      var $regex = '/\n((\|\|).*)(\n)(?!(\|\|))/Us';
  53      
  54      
  55      /**
  56      * 
  57      * Generates a replacement for the matched text.  Token options are:
  58      * 
  59      * 'type' =>
  60      *     'table_start' : the start of a bullet list
  61      *     'table_end'   : the end of a bullet list
  62      *     'row_start' : the start of a number list
  63      *     'row_end'   : the end of a number list
  64      *     'cell_start'   : the start of item text (bullet or number)
  65      *     'cell_end'     : the end of item text (bullet or number)
  66      * 
  67      * 'colspan' => column span (for a cell)
  68      * 
  69      * @access public
  70      *
  71      * @param array &$matches The array of matches from parse().
  72      *
  73      * @return A series of text and delimited tokens marking the different
  74      * table elements and cell text.
  75      *
  76      */
  77      
  78      function process(&$matches)
  79      {
  80          // out eventual return value
  81          $return = '';
  82          
  83          // start a new table
  84          $return .= $this->addToken(array('type' => 'table_start'));
  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              // start a new row
  93              $return .= $this->addToken(array('type' => 'row_start'));
  94              
  95              // cells are separated by double-pipes
  96              $cell = explode("||", $row);
  97              
  98              // get the last cell number
  99              $last = count($cell) - 1;
 100              
 101              // by default, cells span only one column (their own)
 102              $span = 1;
 103              
 104              // ignore cell zero, and ignore the "last" cell; cell zero
 105              // is before the first double-pipe, and the "last" cell is
 106              // after the last double-pipe. both are always empty.
 107              for ($i = 1; $i < $last; $i ++) {
 108                  
 109                  // if there is no content at all, then it's an instance
 110                  // of two sets of || next to each other, indicating a
 111                  // colspan.
 112                  if ($cell[$i] == '') {
 113                      
 114                      // add to the span and loop to the next cell
 115                      $span += 1;
 116                      continue;
 117                      
 118                  } else {
 119                      
 120                      // this cell has content.
 121                      
 122                      // find the alignment, if any.
 123                      if (substr($cell[$i], 0, 2) == '> ') {
 124                          // set to right-align and strip the tag
 125                          $align = 'right';
 126                          $cell[$i] = substr($cell[$i], 2);
 127                      } elseif (substr($cell[$i], 0, 2) == '= ') {
 128                          // set to center-align and strip the tag
 129                          $align = 'center';
 130                          $cell[$i] = substr($cell[$i], 2);
 131                      } elseif (substr($cell[$i], 0, 2) == '< ') {
 132                          // set to left-align and strip the tag
 133                          $align = 'left';
 134                          $cell[$i] = substr($cell[$i], 2);
 135                      } else {
 136                          $align = null;
 137                      }
 138                      
 139                      // start a new cell...
 140                      $return .= $this->addToken(
 141                          array (
 142                              'type' => 'cell_start',
 143                              'align' => $align,
 144                              'colspan' => $span
 145                          )
 146                      );
 147                      
 148                      // ...add the content...
 149                      $return .= trim($cell[$i]);
 150                      
 151                      // ...and end the cell.
 152                      $return .= $this->addToken(
 153                          array (
 154                              'type' => 'cell_end',
 155                              'align' => $align,
 156                              'colspan' => $span
 157                          )
 158                      );
 159                      
 160                      // reset the colspan.
 161                      $span = 1;
 162                  }
 163                      
 164              }
 165              
 166              // end the row
 167              $return .= $this->addToken(array('type' => 'row_end'));
 168              
 169          }
 170          
 171          // end the table
 172          $return .= $this->addToken(array('type' => 'table_end'));
 173          
 174          // we're done!
 175          return "\n$return\n";
 176      }
 177      
 178      
 179      /**
 180      * 
 181      * Renders a token into text matching the requested format.
 182      * 
 183      * @access public
 184      * 
 185      * @param array $options The "options" portion of the token (second
 186      * element).
 187      * 
 188      * @return string The text rendered from the token options.
 189      * 
 190      */
 191      
 192      function renderXhtml($options)
 193      {
 194          // make nice variable names (type, align, colspan)
 195          extract($options);
 196          
 197          $pad = '    ';
 198          
 199          $border = (isset($this->_conf['border']))
 200              ? $this->_conf['border'] : '1';
 201              
 202          $spacing = (isset($this->_conf['spacing']))
 203              ? $this->_conf['spacing'] : '0';
 204          
 205          $padding = (isset($this->_conf['padding']))
 206              ? $this->_conf['padding'] : '4';
 207          
 208          switch ($type) {
 209          
 210          case 'table_start':
 211              return "<table border=\"$border\" " .
 212                  "cellspacing=\"$spacing\" " .
 213                  "cellpadding=\"$padding\">\n";
 214              break;
 215          
 216          case 'table_end':
 217              return "</table>\n";
 218              break;
 219          
 220          case 'row_start':
 221              return "$pad<tr>\n";
 222              break;
 223          
 224          case 'row_end':
 225              return "$pad</tr>\n";
 226              break;
 227          
 228          case 'cell_start':
 229              $tmp = $pad . $pad . '<td';
 230              if ($colspan > 1) {
 231                  $tmp .= " colspan=\"$colspan\"";
 232              }
 233              if ($align) {
 234                  $tmp .= " align=\"$align\"";
 235              }
 236              return $tmp . '>';
 237              break;
 238          
 239          case 'cell_end':
 240              return "</td>\n";
 241              break;
 242          
 243          default:
 244              return '';
 245          
 246          }
 247      }
 248  }
 249  ?>


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics