xref: /dokuwiki/inc/Parsing/ParserMode/Preformatted.php (revision 504c13e8df88563c11b3720b317991bc38835a35)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6use dokuwiki\Parsing\Handler\Preformatted as PreformattedHandler;
7use dokuwiki\Parsing\ModeRegistry;
8
9class Preformatted extends AbstractMode
10{
11    /** @inheritdoc */
12    public function getSort()
13    {
14        return 20;
15    }
16
17    /** @inheritdoc */
18    public function connectTo($mode)
19    {
20        $markers = ModeRegistry::getInstance()->getLineStartMarkers();
21        $lookahead = $markers ? '(?![' . implode('', $markers) . '])' : '';
22
23        $this->Lexer->addEntryPattern('\n  ' . $lookahead, $mode, 'preformatted');
24        $this->Lexer->addEntryPattern('\n\t' . $lookahead, $mode, 'preformatted');
25
26        // match continuation lines inside the preformatted block
27        $this->Lexer->addPattern('\n  ', 'preformatted');
28        $this->Lexer->addPattern('\n\t', 'preformatted');
29    }
30
31    /** @inheritdoc */
32    public function postConnect()
33    {
34        $this->Lexer->addExitPattern('\n', 'preformatted');
35    }
36
37    /** @inheritdoc */
38    public function handle($match, $state, $pos, Handler $handler)
39    {
40        switch ($state) {
41            case DOKU_LEXER_ENTER:
42                $handler->setCallWriter(new PreformattedHandler($handler->getCallWriter()));
43                $handler->addCall('preformatted_start', [], $pos);
44                break;
45            case DOKU_LEXER_EXIT:
46                $handler->addCall('preformatted_end', [], $pos);
47                /** @var PreformattedHandler $reWriter */
48                $reWriter = $handler->getCallWriter();
49                $handler->setCallWriter($reWriter->process());
50                break;
51            case DOKU_LEXER_MATCHED:
52                $handler->addCall('preformatted_newline', [], $pos);
53                break;
54            case DOKU_LEXER_UNMATCHED:
55                $handler->addCall('preformatted_content', [$match], $pos);
56                break;
57        }
58
59        return true;
60    }
61}
62