1<?php 2 3namespace dokuwiki\Parsing; 4 5use dokuwiki\Extension\PluginInterface; 6use dokuwiki\Extension\SyntaxPlugin; 7use dokuwiki\Parsing\ParserMode\Acronym; 8use dokuwiki\Parsing\ParserMode\ModeInterface; 9use dokuwiki\Parsing\ParserMode\Camelcaselink; 10use dokuwiki\Parsing\ParserMode\Entity; 11use dokuwiki\Parsing\ParserMode\Smiley; 12 13/** 14 * Central registry for parser mode categories and mode instantiation. 15 * 16 * The underlying data is kept in the global $PARSER_MODES array because 17 * third-party plugins read and write it directly at runtime (e.g. to register 18 * their mode in a category). All methods in this class operate on that global 19 * so changes are visible to both old and new code. 20 */ 21class ModeRegistry 22{ 23 // Category constants (preserving the historical 'substition' typo) 24 public const CATEGORY_CONTAINER = 'container'; 25 public const CATEGORY_BASEONLY = 'baseonly'; 26 public const CATEGORY_FORMATTING = 'formatting'; 27 public const CATEGORY_SUBSTITION = 'substition'; 28 public const CATEGORY_PROTECTED = 'protected'; 29 public const CATEGORY_DISABLED = 'disabled'; 30 public const CATEGORY_PARAGRAPHS = 'paragraphs'; 31 32 /** @var array{sort: int, mode: string, obj: ModeInterface}[]|null */ 33 private ?array $modes = null; 34 35 /** @var string[] Modes that handle their own line endings (skip EOL connection) */ 36 private array $blockEolModes = []; 37 38 /** @var array<string, string[]> Mode name => regex-escaped line start marker characters */ 39 private array $lineStartMarkers = []; 40 41 private static ?self $instance = null; 42 43 /** 44 * Get the singleton instance of the ModeRegistry. 45 * 46 * @return self 47 */ 48 public static function getInstance(): self 49 { 50 if (!self::$instance instanceof self) { 51 self::$instance = new self(); 52 } 53 return self::$instance; 54 } 55 56 /** 57 * Reset the singleton instance. 58 * 59 * This is mainly useful for testing to force re-initialization. 60 * 61 * @return void 62 */ 63 public static function reset(): void 64 { 65 self::$instance = null; 66 } 67 68 /** 69 * Constructor. Initializes the global $PARSER_MODES array with the default mode categories. 70 */ 71 private function __construct() 72 { 73 global $PARSER_MODES; 74 $PARSER_MODES = [ 75 self::CATEGORY_CONTAINER => ['listblock', 'table', 'quote', 'hr'], 76 self::CATEGORY_BASEONLY => ['header'], 77 self::CATEGORY_FORMATTING => [ 78 'strong', 'emphasis', 'underline', 'monospace', 79 'subscript', 'superscript', 'deleted', 'footnote', 80 'gfm_emphasis', 'gfm_emphasis_underscore', 'gfm_strong_underscore', 81 ], 82 self::CATEGORY_SUBSTITION => [ 83 'acronym', 'smiley', 'wordblock', 'entity', 84 'camelcaselink', 'internallink', 'media', 'externallink', 85 'linebreak', 'emaillink', 'windowssharelink', 'filelink', 86 'notoc', 'nocache', 'multiplyentity', 'quotes', 'rss', 87 ], 88 self::CATEGORY_PROTECTED => ['preformatted', 'code', 'file'], 89 self::CATEGORY_DISABLED => ['unformatted'], 90 self::CATEGORY_PARAGRAPHS => ['eol'], 91 ]; 92 } 93 94 /** 95 * Get all mode names in the given categories. 96 * 97 * @param string[] $categories One or more CATEGORY_* constants 98 * @return string[] Unique list of mode names 99 */ 100 public function getModesForCategories(array $categories): array 101 { 102 global $PARSER_MODES; 103 $modes = []; 104 foreach ($categories as $cat) { 105 if (isset($PARSER_MODES[$cat])) { 106 $modes = array_merge($modes, $PARSER_MODES[$cat]); 107 } 108 } 109 return array_unique($modes); 110 } 111 112 /** 113 * Get the raw categories array. 114 * 115 * @return array<string, string[]> Category name => list of mode names 116 */ 117 public function getCategories(): array 118 { 119 global $PARSER_MODES; 120 return $PARSER_MODES; 121 } 122 123 /** 124 * Register a mode in a category. 125 * 126 * @param string $category One of the CATEGORY_* constants 127 * @param string $modeName The mode name to register 128 * @return void 129 */ 130 public function registerMode(string $category, string $modeName): void 131 { 132 global $PARSER_MODES; 133 $PARSER_MODES[$category][] = $modeName; 134 $this->modes = null; // invalidate cached mode list 135 } 136 137 /** 138 * Register a mode that handles its own line endings. 139 * Modes registered here will be skipped by Eol's connectTo(). 140 * 141 * @param string $mode The mode name 142 * @return void 143 */ 144 public function registerBlockEolMode(string $mode): void 145 { 146 $this->blockEolModes[] = $mode; 147 } 148 149 /** 150 * Get all modes that handle their own line endings. 151 * 152 * @return string[] 153 */ 154 public function getBlockEolModes(): array 155 { 156 return $this->blockEolModes; 157 } 158 159 /** 160 * Register regex-escaped line start marker characters for a mode. 161 * Preformatted uses these to build a negative lookahead. 162 * 163 * @param string $mode The mode name 164 * @param string[] $markers Regex-escaped marker characters (e.g. ['\\*', '\\-']) 165 * @return void 166 */ 167 public function registerLineStartMarkers(string $mode, array $markers): void 168 { 169 $this->lineStartMarkers[$mode] = $markers; 170 } 171 172 /** 173 * Get all registered line start markers, merged and deduplicated. 174 * 175 * @return string[] 176 */ 177 public function getLineStartMarkers(): array 178 { 179 if (!$this->lineStartMarkers) return []; 180 return array_unique(array_merge(...array_values($this->lineStartMarkers))); 181 } 182 183 /** 184 * Get all parser modes, fully instantiated and sorted by priority. 185 * 186 * This includes syntax plugins, built-in modes, formatting modes, and 187 * data-driven modes (smileys, acronyms, entities). Results are cached 188 * unless running in a test environment. 189 * 190 * @return array[] Each entry is ['sort' => int, 'mode' => string, 'obj' => ModeInterface] 191 */ 192 public function getModes(): array 193 { 194 global $conf; 195 196 if ($this->modes !== null && !defined('DOKU_UNITTEST')) { 197 return $this->modes; 198 } 199 200 $this->modes = []; 201 $syntax = $conf['syntax'] ?? 'dokuwiki'; 202 $loadDw = in_array($syntax, ['dokuwiki', 'dw+md', 'md+dw']); 203 $loadMd = in_array($syntax, ['markdown', 'dw+md', 'md+dw']); 204 205 $this->loadPluginModes(); 206 $this->loadAlwaysModes(); 207 if ($loadDw) $this->loadDokuWikiModes(); 208 if ($loadMd) $this->loadMarkdownModes(); 209 $this->loadDataModes(); 210 211 usort($this->modes, self::sortModes(...)); 212 return $this->modes; 213 } 214 215 /** 216 * Load syntax plugin modes and register them in their categories. 217 */ 218 protected function loadPluginModes(): void 219 { 220 global $PARSER_MODES; 221 222 $plugins = plugin_list('syntax'); 223 foreach ($plugins as $p) { 224 $obj = plugin_load('syntax', $p); 225 if (!$obj instanceof PluginInterface) continue; 226 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; 227 $this->modes[] = [ 228 'sort' => $obj->getSort(), 229 'mode' => "plugin_$p", 230 'obj' => $obj, 231 ]; 232 unset($obj); 233 } 234 } 235 236 /** 237 * Load modes that have no equivalent in the other syntax. 238 * These are always active regardless of the syntax setting. 239 */ 240 protected function loadAlwaysModes(): void 241 { 242 global $conf; 243 244 $modes = [ 245 'strong', 'monospace', 'subscript', 'superscript', 246 'footnote', 'eol', 'unformatted', 'preformatted', 'file', 247 'quote', 'externallink', 'emaillink', 'windowssharelink', 248 'notoc', 'nocache', 'rss', 249 ]; 250 251 if ($conf['typography']) { 252 $modes[] = 'quotes'; 253 $modes[] = 'multiplyentity'; 254 } 255 256 $this->instantiateModes($modes); 257 } 258 259 /** 260 * Load DokuWiki-specific modes for features that also exist in Markdown. 261 * Skipped when syntax is 'markdown'. 262 */ 263 protected function loadDokuWikiModes(): void 264 { 265 global $conf; 266 $syntax = $conf['syntax'] ?? 'dokuwiki'; 267 $dwPreferred = in_array($syntax, ['dokuwiki', 'dw+md'], true); 268 269 $modes = [ 270 'emphasis', 'deleted', 'code', 'header', 'hr', 271 'linebreak', 'internallink', 'media', 'listblock', 'table', 272 ]; 273 274 // Underline only loads when DokuWiki is preferred. In MD-preferred 275 // modes, `__` means strong (via gfm_strong_underscore) and loading 276 // Underline here would conflict. 277 if ($dwPreferred) { 278 $modes[] = 'underline'; 279 } 280 281 $this->instantiateModes($modes); 282 } 283 284 /** 285 * Load Markdown-specific modes for features that also exist in DokuWiki. 286 * Skipped when syntax is 'dokuwiki'. 287 */ 288 protected function loadMarkdownModes(): void 289 { 290 global $conf; 291 $syntax = $conf['syntax'] ?? 'dokuwiki'; 292 $mdPreferred = in_array($syntax, ['markdown', 'md+dw'], true); 293 294 $modes = ['gfm_emphasis']; 295 296 // Underscore-based emphasis and strong only load when Markdown is 297 // preferred. In DW-preferred modes, `__` means underline and loading 298 // these would conflict. 299 if ($mdPreferred) { 300 $modes[] = 'gfm_emphasis_underscore'; 301 $modes[] = 'gfm_strong_underscore'; 302 } 303 304 $this->instantiateModes($modes); 305 } 306 307 /** 308 * Load data-driven modes that require constructor arguments 309 * (smileys, acronyms, entities) and optional config-gated modes. 310 */ 311 protected function loadDataModes(): void 312 { 313 global $conf; 314 315 $obj = new Smiley(array_keys(getSmileys())); 316 $this->modes[] = ['sort' => $obj->getSort(), 'mode' => 'smiley', 'obj' => $obj]; 317 318 $obj = new Acronym(array_keys(getAcronyms())); 319 $this->modes[] = ['sort' => $obj->getSort(), 'mode' => 'acronym', 'obj' => $obj]; 320 321 $obj = new Entity(array_keys(getEntities())); 322 $this->modes[] = ['sort' => $obj->getSort(), 'mode' => 'entity', 'obj' => $obj]; 323 324 if (!empty($conf['camelcase'])) { 325 $obj = new Camelcaselink(); 326 $this->modes[] = ['sort' => $obj->getSort(), 'mode' => 'camelcaselink', 'obj' => $obj]; 327 } 328 } 329 330 /** 331 * Instantiate mode classes by name and add them to the mode list. 332 * 333 * Mode names are split on `_` and each segment is PascalCased to form the 334 * class name (e.g. `gfm_emphasis_underscore` → `GfmEmphasisUnderscore`, 335 * `internallink` → `Internallink`, `strong` → `Strong`). 336 * 337 * @param string[] $modeNames 338 */ 339 protected function instantiateModes(array $modeNames): void 340 { 341 foreach ($modeNames as $mode) { 342 $class = implode('', array_map('ucfirst', explode('_', $mode))); // snake_case to PascalCase 343 $class = 'dokuwiki\\Parsing\\ParserMode\\' . $class; // prepend namespace 344 $obj = new $class(); 345 $this->modes[] = [ 346 'sort' => $obj->getSort(), 347 'mode' => $mode, 348 'obj' => $obj, 349 ]; 350 } 351 } 352 353 /** 354 * Callback function for usort 355 * 356 * @param array $a 357 * @param array $b 358 * @return int 359 */ 360 public static function sortModes(array $a, array $b): int 361 { 362 return $a['sort'] <=> $b['sort']; 363 } 364} 365