xref: /dokuwiki/inc/Parsing/ParserMode/Code.php (revision 1e28e406b358f79221c515b2a56520d5dbbfb6c8)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6use dokuwiki\Parsing\Helpers\Code as CodeHelper;
7
8class Code extends AbstractMode
9{
10    /** @var string The call type used in addCall ('code' or 'file') */
11    protected $type = 'code';
12
13    /** @inheritdoc */
14    public function getSort()
15    {
16        return 200;
17    }
18
19    /** @inheritdoc */
20    public function connectTo($mode)
21    {
22        $this->Lexer->addEntryPattern('<code\b(?=.*</code>)', $mode, 'code');
23    }
24
25    /** @inheritdoc */
26    public function postConnect()
27    {
28        $this->Lexer->addExitPattern('</code>', 'code');
29    }
30
31    /** @inheritdoc */
32    public function handle($match, $state, $pos, Handler $handler)
33    {
34        if ($state !== DOKU_LEXER_UNMATCHED) return true;
35
36        // split "language filename [options]>content" at the first >
37        [$attr, $content] = sexplode('>', $match, 2, '');
38        [$language, $filename, $options] = CodeHelper::parseAttributes($attr);
39
40        $param = [$content, $language, $filename];
41        if ($options !== null) $param[] = $options;
42        $handler->addCall($this->type, $param, $pos);
43
44        return true;
45    }
46}
47