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