MediaWikiでResourceLoaderを使用する

バージョン1.17から、MediaWikiはスタイルとスクリプトをアセンブルおよびロードするための新しいメカニズム-ResourceLoaderを導入しました。 この記事では、 としてGoogleCodePrettify拡張機能を使用して、MediaWikiマークアップにsyntaxhighlight



タグを追加する使用法について説明します。



拡張機能のすべての作成者は、廃止されたaddScript



などの代わりに新しいメカニズムを使用することを強くお勧めします。



最初に、ロードするリソースモジュールを定義します。

 $wgResourceModules['ext.GoogleCodePrettify'] = array( 'localBasePath' => dirname(__FILE__), 'remoteExtPath' => 'GoogleCodePrettify', 'styles' => array('google-code-prettify/prettify.css'), 'scripts' => array('google-code-prettify/prettify.js', 'init.js') );
      
      







Wikiマークアップパーサーをロードするときに、 syntaxhighlightタグを処理します

 // Register parser hook $wgHooks['ParserFirstCallInit'][] = 'efGoogleCodePrettify_Setup'; /** * Register parser hook */ function efGoogleCodePrettify_Setup( &$parser ) { $parser->setHook('syntaxhighlight', array('GoogleCodePrettify', 'parserHook')); return true; } class GoogleCodePrettify { private static $prettified = false; public static function parserHook($text, $args = array(), $parser) { self::$prettified = true; return "<pre class=\"prettyprint\">$text</pre>"; }
      
      







最後に、必要に応じて必要なスタイルとスクリプトを追加します。

 // Register before display hook $wgHooks['BeforePageDisplay'][] = 'GoogleCodePrettify::beforePageDisplay'; #   public static function beforePageDisplay(&$wgOut, &$sk) { if (self::$prettified) { $wgOut->addModules('ext.GoogleCodePrettify'); } // Continue return true; }
      
      







ResourceLoaderがスタイルとスクリプトのアセンブリを引き継ぎます。 クエリ文字列にdebug = 1オプションがある場合、スタイルとスクリプトが「そのまま」提供されることに注意してください。



はい、ほとんど忘れていました。 init.js



スクリプトは次のようになります。

 (function($, window) { $(window.document).ready(function() { window.prettyPrint(); }); })(window.jQuery, window);
      
      










こちらもご覧ください:




All Articles