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.
 
 
 

159 lines
4.2 KiB

  1. <?php
  2. if ( ! defined( 'MEDIAWIKI' ) )
  3. die();
  4. /**#@+
  5. * A code highlighter using the prismjs.com code highlighter
  6. *
  7. * @file
  8. * @link http://
  9. * @ingroup Extensions
  10. * @author Andrew Senetar <[email protected]>
  11. * @copyright (c) 2013 by Andrew Senetar
  12. * @license http://opensource.org/licenses/MIT MIT License
  13. */
  14. $wgExtensionCredits['other'][] = array(
  15. 'path' => __FILE__,
  16. 'name' => 'PrismHighlight',
  17. 'version' => 0.1,
  18. 'author' => 'Andrew Senetar',
  19. //'url' => 'http://',
  20. 'descriptionmsg' =>'prismhighlight-desc',
  21. );
  22. /**
  23. * Configuration variables for the extension
  24. */
  25. // Default languages to load
  26. if (! isset($wgPrismHighlightLanguages)) {
  27. $wgPrismHighlightLanguages = array(
  28. 'bash',
  29. 'c',
  30. 'css',
  31. 'javascript',
  32. 'python',
  33. 'php',
  34. 'sql',
  35. 'markup',
  36. );
  37. }
  38. // List of plugins to load (none by default)
  39. // autolinker does not appear to work nor does show-invisibles
  40. // line-number and line-highlight should not be used together since it breaks visual
  41. if (! isset($wgPrismHighlightPlugins)) {
  42. $wgPrismHighlightPlugins = array(
  43. );
  44. }
  45. // Allow highlighting source tags
  46. if (! isset($wgPrismHighlightSource)) {
  47. $wgPrismHighlightSource = true;
  48. }
  49. // The theme to use (false for default)
  50. if (! isset($wgPrismHighlightTheme)) {
  51. $wgPrismHighlightTheme = false;
  52. }
  53. // Build the list for the js includes
  54. function efPrismHighlight_Scripts() {
  55. global $wgPrismHighlightPlugins, $wgPrismHighlightLanguages;
  56. // some languages depend on others
  57. $plugin_deps = array(
  58. 'bash' => 'clike',
  59. 'c' => 'clike',
  60. 'coffeescript' => 'javascript',
  61. 'cpp' => 'c',
  62. 'groovy' => 'clike',
  63. 'java' => 'clike',
  64. 'javascript' => 'clike',
  65. 'php' => 'clike',
  66. 'scss' => 'css',
  67. );
  68. // make sure all needed languages are loaded
  69. $langs = $wgPrismHighlightLanguages;
  70. foreach($langs as $lang ){
  71. if( array_key_exists( $lang, $plugin_deps ) )
  72. $langs[] = $plugin_deps[$lang];
  73. }
  74. // remove duplicates order matters...
  75. $langs = array_unique( array_reverse( $langs ) );
  76. // load plugins
  77. foreach( $wgPrismHighlightPlugins as $plugin )
  78. $scripts[] = "prism/plugins/prism-$plugin.js";
  79. // load languages
  80. foreach( $langs as $lang )
  81. $scripts[] = "prism/components/prism-$lang.js";
  82. return $scripts;
  83. }
  84. // build list of styles to include
  85. function efPrismHighlight_Styles() {
  86. global $wgPrismHighlightPlugins, $wgPrismHighlightTheme;
  87. // stop mediawiki from causing problems
  88. $styles[] = 'overrides.css';
  89. // load the theme or not
  90. if( $wgPrismHighlightTheme != false )
  91. $styles[] = "prism/themes/prism-$wgPrismHighlightTheme.css";
  92. else
  93. $styles[] = "prism/prism.css";
  94. // load plugin styles
  95. foreach($wgPrismHighlightPlugins as $plugin)
  96. $styles[] = "prism/plugins/prism-$plugin.css";
  97. return $styles;
  98. }
  99. /**
  100. * Setup for the extension
  101. */
  102. $wgAutoloadClasses['PrismHighlight'] = dirname(__FILE__) . '/PrismHighlight.base.php';
  103. $wgExtensionMessagesFiles['PrismHighlight'] = dirname(__FILE__) . '/PrismHighlight.i18n.php';
  104. /**
  105. * Resource Modules
  106. */
  107. // styles and scripts to load after core is in place
  108. $wgResourceModules['ext.PrismHighlight'] = array(
  109. 'localBasePath' => dirname(__FILE__),
  110. 'remoteExtPath' => 'PrismHighlight',
  111. 'styles' => efPrismHighlight_Styles(),
  112. 'scripts' => efPrismHighlight_Scripts(),
  113. 'dependencies' => 'ext.PrismHighlight.core',
  114. );
  115. // the core
  116. $wgResourceModules['ext.PrismHighlight.core'] = array(
  117. 'localBasePath' => dirname(__FILE__),
  118. 'remoteExtPath' => 'PrismHighlight',
  119. 'scripts' => array( 'prism/prism-core.js', 'init.js' ),
  120. );
  121. /**
  122. * Hooks
  123. */
  124. // Register parser hook
  125. $wgHooks['ParserFirstCallInit'][] = 'efPrismHighlight_Setup';
  126. // Register before display hook to load css / js files
  127. $wgHooks['BeforePageDisplay'][] = 'PrismHighlight::beforePageDisplay';
  128. /**
  129. * Register parser hook
  130. */
  131. function efPrismHighlight_Setup( &$parser ) {
  132. global $wgPrismHighlightSource;
  133. if( $wgPrismHighlightSource)
  134. $parser->setHook('source', array('PrismHighlight', 'parserHook'));
  135. $parser->setHook('syntaxhighlight', array('PrismHighlight', 'parserHook'));
  136. return true;
  137. }