1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin aichat (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author Andreas Gohr <gohr@cosmocode.de>
10 */
11class syntax_plugin_aichat_ignore extends SyntaxPlugin
12{
13    /** @var string temporary store for the current document */
14    protected $temporaryDoc;
15
16    /** @inheritDoc */
17    public function getType()
18    {
19        return 'formatting';
20    }
21
22    /** @inheritdoc */
23    public function getAllowedTypes()
24    {
25        return [
26            'container',
27            'formatting',
28            'substition',
29            'protected',
30            'disabled',
31            'paragraphs',
32            'baseonly',
33        ];
34    }
35
36    /** @inheritDoc */
37    public function getPType()
38    {
39        return 'normal';
40    }
41
42    /** @inheritDoc */
43    public function getSort()
44    {
45        return 32;
46    }
47
48    /** @inheritDoc */
49    public function connectTo($mode)
50    {
51        $this->Lexer->addEntryPattern('<ai-ignore>', $mode, 'plugin_aichat_ignore');
52    }
53
54    /** @inheritDoc */
55    public function postConnect()
56    {
57        $this->Lexer->addExitPattern('</ai-ignore>', 'plugin_aichat_ignore');
58    }
59
60    /** @inheritDoc */
61    public function handle($match, $state, $pos, Doku_Handler $handler)
62    {
63        $data = [
64            'state' => $state,
65            'match' => $match,
66            'pos' => $pos,
67        ];
68
69        return $data;
70    }
71
72    /** @inheritDoc */
73    public function render($mode, Doku_Renderer $renderer, $data)
74    {
75        // in all non-aichat modes we just output the raw content and are done
76        if ($mode !== 'aichat') {
77            if ($data['state'] === DOKU_LEXER_UNMATCHED) {
78                $renderer->cdata($data['match']);
79            }
80            return true;
81        }
82
83        // we're now in aichat mode, ignore everything inside the tags
84        switch ($data['state']) {
85            case DOKU_LEXER_ENTER:
86                $this->temporaryDoc = $renderer->doc;
87                $renderer->doc = '';
88                break;
89            case DOKU_LEXER_EXIT:
90                $renderer->doc = $this->temporaryDoc;
91                $this->temporaryDoc = '';
92                break;
93        }
94
95        return true;
96    }
97}
98