xref: /dokuwiki/inc/Parsing/ParserMode/Listblock.php (revision 56c730b56ef2746acf3b1a27c69bada1239535bd)
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{
11be906b56SAndreas Gohr    /**
12be906b56SAndreas Gohr     * Listblock constructor.
13be906b56SAndreas Gohr     */
14be906b56SAndreas Gohr    public function __construct()
15be906b56SAndreas Gohr    {
16c8dd1b9dSAndreas Gohr        $this->allowedModes = ModeRegistry::getInstance()->getModesForCategories([
17c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_FORMATTING,
18*56c730b5SAndreas Gohr            ModeRegistry::CATEGORY_SUBSTITUTION,
19c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_DISABLED,
20c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_PROTECTED,
21c8dd1b9dSAndreas Gohr        ]);
22be906b56SAndreas Gohr    }
23be906b56SAndreas Gohr
24be906b56SAndreas Gohr    /** @inheritdoc */
2571096e46SAndreas Gohr    public function getSort()
2671096e46SAndreas Gohr    {
2771096e46SAndreas Gohr        return 10;
2871096e46SAndreas Gohr    }
2971096e46SAndreas Gohr
3071096e46SAndreas Gohr    /** @inheritdoc */
317958e698SAndreas Gohr    public function preConnect()
327958e698SAndreas Gohr    {
337958e698SAndreas Gohr        $registry = ModeRegistry::getInstance();
347958e698SAndreas Gohr        $registry->registerBlockEolMode('listblock');
357958e698SAndreas Gohr        $registry->registerLineStartMarkers('listblock', ['\\*', '\\-']);
367958e698SAndreas Gohr    }
377958e698SAndreas Gohr
387958e698SAndreas Gohr    /** @inheritdoc */
39be906b56SAndreas Gohr    public function connectTo($mode)
40be906b56SAndreas Gohr    {
41be906b56SAndreas Gohr        $this->Lexer->addEntryPattern('[ \t]*\n {2,}[\-\*]', $mode, 'listblock');
42be906b56SAndreas Gohr        $this->Lexer->addEntryPattern('[ \t]*\n\t{1,}[\-\*]', $mode, 'listblock');
43be906b56SAndreas Gohr
44be906b56SAndreas Gohr        $this->Lexer->addPattern('\n {2,}[\-\*]', 'listblock');
45be906b56SAndreas Gohr        $this->Lexer->addPattern('\n\t{1,}[\-\*]', 'listblock');
46be906b56SAndreas Gohr    }
47be906b56SAndreas Gohr
48be906b56SAndreas Gohr    /** @inheritdoc */
49be906b56SAndreas Gohr    public function postConnect()
50be906b56SAndreas Gohr    {
51be906b56SAndreas Gohr        $this->Lexer->addExitPattern('\n', 'listblock');
52be906b56SAndreas Gohr    }
53be906b56SAndreas Gohr
54be906b56SAndreas Gohr    /** @inheritdoc */
5571096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
56be906b56SAndreas Gohr    {
5771096e46SAndreas Gohr        switch ($state) {
5871096e46SAndreas Gohr            case DOKU_LEXER_ENTER:
5971096e46SAndreas Gohr                $handler->setCallWriter(new Lists($handler->getCallWriter()));
6071096e46SAndreas Gohr                $handler->addCall('list_open', [$match], $pos);
6171096e46SAndreas Gohr                break;
6271096e46SAndreas Gohr            case DOKU_LEXER_EXIT:
6371096e46SAndreas Gohr                $handler->addCall('list_close', [], $pos);
6471096e46SAndreas Gohr                /** @var Lists $reWriter */
6571096e46SAndreas Gohr                $reWriter = $handler->getCallWriter();
6671096e46SAndreas Gohr                $handler->setCallWriter($reWriter->process());
6771096e46SAndreas Gohr                break;
6871096e46SAndreas Gohr            case DOKU_LEXER_MATCHED:
6971096e46SAndreas Gohr                $handler->addCall('list_item', [$match], $pos);
7071096e46SAndreas Gohr                break;
7171096e46SAndreas Gohr            case DOKU_LEXER_UNMATCHED:
7271096e46SAndreas Gohr                $handler->addCall('cdata', [$match], $pos);
7371096e46SAndreas Gohr                break;
7471096e46SAndreas Gohr        }
7571096e46SAndreas Gohr        return true;
76be906b56SAndreas Gohr    }
77be906b56SAndreas Gohr}
78