?? SyntaxHighlighter Evolved是一款很好用的WordPress代碼高亮插件,基于 SyntaxHighlighter JavaScript package by Alex Gorbatchev開發(fā)。默認支持的語言還是比較全的,具體如下:
actionscript3、bash、clojure、coldfusion、cpp、csharp、css、delphi、diff、erlang、fsharp、go、groovy、html、java、javafx、javascript、latex(youcanalsorenderLaTeX)、matlab(keywordsonly)、objc、perl、php、powershell、python、r、ruby、scala、sql、text、vb、xml
??想要給它添加新的語言支持也比較容易,只要注冊所謂的“Brush”即可。各種Brush通過Google搜索形如“shBrushXXX.js”(XXX為語言名稱,如:shBrushAsm.js),就能很容易獲取到。
??接下來還需要編寫一個WordPress插件,用于注冊新的Brush。具體代碼可參考:syntaxhighlighter_brush.php
<?php
/**
* Plugin Name: SyntaxHighlighter Evolved: Brush Pack
* Description: Adds support for the mutiple language to the SyntaxHighlighter Evolved plugin.
* Author: Hattiss<hattiss@163.com>
* Version: 1.0
* Author URI: http://ddxoyy.info
*/
// SyntaxHighlighter Evolved doesn't do anything until early in the "init" hook, so best to wait until after that
add_action( 'init', 'syntaxhighlighter_pack_regscript' );
// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_pack_addlang' );
// Register the brush file with WordPress
function syntaxhighlighter_pack_regscript() {
wp_register_script( 'syntaxhighlighter-brush-vim', plugins_url( 'shBrushVim.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );
wp_register_script( 'syntaxhighlighter-brush-asm', plugins_url( 'shBrushAsm.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );
}
// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_pack_addlang( $brushes ) {
$brushes['vim'] = 'vim';
$brushes['asm'] = 'asm';
return $brushes;
}
?>
??最后,在WordPress的插件目錄(wordpress\wp-content\plugins)下創(chuàng)建一個文件夾(syntaxhighlighter-evolved-brush-pack)。然后將編寫好的插件文件和下載到的Brush文件上傳至該文件夾。再登錄WordPress后臺,啟用剛剛上傳的插件,即可獲得新增語言的高亮支持了。
參考資料