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