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