Blockquote.php 4.7 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

/**
* 
* Parse for block-quoted text.
* 
* @category Text
* 
* @package Text_Wiki
* 
* @author Paul M. Jones <pmjones@php.net>
* 
* @license LGPL
* 
15
* @version $Id: Blockquote.php,v 1.4 2006/10/21 05:56:28 justinpatrin Exp $
zYne's avatar
zYne committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
* 
*/

/**
* 
* Parse for block-quoted text.
* 
* Find source text marked as a blockquote, identified by any number of
* greater-than signs '>' at the start of the line, followed by a space,
* and then the quote text; each '>' indicates an additional level of
* quoting.
* 
* @category Text
* 
* @package Text_Wiki
* 
* @author Paul M. Jones <pmjones@php.net>
* 
*/

class Text_Wiki_Parse_Blockquote extends Text_Wiki_Parse {
    
    
    /**
    * 
    * Regex for parsing the source text.
    * 
    * @access public
    * 
    * @var string
    * 
    * @see parse()
    * 
    */
    
    var $regex = '/\n((\>).*\n)(?!(\>))/Us';
    
    
    /**
    * 
    * Generates a replacement for the matched text.
    * 
    * Token options are:
    * 
    * 'type' =>
    *     'start' : the start of a blockquote
    *     'end'   : the end of a blockquote
    *
    * 'level' => the indent level (0 for the first level, 1 for the
    * second, etc)
    * 
    * @access public
    *
    * @param array &$matches The array of matches from parse().
    *
    * @return A series of text and delimited tokens marking the different
    * list text and list elements.
    *
    */
    
    function process(&$matches)
    {
        // the replacement text we will return to parse()
79
        $return = "\n";
zYne's avatar
zYne committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93
        
        // the list of post-processing matches
        $list = array();
        
        // $matches[1] is the text matched as a list set by parse();
        // create an array called $list that contains a new set of
        // matches for the various list-item elements.
        preg_match_all(
            '=^(\>+) (.*\n)=Ums',
            $matches[1],
            $list,
            PREG_SET_ORDER
        );
        
94 95
        $curLevel = 0;

zYne's avatar
zYne committed
96 97 98 99 100 101 102 103 104 105 106
        // loop through each list-item element.
        foreach ($list as $key => $val) {
            
            // $val[0] is the full matched list-item line
            // $val[1] is the number of initial '>' chars (indent level)
            // $val[2] is the quote text
            
            // we number levels starting at 1, not zero
            $level = strlen($val[1]);
            
            // add a level to the list?
107
            while ($level > $curLevel) {
zYne's avatar
zYne committed
108 109 110 111
                // the current indent level is greater than the number
                // of stack elements, so we must be starting a new
                // level.  push the new level onto the stack with a 
                // dummy value (boolean true)...
112
                ++$curLevel;
zYne's avatar
zYne committed
113
                
114
                //$return .= "\n";
zYne's avatar
zYne committed
115 116 117 118 119 120
                
                // ...and add a start token to the return.
                $return .= $this->wiki->addToken(
                    $this->rule, 
                    array(
                        'type' => 'start',
121
                        'level' => $curLevel
zYne's avatar
zYne committed
122 123 124
                    )
                );
                
125
                //$return .= "\n\n";
zYne's avatar
zYne committed
126 127 128
            }
            
            // remove a level?
129
            while ($curLevel > $level) {
zYne's avatar
zYne committed
130 131 132 133 134 135
                
                // as long as the stack count is greater than the
                // current indent level, we need to end list types.
                // continue adding end-list tokens until the stack count
                // and the indent level are the same.
                
136
                //$return .= "\n\n";
zYne's avatar
zYne committed
137 138 139 140 141
                
                $return .= $this->wiki->addToken(
                    $this->rule, 
                    array (
                        'type' => 'end',
142
                        'level' => $curLevel
zYne's avatar
zYne committed
143 144 145
                    )
                );
                
146 147
                //$return .= "\n";
                --$curLevel;
zYne's avatar
zYne committed
148 149 150
            }
            
            // add the line text.
151
            $return .= $val[2];
zYne's avatar
zYne committed
152 153
        }
        
154 155 156 157
        // the last char of the matched pattern must be \n but we don't
        // want this to be inside the tokens
        $return = substr($return, 0, -1);
        
zYne's avatar
zYne committed
158 159
        // the last line may have been indented.  go through the stack
        // and create end-tokens until the stack is empty.
160 161 162
        //$return .= "\n";

        while ($curLevel > 0) {
zYne's avatar
zYne committed
163 164 165 166
            $return .= $this->wiki->addToken(
                $this->rule, 
                array (
                    'type' => 'end',
167
                    'level' => $curLevel
zYne's avatar
zYne committed
168 169
                )
            );
170
            --$curLevel;
zYne's avatar
zYne committed
171 172
        }
        
173 174 175
        // put back the trailing \n
        $return .= "\n";

zYne's avatar
zYne committed
176
        // we're done!  send back the replacement text.
177
        return $return;
zYne's avatar
zYne committed
178 179 180
    }
}
?>