xref: /dokuwiki/inc/Parsing/ParserMode/Footnote.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\Nest;
7c8dd1b9dSAndreas Gohruse dokuwiki\Parsing\ModeRegistry;
8c8dd1b9dSAndreas Gohr
9be906b56SAndreas Gohrclass Footnote extends AbstractMode
10be906b56SAndreas Gohr{
11be906b56SAndreas Gohr    /**
12be906b56SAndreas Gohr     * Footnote constructor.
13be906b56SAndreas Gohr     */
14be906b56SAndreas Gohr    public function __construct()
15be906b56SAndreas Gohr    {
16c8dd1b9dSAndreas Gohr        $this->allowedModes = ModeRegistry::getInstance()->getModesForCategories([
17c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_CONTAINER,
18c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_FORMATTING,
19*56c730b5SAndreas Gohr            ModeRegistry::CATEGORY_SUBSTITUTION,
20c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_PROTECTED,
21c8dd1b9dSAndreas Gohr            ModeRegistry::CATEGORY_DISABLED,
22c8dd1b9dSAndreas Gohr        ]);
23be906b56SAndreas Gohr
24be906b56SAndreas Gohr        unset($this->allowedModes[array_search('footnote', $this->allowedModes)]);
25be906b56SAndreas Gohr    }
26be906b56SAndreas Gohr
27be906b56SAndreas Gohr    /** @inheritdoc */
2871096e46SAndreas Gohr    public function getSort()
2971096e46SAndreas Gohr    {
3071096e46SAndreas Gohr        return 150;
3171096e46SAndreas Gohr    }
3271096e46SAndreas Gohr
3371096e46SAndreas Gohr    /** @inheritdoc */
34be906b56SAndreas Gohr    public function connectTo($mode)
35be906b56SAndreas Gohr    {
36be906b56SAndreas Gohr        $this->Lexer->addEntryPattern(
37be906b56SAndreas Gohr            '\x28\x28(?=.*\x29\x29)',
38be906b56SAndreas Gohr            $mode,
39be906b56SAndreas Gohr            'footnote'
40be906b56SAndreas Gohr        );
41be906b56SAndreas Gohr    }
42be906b56SAndreas Gohr
43be906b56SAndreas Gohr    /** @inheritdoc */
44be906b56SAndreas Gohr    public function postConnect()
45be906b56SAndreas Gohr    {
46be906b56SAndreas Gohr        $this->Lexer->addExitPattern(
47be906b56SAndreas Gohr            '\x29\x29',
48be906b56SAndreas Gohr            'footnote'
49be906b56SAndreas Gohr        );
50be906b56SAndreas Gohr    }
51be906b56SAndreas Gohr
52be906b56SAndreas Gohr    /** @inheritdoc */
5371096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
54be906b56SAndreas Gohr    {
5571096e46SAndreas Gohr        switch ($state) {
5671096e46SAndreas Gohr            case DOKU_LEXER_ENTER:
5771096e46SAndreas Gohr                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
5871096e46SAndreas Gohr                // we will still enter a new footnote mode, we just do nothing
5971096e46SAndreas Gohr                if ($handler->getStatus('footnote')) {
6071096e46SAndreas Gohr                    $handler->addCall('cdata', [$match], $pos);
6171096e46SAndreas Gohr                    break;
6271096e46SAndreas Gohr                }
6371096e46SAndreas Gohr                $handler->setStatus('footnote', true);
6471096e46SAndreas Gohr
6571096e46SAndreas Gohr                $handler->setCallWriter(new Nest($handler->getCallWriter(), 'footnote_close'));
6671096e46SAndreas Gohr                $handler->addCall('footnote_open', [], $pos);
6771096e46SAndreas Gohr                break;
6871096e46SAndreas Gohr            case DOKU_LEXER_EXIT:
6971096e46SAndreas Gohr                // check whether we have already exited the footnote mode, can happen if the modes were nested
7071096e46SAndreas Gohr                if (!$handler->getStatus('footnote')) {
7171096e46SAndreas Gohr                    $handler->addCall('cdata', [$match], $pos);
7271096e46SAndreas Gohr                    break;
7371096e46SAndreas Gohr                }
7471096e46SAndreas Gohr
7571096e46SAndreas Gohr                $handler->setStatus('footnote', false);
7671096e46SAndreas Gohr                $handler->addCall('footnote_close', [], $pos);
7771096e46SAndreas Gohr
7871096e46SAndreas Gohr                /** @var Nest $reWriter */
7971096e46SAndreas Gohr                $reWriter = $handler->getCallWriter();
8071096e46SAndreas Gohr                $handler->setCallWriter($reWriter->process());
8171096e46SAndreas Gohr                break;
8271096e46SAndreas Gohr            case DOKU_LEXER_UNMATCHED:
8371096e46SAndreas Gohr                $handler->addCall('cdata', [$match], $pos);
8471096e46SAndreas Gohr                break;
8571096e46SAndreas Gohr        }
8671096e46SAndreas Gohr        return true;
87be906b56SAndreas Gohr    }
88be906b56SAndreas Gohr}
89