[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> blockquote.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: blockquote.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  * blockquote, identified by any number of greater-than signs '>' at the
  26  * start of the line, followed by a space, and then the quote text; each
  27  * '>' indicates an additional level of quoting.
  28  *
  29  * @author Paul M. Jones <pmjones@ciaweb.net>
  30  *
  31  * @package Text_Wiki
  32  *
  33  */
  34  
  35  class Text_Wiki_Rule_blockquote extends Text_Wiki_Rule {
  36      
  37      
  38      /**
  39      * 
  40      * The regular expression used to parse the source text and find
  41      * matches conforming to this rule.  Used by the parse() method.
  42      * 
  43      * @access public
  44      * 
  45      * @var string
  46      * 
  47      * @see parse()
  48      * 
  49      */
  50      
  51      var $regex = '/\n((\>).*\n)(?!(\>))/Us';
  52      
  53      
  54      /**
  55      * 
  56      * Generates a replacement for the matched text.  Token options are:
  57      * 
  58      * 'type' =>
  59      *     'start' : the start of a blockquote
  60      *     'end'   : the end of a blockquote
  61      *
  62      * 'level' => the indent level (0 for the first level, 1 for the
  63      * second, etc)
  64      * 
  65      * @access public
  66      *
  67      * @param array &$matches The array of matches from parse().
  68      *
  69      * @return A series of text and delimited tokens marking the different
  70      * list text and list elements.
  71      *
  72      */
  73      
  74      function process(&$matches)
  75      {
  76          // the replacement text we will return to parse()
  77          $return = '';
  78          
  79          // the list of post-processing matches
  80          $list = array();
  81          
  82          // $matches[1] is the text matched as a list set by parse();
  83          // create an array called $list that contains a new set of
  84          // matches for the various list-item elements.
  85          preg_match_all(
  86              '=^(\>+) (.*\n)=Ums',
  87              $matches[1],
  88              $list,
  89              PREG_SET_ORDER
  90          );
  91          
  92          // a stack of starts and ends; we keep this so that we know what
  93          // indent level we're at.
  94          $stack = array();
  95          
  96          // loop through each list-item element.
  97          foreach ($list as $key => $val) {
  98              
  99              // $val[0] is the full matched list-item line
 100              // $val[1] is the number of initial '>' chars (indent level)
 101              // $val[2] is the quote text
 102              
 103              // we number levels starting at 1, not zero
 104              $level = strlen($val[1]);
 105              
 106              // get the text of the line
 107              $text = $val[2];
 108              
 109              // add a level to the list?
 110              if ($level > count($stack)) {
 111                  
 112                  // the current indent level is greater than the number
 113                  // of stack elements, so we must be starting a new
 114                  // level.  push the new level onto the stack with a 
 115                  // dummy value (boolean true)...
 116                  array_push($stack, true);
 117                  
 118                  $return .= "\n";
 119                  
 120                  // ...and add a start token to the return.
 121                  $return .= $this->addToken(
 122                      array(
 123                          'type' => 'start',
 124                          'level' => $level - 1
 125                      )
 126                  );
 127                  
 128                  $return .= "\n\n";
 129              }
 130              
 131              // remove a level?
 132              while (count($stack) > $level) {
 133                  
 134                  // as long as the stack count is greater than the
 135                  // current indent level, we need to end list types.
 136                  // continue adding end-list tokens until the stack count
 137                  // and the indent level are the same.
 138                  array_pop($stack);
 139                  
 140                  $return .= "\n\n";
 141                  
 142                  $return .= $this->addToken(
 143                      array (
 144                          'type' => 'end',
 145                          'level' => count($stack)
 146                      )
 147                  );
 148                  
 149                  $return .= "\n";
 150              }
 151              
 152              // add the line text.
 153              $return .= $text;
 154          }
 155          
 156          // the last line may have been indented.  go through the stack
 157          // and create end-tokens until the stack is empty.
 158          $return .= "\n";
 159          
 160          while (count($stack) > 0) {
 161              array_pop($stack);
 162              $return .= $this->addToken(
 163                  array (
 164                      'type' => 'end',
 165                      'level' => count($stack)
 166                  )
 167              );
 168          }
 169          
 170          // we're done!  send back the replacement text.
 171          return "$return\n";
 172      }
 173      
 174      
 175      /**
 176      * 
 177      * Renders a token into text matching the requested format.
 178      * 
 179      * @access public
 180      * 
 181      * @param array $options The "options" portion of the token (second
 182      * element).
 183      * 
 184      * @return string The text rendered from the token options.
 185      * 
 186      */
 187      
 188      function renderXhtml($options)
 189      {
 190          $type = $options['type'];
 191          $level = $options['level'];
 192      
 193          // set up indenting so that the results look nice; we do this
 194          // in two steps to avoid str_pad mathematics.  ;-)
 195          $pad = str_pad('', $level, "\t");
 196          $pad = str_replace("\t", '    ', $pad);
 197          
 198          // starting
 199          if ($type == 'start') {
 200              return "$pad<blockquote>";
 201          }
 202          
 203          // ending
 204          if ($type == 'end') {
 205              return $pad . "</blockquote>\n";
 206          }
 207      }
 208  }
 209  ?>


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