xref: /dokuwiki/inc/Parsing/ParserMode/Smiley.php (revision 71096e46fcbfaeaa808667aba794e77fe2780169)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6use dokuwiki\Parsing\Lexer\Lexer;
7
8class Smiley extends AbstractMode
9{
10    protected $smileys = [];
11    protected $pattern = '';
12
13    /**
14     * Smiley constructor.
15     * @param string[] $smileys
16     */
17    public function __construct($smileys)
18    {
19        $this->smileys = $smileys;
20    }
21
22    /** @inheritdoc */
23    public function getSort()
24    {
25        return 230;
26    }
27
28    /** @inheritdoc */
29    public function preConnect()
30    {
31        if (!count($this->smileys) || $this->pattern != '') return;
32
33        $sep = '';
34        foreach ($this->smileys as $smiley) {
35            $this->pattern .= $sep . '(?<=\W|^)' . Lexer::escape($smiley) . '(?=\W|$)';
36            $sep = '|';
37        }
38    }
39
40    /** @inheritdoc */
41    public function connectTo($mode)
42    {
43        if (!count($this->smileys)) return;
44
45        if ((string) $this->pattern !== '') {
46            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'smiley');
47        }
48    }
49
50    /** @inheritdoc */
51    public function handle($match, $state, $pos, Handler $handler)
52    {
53        $handler->addCall('smiley', [$match], $pos);
54        return true;
55    }
56}
57