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