| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\SyntaxHighlight; |
| 20 |
|
| 21 |
use MediaWiki\Api\Hook\ApiFormatHighlightHook; |
| 22 |
use MediaWiki\Content\Content; |
| 23 |
use MediaWiki\Content\Hook\ContentGetParserOutputHook; |
| 24 |
use MediaWiki\Content\TextContent; |
| 25 |
use MediaWiki\Context\IContextSource; |
| 26 |
use MediaWiki\Hook\ParserFirstCallInitHook; |
| 27 |
use MediaWiki\Hook\SoftwareInfoHook; |
| 28 |
use MediaWiki\Html\Html; |
| 29 |
use MediaWiki\Json\FormatJson; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Parser\Parser; |
| 33 |
use MediaWiki\Parser\ParserOptions; |
| 34 |
use MediaWiki\Parser\ParserOutput; |
| 35 |
use MediaWiki\Parser\Sanitizer; |
| 36 |
use MediaWiki\Registration\ExtensionRegistry; |
| 37 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook; |
| 38 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 39 |
use MediaWiki\Status\Status; |
| 40 |
use MediaWiki\Title\Title; |
| 41 |
use RuntimeException; |
| 42 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 43 |
use Wikimedia\Parsoid\Core\ContentMetadataCollectorStringSets as CMCSS; |
| 44 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 45 |
use Wikimedia\Parsoid\Ext\ExtensionTagHandler; |
| 46 |
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 47 |
|
| 48 |
class SyntaxHighlight extends ExtensionTagHandler implements |
| 49 |
ParserFirstCallInitHook, |
| 50 |
ContentGetParserOutputHook, |
| 51 |
ResourceLoaderRegisterModulesHook, |
| 52 |
ApiFormatHighlightHook, |
| 53 |
SoftwareInfoHook |
| 54 |
{ |
| 55 |
|
| 56 |
|
| 57 |
public const HIGHLIGHT_CSS_CLASS = 'mw-highlight'; |
| 58 |
|
| 59 |
|
| 60 |
private const CACHE_VERSION = 2; |
| 61 |
|
| 62 |
|
| 63 |
private static $mimeLexers = [ |
| 64 |
'text/javascript' => 'javascript', |
| 65 |
'application/json' => 'javascript', |
| 66 |
'text/xml' => 'xml', |
| 67 |
]; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private static function getMaxLines(): int { |
| 75 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 76 |
return $config->get( 'SyntaxHighlightMaxLines' ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private static function getMaxBytes(): int { |
| 85 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 86 |
return $config->get( 'SyntaxHighlightMaxBytes' ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private static function getLexer( $lang ) { |
| 96 |
static $lexers = null; |
| 97 |
|
| 98 |
if ( $lang === null ) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
$lexers ??= Pygmentize::getLexers(); |
| 103 |
|
| 104 |
$lexer = strtolower( $lang ); |
| 105 |
|
| 106 |
if ( isset( $lexers[$lexer] ) ) { |
| 107 |
return $lexer; |
| 108 |
} |
| 109 |
|
| 110 |
$geshi2pygments = SyntaxHighlightGeSHiCompat::getGeSHiToPygmentsMap(); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if ( isset( $geshi2pygments[$lexer] ) ) { |
| 115 |
$lexer = $geshi2pygments[$lexer]; |
| 116 |
if ( isset( $lexers[$lexer] ) ) { |
| 117 |
return $lexer; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function onParserFirstCallInit( $parser ) { |
| 130 |
$parser->setHook( 'source', [ self::class, 'parserHookSource' ] ); |
| 131 |
$parser->setHook( 'syntaxhighlight', [ self::class, 'parserHook' ] ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static function parserHookSource( $text, $args, $parser ) { |
| 143 |
$parser->addTrackingCategory( 'syntaxhighlight-source-category' ); |
| 144 |
return self::parserHook( $text, $args, $parser ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private static function getModuleStyles(): array { |
| 151 |
return [ 'ext.pygments' ]; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private static function processContent( string $text, array $args, ?Parser $parser = null ): array { |
| 162 |
|
| 163 |
$out = rtrim( trim( $text, "\n" ) ); |
| 164 |
$trackingCats = []; |
| 165 |
|
| 166 |
|
| 167 |
if ( isset( $args['enclose'] ) ) { |
| 168 |
if ( $args['enclose'] === 'none' ) { |
| 169 |
$args['inline'] = true; |
| 170 |
} |
| 171 |
unset( $args['enclose'] ); |
| 172 |
$trackingCats[] = 'syntaxhighlight-enclose-category'; |
| 173 |
} |
| 174 |
|
| 175 |
$lexer = $args['lang'] ?? ''; |
| 176 |
|
| 177 |
$result = self::highlight( $out, $lexer, $args, $parser ); |
| 178 |
if ( !$result->isGood() ) { |
| 179 |
$trackingCats[] = 'syntaxhighlight-error-category'; |
| 180 |
} |
| 181 |
|
| 182 |
return [ 'html' => $result->getValue(), 'cats' => $trackingCats ]; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static function parserHook( $text, $args, $parser ) { |
| 194 |
|
| 195 |
$out = $parser->getStripState()->unstripNoWiki( $text ?? '' ); |
| 196 |
|
| 197 |
$result = self::processContent( $out, $args, $parser ); |
| 198 |
foreach ( $result['cats'] as $cat ) { |
| 199 |
$parser->addTrackingCategory( $cat ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
$parser->getOutput()->addModuleStyles( self::getModuleStyles() ); |
| 204 |
$parser->getOutput()->addModules( [ 'ext.pygments.view' ] ); |
| 205 |
return $result['html']; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
public function sourceToDom( |
| 210 |
ParsoidExtensionAPI $extApi, string $text, array $extArgs |
| 211 |
): ?DocumentFragment { |
| 212 |
$result = self::processContent( $text, $extApi->extArgsToArray( $extArgs ) ); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
$extApi->getMetadata()->appendOutputStrings( CMCSS::MODULE_STYLE, self::getModuleStyles() ); |
| 219 |
$extApi->getMetadata()->appendOutputStrings( CMCSS::MODULE, [ 'ext.pygments.view' ] ); |
| 220 |
|
| 221 |
return $extApi->htmlToDom( $result['html'] ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private static function unwrap( string $out ): string { |
| 231 |
if ( $out !== '' ) { |
| 232 |
$m = []; |
| 233 |
if ( preg_match( '/^<div class="?mw-highlight"?>(.*)<\/div>$/s', trim( $out ), $m ) ) { |
| 234 |
$out = trim( $m[1] ); |
| 235 |
} else { |
| 236 |
throw new RuntimeException( 'Unexpected output from Pygments encountered' ); |
| 237 |
} |
| 238 |
} |
| 239 |
return $out; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
private static function plainCodeWrap( $code, $isInline ) { |
| 248 |
if ( $isInline ) { |
| 249 |
return htmlspecialchars( $code, ENT_NOQUOTES ); |
| 250 |
} |
| 251 |
|
| 252 |
return Html::rawElement( |
| 253 |
'div', |
| 254 |
[ 'class' => self::HIGHLIGHT_CSS_CLASS ], |
| 255 |
Html::element( 'pre', [], $code ) |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private static function highlightInner( $code, $lang = null, $args = [], ?Parser $parser = null ) { |
| 267 |
$status = new Status; |
| 268 |
|
| 269 |
$lexer = self::getLexer( $lang ); |
| 270 |
if ( $lexer === null && $lang !== null ) { |
| 271 |
$status->warning( 'syntaxhighlight-error-unknown-language', $lang ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if ( $code === '' ) { |
| 276 |
$status->value = ''; |
| 277 |
return $status; |
| 278 |
} |
| 279 |
|
| 280 |
$length = strlen( $code ); |
| 281 |
if ( strlen( $code ) > self::getMaxBytes() ) { |
| 282 |
|
| 283 |
$lexer = null; |
| 284 |
$status->warning( |
| 285 |
'syntaxhighlight-error-exceeds-size-limit', |
| 286 |
$length, |
| 287 |
self::getMaxBytes() |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
$isInline = isset( $args['inline'] ); |
| 292 |
$showLines = isset( $args['line'] ); |
| 293 |
|
| 294 |
if ( $isInline ) { |
| 295 |
$code = trim( $code ); |
| 296 |
} |
| 297 |
|
| 298 |
if ( $lexer === null ) { |
| 299 |
|
| 300 |
$status->value = self::plainCodeWrap( $code, $isInline ); |
| 301 |
return $status; |
| 302 |
} |
| 303 |
|
| 304 |
if ( $parser && !$parser->incrementExpensiveFunctionCount() ) { |
| 305 |
|
| 306 |
$status->value = self::plainCodeWrap( $code, $isInline ); |
| 307 |
return $status; |
| 308 |
} |
| 309 |
|
| 310 |
$options = [ |
| 311 |
'cssclass' => self::HIGHLIGHT_CSS_CLASS, |
| 312 |
'encoding' => 'utf-8', |
| 313 |
]; |
| 314 |
|
| 315 |
|
| 316 |
if ( $showLines ) { |
| 317 |
$options['linenos'] = 'inline'; |
| 318 |
} |
| 319 |
|
| 320 |
if ( $lexer === 'php' && strpos( $code, '<?php' ) === false ) { |
| 321 |
$options['startinline'] = 1; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
if ( isset( $args['highlight'] ) ) { |
| 326 |
$lines = self::parseHighlightLines( $args['highlight'] ); |
| 327 |
if ( count( $lines ) ) { |
| 328 |
$options['hl_lines'] = implode( ' ', $lines ); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
if ( isset( $args['start'] ) && ctype_digit( $args['start'] ) ) { |
| 334 |
$options['linenostart'] = (int)$args['start']; |
| 335 |
} |
| 336 |
|
| 337 |
if ( |
| 338 |
!empty( $args['linelinks'] ) |
| 339 |
|
| 340 |
&& !str_contains( $args['linelinks'], ',' ) |
| 341 |
) { |
| 342 |
$options['linespans'] = htmlspecialchars( $args['linelinks'] ); |
| 343 |
} |
| 344 |
|
| 345 |
if ( $isInline ) { |
| 346 |
$options['nowrap'] = 1; |
| 347 |
} |
| 348 |
|
| 349 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 350 |
$error = null; |
| 351 |
$output = $cache->getWithSetCallback( |
| 352 |
$cache->makeGlobalKey( 'highlight', self::makeCacheKeyHash( $code, $lexer, $options ) ), |
| 353 |
$cache::TTL_MONTH, |
| 354 |
static function ( $oldValue, &$ttl ) use ( $code, $lexer, $options, &$error ) { |
| 355 |
try { |
| 356 |
return Pygmentize::highlight( $lexer, $code, $options ); |
| 357 |
} catch ( PygmentsException $e ) { |
| 358 |
$ttl = WANObjectCache::TTL_UNCACHEABLE; |
| 359 |
$error = $e->getMessage(); |
| 360 |
return null; |
| 361 |
} |
| 362 |
} |
| 363 |
); |
| 364 |
|
| 365 |
if ( $error !== null || $output === null ) { |
| 366 |
$status->warning( 'syntaxhighlight-error-pygments-invocation-failure' ); |
| 367 |
if ( $error !== null ) { |
| 368 |
wfWarn( 'Failed to invoke Pygments: ' . $error ); |
| 369 |
} else { |
| 370 |
wfWarn( 'Invoking Pygments returned blank output with no error response' ); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
$output = self::plainCodeWrap( $code, $isInline ); |
| 375 |
} |
| 376 |
|
| 377 |
$status->value = $output; |
| 378 |
|
| 379 |
return $status; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public static function highlight( $code, $lang = null, $args = [], ?Parser $parser = null ) { |
| 405 |
$status = self::highlightInner( $code, $lang, $args, $parser ); |
| 406 |
$output = $status->getValue(); |
| 407 |
|
| 408 |
$isInline = isset( $args['inline'] ); |
| 409 |
$showLines = isset( $args['line'] ); |
| 410 |
$lexer = self::getLexer( $lang ); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if ( $showLines ) { |
| 415 |
$lineReplace = Html::element( 'span', [ 'class' => 'linenos', 'data-line' => '$1' ] ); |
| 416 |
if ( !empty( $args['linelinks'] ) ) { |
| 417 |
$lineReplace = Html::rawElement( |
| 418 |
'a', |
| 419 |
[ 'href' => '#' . $args['linelinks'] . '-$1' ], |
| 420 |
$lineReplace |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
$output = preg_replace( |
| 427 |
'`<span class="linenos">\s*([^<]*)\s*</span>`', |
| 428 |
$lineReplace, |
| 429 |
$output |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
$htmlAttribs = Sanitizer::validateAttributes( |
| 435 |
$args, array_flip( [ 'style', 'class', 'id' ] ) |
| 436 |
); |
| 437 |
|
| 438 |
$dir = ( isset( $args['dir'] ) && $args['dir'] === 'rtl' ) ? 'rtl' : 'ltr'; |
| 439 |
|
| 440 |
|
| 441 |
$classList = []; |
| 442 |
if ( isset( $htmlAttribs['class'] ) ) { |
| 443 |
$classList[] = $htmlAttribs['class']; |
| 444 |
} |
| 445 |
$classList[] = self::HIGHLIGHT_CSS_CLASS; |
| 446 |
if ( $lexer !== null ) { |
| 447 |
$classList[] = self::HIGHLIGHT_CSS_CLASS . '-lang-' . $lexer; |
| 448 |
} |
| 449 |
$classList[] = 'mw-content-' . $dir; |
| 450 |
if ( $showLines ) { |
| 451 |
$classList[] = self::HIGHLIGHT_CSS_CLASS . '-lines'; |
| 452 |
} |
| 453 |
if ( !$isInline && isset( $args['copy'] ) ) { |
| 454 |
$classList[] = 'mw-highlight-copy'; |
| 455 |
} |
| 456 |
$htmlAttribs['class'] = implode( ' ', $classList ); |
| 457 |
$htmlAttribs['dir'] = $dir; |
| 458 |
'@phan-var array{class:string,dir:string} $htmlAttribs'; |
| 459 |
|
| 460 |
if ( $isInline ) { |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
$output = trim( $output ); |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
$output = str_replace( "\n", ' ', $output ); |
| 470 |
$output = Html::rawElement( 'code', $htmlAttribs, $output ); |
| 471 |
} else { |
| 472 |
$output = self::unwrap( $output ); |
| 473 |
|
| 474 |
if ( $parser ) { |
| 475 |
|
| 476 |
|
| 477 |
$marker = $parser::MARKER_PREFIX . '-syntaxhighlightinner-' . |
| 478 |
sprintf( '%08X', $parser->mMarkerIndex++ ) . $parser::MARKER_SUFFIX; |
| 479 |
$parser->getStripState()->addNoWiki( $marker, $output ); |
| 480 |
$output = $marker; |
| 481 |
} |
| 482 |
|
| 483 |
$output = Html::openElement( 'div', $htmlAttribs ) . |
| 484 |
$output . |
| 485 |
Html::closeElement( 'div' ); |
| 486 |
} |
| 487 |
|
| 488 |
$status->value = $output; |
| 489 |
return $status; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
private static function makeCacheKeyHash( $code, $lexer, $options ) { |
| 501 |
$optionString = FormatJson::encode( $options, false, FormatJson::ALL_OK ); |
| 502 |
return md5( "{$code}|{$lexer}|{$optionString}|" . self::CACHE_VERSION ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
protected static function parseHighlightLines( $lineSpec ) { |
| 515 |
$lines = []; |
| 516 |
$values = array_map( 'trim', explode( ',', $lineSpec ) ); |
| 517 |
foreach ( $values as $value ) { |
| 518 |
if ( ctype_digit( $value ) ) { |
| 519 |
$lines[] = (int)$value; |
| 520 |
} elseif ( strpos( $value, '-' ) !== false ) { |
| 521 |
[ $start, $end ] = array_map( 'intval', explode( '-', $value ) ); |
| 522 |
if ( self::validHighlightRange( $start, $end ) ) { |
| 523 |
for ( $i = $start; $i <= $end; $i++ ) { |
| 524 |
$lines[] = $i; |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
if ( count( $lines ) > self::getMaxLines() ) { |
| 529 |
$lines = array_slice( $lines, 0, self::getMaxLines() ); |
| 530 |
break; |
| 531 |
} |
| 532 |
} |
| 533 |
return $lines; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
protected static function validHighlightRange( $start, $end ) { |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
return $start > 0 && |
| 549 |
$start < $end && |
| 550 |
$end - $start < self::getMaxLines(); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public function onContentGetParserOutput( $content, $title, |
| 567 |
$revId, $options, $generateHtml, &$parserOutput |
| 568 |
) { |
| 569 |
|
| 570 |
if ( !( $content instanceof TextContent ) ) { |
| 571 |
|
| 572 |
return true; |
| 573 |
} |
| 574 |
|
| 575 |
if ( !$generateHtml ) { |
| 576 |
|
| 577 |
return true; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
$extension = ExtensionRegistry::getInstance(); |
| 584 |
$models = $extension->getAttribute( 'SyntaxHighlightModels' ) + [ |
| 585 |
CONTENT_MODEL_CSS => 'css', |
| 586 |
CONTENT_MODEL_JAVASCRIPT => 'javascript', |
| 587 |
]; |
| 588 |
$model = $content->getModel(); |
| 589 |
if ( !isset( $models[$model] ) ) { |
| 590 |
|
| 591 |
return true; |
| 592 |
} |
| 593 |
$lexer = $models[$model]; |
| 594 |
$text = $content->getText(); |
| 595 |
|
| 596 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 597 |
|
| 598 |
|
| 599 |
if ( in_array( $model, $config->get( MainConfigNames::TextModelsToParse ), true ) ) { |
| 600 |
$parserOutput = MediaWikiServices::getInstance()->getParser() |
| 601 |
->parse( $text, $title, $options, true, true, $revId ); |
| 602 |
} |
| 603 |
|
| 604 |
$status = self::highlight( $text, $lexer, [ 'line' => true, 'linelinks' => 'L' ] ); |
| 605 |
if ( !$status->isOK() ) { |
| 606 |
return true; |
| 607 |
} |
| 608 |
$out = $status->getValue(); |
| 609 |
|
| 610 |
$parserOutput->addModuleStyles( self::getModuleStyles() ); |
| 611 |
$parserOutput->addModules( [ 'ext.pygments.view' ] ); |
| 612 |
$parserOutput->setText( $out ); |
| 613 |
|
| 614 |
|
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public function onApiFormatHighlight( $context, $text, $mime, $format ) { |
| 629 |
if ( !isset( self::$mimeLexers[$mime] ) ) { |
| 630 |
return true; |
| 631 |
} |
| 632 |
|
| 633 |
$lexer = self::$mimeLexers[$mime]; |
| 634 |
$status = self::highlight( $text, $lexer ); |
| 635 |
if ( !$status->isOK() ) { |
| 636 |
return true; |
| 637 |
} |
| 638 |
|
| 639 |
$out = $status->getValue(); |
| 640 |
if ( preg_match( '/^<pre([^>]*)>/i', $out, $m ) ) { |
| 641 |
$attrs = Sanitizer::decodeTagAttributes( $m[1] ); |
| 642 |
$attrs['class'] .= ' api-pretty-content'; |
| 643 |
$encodedAttrs = Sanitizer::safeEncodeTagAttributes( $attrs ); |
| 644 |
$out = '<pre' . $encodedAttrs . '>' . substr( $out, strlen( $m[0] ) ); |
| 645 |
} |
| 646 |
$output = $context->getOutput(); |
| 647 |
$output->addModuleStyles( self::getModuleStyles() ); |
| 648 |
$output->addHTML( '<div dir="ltr">' . $out . '</div>' ); |
| 649 |
|
| 650 |
|
| 651 |
return false; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
public function onSoftwareInfo( &$software ) { |
| 661 |
try { |
| 662 |
$software['[https://pygments.org/ Pygments]'] = Pygmentize::getVersion(); |
| 663 |
} catch ( PygmentsException $e ) { |
| 664 |
|
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
public function onResourceLoaderRegisterModules( ResourceLoader $rl ): void { |
| 673 |
$rl->register( 'ext.pygments.view', [ |
| 674 |
'localBasePath' => MW_INSTALL_PATH . '/extensions/SyntaxHighlight_GeSHi/modules', |
| 675 |
'remoteExtPath' => 'SyntaxHighlight_GeSHi/modules', |
| 676 |
'scripts' => array_merge( [ |
| 677 |
'pygments.linenumbers.js', |
| 678 |
'pygments.links.js', |
| 679 |
'pygments.copy.js' |
| 680 |
], ExtensionRegistry::getInstance()->isLoaded( 'Scribunto' ) ? [ |
| 681 |
'pygments.links.scribunto.js' |
| 682 |
] : [] ), |
| 683 |
'styles' => [ |
| 684 |
'pygments.copy.less' |
| 685 |
], |
| 686 |
'messages' => [ |
| 687 |
'syntaxhighlight-button-copy', |
| 688 |
'syntaxhighlight-button-copied' |
| 689 |
], |
| 690 |
'dependencies' => [ |
| 691 |
'mediawiki.util', |
| 692 |
'mediawiki.Title' |
| 693 |
] |
| 694 |
] ); |
| 695 |
} |
| 696 |
} |