1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4use dokuwiki\Parsing\Handler;
5
6/**
7 * BBCode plugin: allows BBCode markup familiar from forum software
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Esther Brunner <esther@kaffeehaus.ch>
11 */
12class syntax_plugin_bbcode_quote extends SyntaxPlugin
13{
14    /** @inheritdoc */
15    public function getType()
16    {
17        return 'container';
18    }
19    /** @inheritdoc */
20    public function getPType()
21    {
22        return 'block';
23    }
24    /** @inheritdoc */
25    public function getAllowedTypes()
26    {
27        return ['formatting', 'substition', 'disabled', 'protected'];
28    }
29    /** @inheritdoc */
30    public function getSort()
31    {
32        return 105;
33    }
34    /** @inheritdoc */
35    public function connectTo($mode)
36    {
37        $this->Lexer->addEntryPattern('\[quote.*?\](?=.*?\x5B/quote\x5D)', $mode, 'plugin_bbcode_quote');
38    }
39    /** @inheritdoc */
40    public function postConnect()
41    {
42        $this->Lexer->addExitPattern('\[/quote\]', 'plugin_bbcode_quote');
43    }
44
45    /** @inheritdoc */
46    public function handle($match, $state, $pos, Handler $handler)
47    {
48        switch ($state) {
49            case DOKU_LEXER_ENTER:
50                $match = explode('"', substr($match, 6, -1));
51                return [$state, $match[1] ?? ''];
52
53            case DOKU_LEXER_UNMATCHED:
54                return [$state, $match];
55
56            case DOKU_LEXER_EXIT:
57                return [$state, ''];
58        }
59        return [];
60    }
61
62    /** @inheritdoc */
63    public function render($format, Doku_Renderer $renderer, $data)
64    {
65        if ($format == 'xhtml') {
66            [$state, $match] = $data;
67            switch ($state) {
68                case DOKU_LEXER_ENTER:
69                    if ($match !== '') {
70                        $renderer->doc .= '<p><sub>' . $renderer->_xmlEntities($match) . ':</sub></p>';
71                    }
72                    $renderer->doc .= '<blockquote>';
73                    break;
74
75                case DOKU_LEXER_UNMATCHED:
76                    $match = $renderer->_xmlEntities($match);
77                    $renderer->doc .= str_replace("\n", '<br />', $match);
78                    break;
79
80                case DOKU_LEXER_EXIT:
81                    $renderer->doc .= '</blockquote>';
82                    break;
83            }
84            return true;
85        }
86        return false;
87    }
88}
89