xref: /dokuwiki/inc/Parsing/ParserMode/Code.php (revision b1c59bed2e3645a1f5f11438cdbe7d1596f4a3a4)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
571096e46SAndreas Gohruse dokuwiki\Parsing\Handler;
6*b1c59bedSAndreas Gohruse dokuwiki\Parsing\Helpers;
771096e46SAndreas Gohr
8be906b56SAndreas Gohrclass Code extends AbstractMode
9be906b56SAndreas Gohr{
1071096e46SAndreas Gohr    /** @var string The call type used in addCall ('code' or 'file') */
1171096e46SAndreas Gohr    protected $type = 'code';
1271096e46SAndreas Gohr
1371096e46SAndreas Gohr    /** @inheritdoc */
1471096e46SAndreas Gohr    public function getSort()
1571096e46SAndreas Gohr    {
1671096e46SAndreas Gohr        return 200;
1771096e46SAndreas Gohr    }
1871096e46SAndreas Gohr
19be906b56SAndreas Gohr    /** @inheritdoc */
20be906b56SAndreas Gohr    public function connectTo($mode)
21be906b56SAndreas Gohr    {
22be906b56SAndreas Gohr        $this->Lexer->addEntryPattern('<code\b(?=.*</code>)', $mode, 'code');
23be906b56SAndreas Gohr    }
24be906b56SAndreas Gohr
25be906b56SAndreas Gohr    /** @inheritdoc */
26be906b56SAndreas Gohr    public function postConnect()
27be906b56SAndreas Gohr    {
28be906b56SAndreas Gohr        $this->Lexer->addExitPattern('</code>', 'code');
29be906b56SAndreas Gohr    }
30be906b56SAndreas Gohr
31be906b56SAndreas Gohr    /** @inheritdoc */
3271096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
33be906b56SAndreas Gohr    {
3471096e46SAndreas Gohr        if ($state !== DOKU_LEXER_UNMATCHED) return true;
3571096e46SAndreas Gohr
3671096e46SAndreas Gohr        // split "language filename [options]>content" at the first >
3771096e46SAndreas Gohr        [$attr, $content] = sexplode('>', $match, 2, '');
38*b1c59bedSAndreas Gohr        [$language, $filename, $options] = Helpers::parseCodeAttributes($attr);
3971096e46SAndreas Gohr
4071096e46SAndreas Gohr        $param = [$content, $language, $filename];
41*b1c59bedSAndreas Gohr        if ($options !== null) $param[] = $options;
4271096e46SAndreas Gohr        $handler->addCall($this->type, $param, $pos);
4371096e46SAndreas Gohr
4471096e46SAndreas Gohr        return true;
4571096e46SAndreas Gohr    }
46be906b56SAndreas Gohr}
47