首页 > 文档 > 为SyntaxHighlighter Evolved插件添加新的语言支持

为SyntaxHighlighter Evolved插件添加新的语言支持

SyntaxHighlighter Evolved是一款很好用的WordPress代码高亮插件,基于 SyntaxHighlighter JavaScript package by Alex Gorbatchev开发。默认支持的语言还是比较全的,具体如下:

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。具体代码可参考:

<?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)下创建一个文件夹(syntaxhighlighter-evolved-brush-pack)。然后将编写好的插件文件和下载到的Brush文件上传至该文件夹。再登录WordPress后台,启用刚刚上传的插件,即可获得新增语言的高亮支持了。

参考资料

Adding A New Brush (Language)

分类: 文档 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.