xref: /dokuwiki/inc/Parsing/ParserMode/Quotes.php (revision 71096e46fcbfaeaa808667aba794e77fe2780169)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6
7class Quotes extends AbstractMode
8{
9    /** @inheritdoc */
10    public function getSort()
11    {
12        return 280;
13    }
14
15    /** @inheritdoc */
16    public function connectTo($mode)
17    {
18        global $conf;
19
20        $ws   =  '\s/\#~:+=&%@\-\x28\x29\]\[{}><"\'';   // whitespace
21        $punc =  ';,\.?!';
22
23        if ($conf['typography'] == 2) {
24            $this->Lexer->addSpecialPattern(
25                "(?<=^|[$ws])'(?=[^$ws$punc])",
26                $mode,
27                'singlequoteopening'
28            );
29            $this->Lexer->addSpecialPattern(
30                "(?<=^|[^$ws]|[$punc])'(?=$|[$ws$punc])",
31                $mode,
32                'singlequoteclosing'
33            );
34            $this->Lexer->addSpecialPattern(
35                "(?<=^|[^$ws$punc])'(?=$|[^$ws$punc])",
36                $mode,
37                'apostrophe'
38            );
39        }
40
41        $this->Lexer->addSpecialPattern(
42            "(?<=^|[$ws])\"(?=[^$ws$punc])",
43            $mode,
44            'doublequoteopening'
45        );
46        $this->Lexer->addSpecialPattern(
47            "\"",
48            $mode,
49            'doublequoteclosing'
50        );
51    }
52
53    /** @inheritdoc */
54    public function postConnect()
55    {
56        // Map all sub-mode names back to 'quotes' so the Handler
57        // dispatches them to this mode object
58        $this->Lexer->mapHandler('singlequoteopening', 'quotes');
59        $this->Lexer->mapHandler('singlequoteclosing', 'quotes');
60        $this->Lexer->mapHandler('apostrophe', 'quotes');
61        $this->Lexer->mapHandler('doublequoteopening', 'quotes');
62        $this->Lexer->mapHandler('doublequoteclosing', 'quotes');
63    }
64
65    /** @inheritdoc */
66    public function handle($match, $state, $pos, Handler $handler)
67    {
68        $call = $handler->getModeName();
69
70        if ($call === 'doublequoteclosing' && $handler->getStatus('doublequote') <= 0) {
71            $call = 'doublequoteopening';
72        }
73
74        if ($call === 'doublequoteopening') {
75            $handler->setStatus('doublequote', $handler->getStatus('doublequote') + 1);
76        } elseif ($call === 'doublequoteclosing') {
77            $handler->setStatus('doublequote', max(0, $handler->getStatus('doublequote') - 1));
78        }
79
80        $handler->addCall($call, [], $pos);
81        return true;
82    }
83}
84