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