xref: /dokuwiki/inc/Parsing/ParserMode/Acronym.php (revision 90fb952c4c30c09c8446076ba05991c89a3f0b01)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
5be906b56SAndreas Gohrclass Acronym extends AbstractMode
6be906b56SAndreas Gohr{
7be906b56SAndreas Gohr    // A list
8*bcaec9f4SAndreas Gohr    protected $acronyms = [];
9be906b56SAndreas Gohr    protected $pattern = '';
10be906b56SAndreas Gohr
11be906b56SAndreas Gohr    /**
12be906b56SAndreas Gohr     * Acronym constructor.
13be906b56SAndreas Gohr     *
14be906b56SAndreas Gohr     * @param string[] $acronyms
15be906b56SAndreas Gohr     */
16be906b56SAndreas Gohr    public function __construct($acronyms)
17be906b56SAndreas Gohr    {
18*bcaec9f4SAndreas Gohr        usort($acronyms, [$this, 'compare']);
19be906b56SAndreas Gohr        $this->acronyms = $acronyms;
20be906b56SAndreas Gohr    }
21be906b56SAndreas Gohr
22be906b56SAndreas Gohr    /** @inheritdoc */
23be906b56SAndreas Gohr    public function preConnect()
24be906b56SAndreas Gohr    {
25be906b56SAndreas Gohr        if (!count($this->acronyms)) return;
26be906b56SAndreas Gohr
27be906b56SAndreas Gohr        $bound = '[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]';
28be906b56SAndreas Gohr        $acronyms = array_map(['\\dokuwiki\\Parsing\\Lexer\\Lexer', 'escape'], $this->acronyms);
29*bcaec9f4SAndreas Gohr        $this->pattern = '(?<=^|' . $bound . ')(?:' . implode('|', $acronyms) . ')(?=' . $bound . ')';
30be906b56SAndreas Gohr    }
31be906b56SAndreas Gohr
32be906b56SAndreas Gohr    /** @inheritdoc */
33be906b56SAndreas Gohr    public function connectTo($mode)
34be906b56SAndreas Gohr    {
35be906b56SAndreas Gohr        if (!count($this->acronyms)) return;
36be906b56SAndreas Gohr
37be906b56SAndreas Gohr        if (strlen($this->pattern) > 0) {
38be906b56SAndreas Gohr            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'acronym');
39be906b56SAndreas Gohr        }
40be906b56SAndreas Gohr    }
41be906b56SAndreas Gohr
42be906b56SAndreas Gohr    /** @inheritdoc */
43be906b56SAndreas Gohr    public function getSort()
44be906b56SAndreas Gohr    {
45be906b56SAndreas Gohr        return 240;
46be906b56SAndreas Gohr    }
47be906b56SAndreas Gohr
48be906b56SAndreas Gohr    /**
49be906b56SAndreas Gohr     * sort callback to order by string length descending
50be906b56SAndreas Gohr     *
51be906b56SAndreas Gohr     * @param string $a
52be906b56SAndreas Gohr     * @param string $b
53be906b56SAndreas Gohr     *
54be906b56SAndreas Gohr     * @return int
55be906b56SAndreas Gohr     */
56be906b56SAndreas Gohr    protected function compare($a, $b)
57be906b56SAndreas Gohr    {
58be906b56SAndreas Gohr        $a_len = strlen($a);
59be906b56SAndreas Gohr        $b_len = strlen($b);
60be906b56SAndreas Gohr        if ($a_len > $b_len) {
61be906b56SAndreas Gohr            return -1;
62be906b56SAndreas Gohr        } elseif ($a_len < $b_len) {
63be906b56SAndreas Gohr            return 1;
64be906b56SAndreas Gohr        }
65be906b56SAndreas Gohr
66be906b56SAndreas Gohr        return 0;
67be906b56SAndreas Gohr    }
68be906b56SAndreas Gohr}
69