1<?php
2// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
3/**
4 * BBCode: Parses for block-quoted text.
5 *
6 * This class implements a Text_Wiki_Rule to find source text block-quoted
7 * as defined by text surrounded by [quote="author"] ... [/quote] (author optional)
8 * On parsing, the text itself is left in place, but the starting and ending
9 * tags are replaced with tokens.
10 *
11 * PHP versions 4 and 5
12 *
13 * @category   Text
14 * @package    Text_Wiki
15 * @author     Bertrand Gugger <bertrand@toggg.com>
16 * @copyright  2005 bertrand Gugger
17 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
18 * @version    CVS: $Id$
19 * @link       http://pear.php.net/package/Text_Wiki
20 */
21
22/**
23 * Block-quoted text rule parser class (with nesting) for BBCode.
24 *
25 * @category   Text
26 * @package    Text_Wiki
27 * @author     Bertrand Gugger <bertrand@toggg.com>
28 * @copyright  2005 bertrand Gugger
29 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
30 * @version    Release: @package_version@
31 * @link       http://pear.php.net/package/Text_Wiki
32 * @see        Text_Wiki_Parse::Text_Wiki_Parse()
33 */
34class Text_Wiki_Parse_Blockquote extends Text_Wiki_Parse {
35
36    /**
37     * The regular expression used to parse the source text and find
38     * matches conforming to this rule.  Used by the parse() method.
39     * We match [quote=..] ... [/quote] with nesting
40     *
41     * @access public
42     * @var string
43     * @see Text_Wiki_Parse::parse()
44     */
45    var $regex = '#\[quote(?:=\s*"(.*?)")?\s*]((?:((?R))|.)*?)\[/quote]#msi';
46
47    /**
48     * The current quote nesting depth, starts by zero
49     *
50     * @access private
51     * @var int
52     */
53    var $_level = 0;
54
55    /**
56     * Generates a replacement for the matched text.  Token options are:
57     * - 'type' => ['start'|'end'] The starting or ending point of the block-quoted text.
58     * The text itself is left in the source but may content bested blocks
59     * - 'level' => the level of nesting (starting 0)
60     * - 'name' => the author indicator (optional)
61     *
62     * @param array &$matches The array of matches from parse().
63     * @return string Delimited by start/end tokens to be used as
64     * placeholder in the source text surrounding the text to be quoted.
65     * @access public
66     */
67    function process(&$matches)
68    {
69        // nested block ?
70        if (array_key_exists(3, $matches)) {
71            $this->_level++;
72            $expsub = preg_replace_callback(
73                $this->regex,
74                array(&$this, 'process'),
75                $matches[2]
76            );
77            $this->_level--;
78        } else {
79            $expsub = $matches[2];
80        }
81
82        // builds the option array
83        $options = array('type' => 'start', 'level'=>$this->_level);
84        if (isset($matches[1])) {
85            $options['name'] = $matches[1];
86        }
87        $statok = $this->wiki->addToken($this->rule, $options);
88        $options['type'] = 'end';
89        return $statok . $expsub . $this->wiki->addToken($this->rule, $options);
90    }
91}
92