You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrismHighlight.base.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. if ( ! defined( 'MEDIAWIKI' ) )
  3. die();
  4. /**#@+
  5. * A code highlighter using the prismjs.com code highlighter
  6. * (c) 2013 Andrew Senetar <[email protected]>
  7. *
  8. * @file
  9. * @link http://
  10. * @ingroup Extensions
  11. * @author Andrew Senetar <[email protected]>
  12. * @copyright (c) 2013 by Andrew Senetar
  13. * @license http://opensource.org/licenses/MIT MIT License
  14. */
  15. /**
  16. * Implements the Prismjs code highlighting extension
  17. *
  18. * @author Andrew Senetar
  19. */
  20. class PrismHighlight {
  21. public static function parserHook($text, $args = array(), $parser) {
  22. $code_classes = '';
  23. $pre_args ='';
  24. if(!isset($args['lang']) && !isset($args['language']))
  25. $args['lang'] = 'markup';
  26. foreach( $args as $key => $value ){
  27. switch ($key) {
  28. case 'lang':
  29. case 'language':
  30. $code_classes .= " language-$value";
  31. break;
  32. case 'class':
  33. $code_classes .= " $value";
  34. break;
  35. case 'line-numbers':
  36. $pre_args .= " class=$key";
  37. break;
  38. case 'data-start':
  39. $pre_args .= " data-line-offset=$value"; //should make sure this is set
  40. default;
  41. $pre_args .= " $key=$value";
  42. }
  43. }
  44. # Replace all '&', '<,' and '>' with their HTML entitites. Order is
  45. # important. You have to do '&' first.
  46. # not sure if this helps us
  47. $text = str_replace('&', '&amp;', $text);
  48. $text = str_replace('<', '&lt;', $text);
  49. $text = str_replace('>', '&gt;', $text);
  50. $text = trim($text); // trim any begining / end whitespace as it is ugly
  51. return"<pre $pre_args><code class=\"$code_classes\">$text</code></pre>";
  52. }
  53. // load modules
  54. public static function beforePageDisplay(&$wgOut, &$sk) {
  55. $wgOut->addModules('ext.PrismHighlight');
  56. $wgOut->addModules('ext.PrismHighlight.core');
  57. // Continue
  58. return true;
  59. }
  60. }