xref: /dokuwiki/inc/Parsing/ParserMode/Header.php (revision 71096e46fcbfaeaa808667aba794e77fe2780169)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6
7class Header extends AbstractMode
8{
9    /** @inheritdoc */
10    public function getSort()
11    {
12        return 50;
13    }
14
15    /** @inheritdoc */
16    public function connectTo($mode)
17    {
18        //we're not picky about the closing ones, two are enough
19        $this->Lexer->addSpecialPattern(
20            '[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)',
21            $mode,
22            'header'
23        );
24    }
25
26    /** @inheritdoc */
27    public function handle($match, $state, $pos, Handler $handler)
28    {
29        // get level and title
30        $title = trim($match);
31        $level = 7 - strspn($title, '=');
32        if ($level < 1) $level = 1;
33        $title = trim($title, '=');
34        $title = trim($title);
35
36        if ($handler->getStatus('section')) $handler->addCall('section_close', [], $pos);
37
38        $handler->addCall('header', [$title, $level, $pos], $pos);
39
40        $handler->addCall('section_open', [$level], $pos);
41        $handler->setStatus('section', true);
42        return true;
43    }
44}
45