OpenAPI Generator. Better DocBlock parsing [WIP]
[dokuwiki.git] / inc / Parsing / ParserMode / Acronym.php
blobb80df488c7fd3f115e29d676c9a6aeb3359ada3a
1 <?php
3 namespace dokuwiki\Parsing\ParserMode;
5 class Acronym extends AbstractMode
7 // A list
8 protected $acronyms = [];
9 protected $pattern = '';
11 /**
12 * Acronym constructor.
14 * @param string[] $acronyms
16 public function __construct($acronyms)
18 usort($acronyms, [$this, 'compare']);
19 $this->acronyms = $acronyms;
22 /** @inheritdoc */
23 public function preConnect()
25 if (!count($this->acronyms)) return;
27 $bound = '[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]';
28 $acronyms = array_map(['\\dokuwiki\\Parsing\\Lexer\\Lexer', 'escape'], $this->acronyms);
29 $this->pattern = '(?<=^|' . $bound . ')(?:' . implode('|', $acronyms) . ')(?=' . $bound . ')';
32 /** @inheritdoc */
33 public function connectTo($mode)
35 if (!count($this->acronyms)) return;
37 if (strlen($this->pattern) > 0) {
38 $this->Lexer->addSpecialPattern($this->pattern, $mode, 'acronym');
42 /** @inheritdoc */
43 public function getSort()
45 return 240;
48 /**
49 * sort callback to order by string length descending
51 * @param string $a
52 * @param string $b
54 * @return int
56 protected function compare($a, $b)
58 $a_len = strlen($a);
59 $b_len = strlen($b);
60 if ($a_len > $b_len) {
61 return -1;
62 } elseif ($a_len < $b_len) {
63 return 1;
66 return 0;