xref: /dokuwiki/inc/Parsing/ParserMode/Listblock.php (revision 47a02a102092be9e1e6f1ddaf158bdfffdb13d4f)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
571096e46SAndreas Gohruse dokuwiki\Parsing\Handler;
671096e46SAndreas Gohruse dokuwiki\Parsing\Handler\Lists;
7c8dd1b9dSAndreas Gohruse dokuwiki\Parsing\ModeRegistry;
8c8dd1b9dSAndreas Gohr
9be906b56SAndreas Gohrclass Listblock extends AbstractMode
10be906b56SAndreas Gohr{
11*47a02a10SAndreas Gohr    /** @inheritdoc */
12*47a02a10SAndreas Gohr    protected function allowedCategories(): array
13be906b56SAndreas Gohr    {
14*47a02a10SAndreas Gohr        return [
15c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_FORMATTING,
1656c730b5SAndreas Gohr            ModeRegistry::CATEGORY_SUBSTITUTION,
17c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_DISABLED,
18c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_PROTECTED,
19*47a02a10SAndreas Gohr        ];
20be906b56SAndreas Gohr    }
21be906b56SAndreas Gohr
22be906b56SAndreas Gohr    /** @inheritdoc */
2371096e46SAndreas Gohr    public function getSort()
2471096e46SAndreas Gohr    {
2571096e46SAndreas Gohr        return 10;
2671096e46SAndreas Gohr    }
2771096e46SAndreas Gohr
2871096e46SAndreas Gohr    /** @inheritdoc */
297958e698SAndreas Gohr    public function preConnect()
307958e698SAndreas Gohr    {
31*47a02a10SAndreas Gohr        $this->registry->registerBlockEolMode('listblock');
327958e698SAndreas Gohr    }
337958e698SAndreas Gohr
347958e698SAndreas Gohr    /** @inheritdoc */
35be906b56SAndreas Gohr    public function connectTo($mode)
36be906b56SAndreas Gohr    {
37be906b56SAndreas Gohr        $this->Lexer->addEntryPattern('[ \t]*\n {2,}[\-\*]', $mode, 'listblock');
38be906b56SAndreas Gohr        $this->Lexer->addEntryPattern('[ \t]*\n\t{1,}[\-\*]', $mode, 'listblock');
39be906b56SAndreas Gohr
40be906b56SAndreas Gohr        $this->Lexer->addPattern('\n {2,}[\-\*]', 'listblock');
41be906b56SAndreas Gohr        $this->Lexer->addPattern('\n\t{1,}[\-\*]', 'listblock');
42be906b56SAndreas Gohr    }
43be906b56SAndreas Gohr
44be906b56SAndreas Gohr    /** @inheritdoc */
45be906b56SAndreas Gohr    public function postConnect()
46be906b56SAndreas Gohr    {
47be906b56SAndreas Gohr        $this->Lexer->addExitPattern('\n', 'listblock');
48be906b56SAndreas Gohr    }
49be906b56SAndreas Gohr
50be906b56SAndreas Gohr    /** @inheritdoc */
5171096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
52be906b56SAndreas Gohr    {
5371096e46SAndreas Gohr        switch ($state) {
5471096e46SAndreas Gohr            case DOKU_LEXER_ENTER:
5571096e46SAndreas Gohr                $handler->setCallWriter(new Lists($handler->getCallWriter()));
5671096e46SAndreas Gohr                $handler->addCall('list_open', [$match], $pos);
5771096e46SAndreas Gohr                break;
5871096e46SAndreas Gohr            case DOKU_LEXER_EXIT:
5971096e46SAndreas Gohr                $handler->addCall('list_close', [], $pos);
6071096e46SAndreas Gohr                /** @var Lists $reWriter */
6171096e46SAndreas Gohr                $reWriter = $handler->getCallWriter();
6271096e46SAndreas Gohr                $handler->setCallWriter($reWriter->process());
6371096e46SAndreas Gohr                break;
6471096e46SAndreas Gohr            case DOKU_LEXER_MATCHED:
6571096e46SAndreas Gohr                $handler->addCall('list_item', [$match], $pos);
6671096e46SAndreas Gohr                break;
6771096e46SAndreas Gohr            case DOKU_LEXER_UNMATCHED:
6871096e46SAndreas Gohr                $handler->addCall('cdata', [$match], $pos);
6971096e46SAndreas Gohr                break;
7071096e46SAndreas Gohr        }
7171096e46SAndreas Gohr        return true;
72be906b56SAndreas Gohr    }
73be906b56SAndreas Gohr}
74