SyntaxHighlighter插件默认是没有Lua语言的高亮显示的,最近本人写Lua博客的时候都是用c++的模板代替的,这实在不是个办法啊,于是乎本篇博客诞生了,经过我的研究,我们需要采用如下的策略。

1、 使用FTP工具登陆到WordPress空间,进入到wp-content/plugins目录,新建一个目录,取一个有意义的名字,比如syntaxhighlighter-lua;
2、 创建一个文件命名为shBrushLua.js,然后修改这个文件为以下的内容:

/**
 * SyntaxHighlighter
 * <a href="http://alexgorbatchev.com/SyntaxHighlighter">http://alexgorbatchev.com/SyntaxHighlighter</a>
 *
 * SyntaxHighlighter is donationware. If you are using it, please donate.
 * <a href="http://alexgorbatchev.com/SyntaxHighlighter/donate.html">http://alexgorbatchev.com/SyntaxHighlighter/donate.html</a>
 *
 * @version
 * 3.0.83 (July 02 2010)
 *
 * @copyright
 * Copyright (C) 2004-2010 Alex Gorbatchev.
 *
 * @license
 * Dual licensed under the MIT and GPL licenses.
 */
;(function()
{
    // CommonJS
    typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
    function Brush()
    {
        var keywords = 'and break do else elseif end false for function if in ' +
                       'local nil not or repeat return then true until while';
        var functions = '_G _VERSION assert collectgarbage dofile error getfenv ' +
                        'getmetatable ipairs load module next pairs pcall print ' +
                        ' rawequal rawget rawset require select setfenv setmetatable ' +
                        'tonumber tostring type unpack xpcall ' +
                        'coroutine.create coroutine.resume coroutine.running ' +
                        'coroutine.status coroutine.wrap coroutine.yield ' +
                        'debug.debug debug.getfenv debug.gethook debug.getinfo ' +
                        'debug.getlocal debug.getmetatable debug.getregistry ' +
                        'debug.getupvalue debug.setfenv debug.sethook debug.setlocal ' +
                        'debug.setmetatable debug.setupvalue debug.traceback ' +
                        'file:close file:flush file:lines file:read file:seek ' +
                        'file:setvbuf file:write' +
                        'io.close io.flush io.input io.lines io.open io.output ' +
                        'io.popen io.read io.stderr io.stdin io.stdout io.tmpfile ' +
                        'io.type io.write ' +
                        'math.abs math.acos math.asin math.atan math.atan2 math.ceil ' +
                        'math.cos math.cosh math.deg math.exp math.floor math.fmod ' +
                        'math.frexp math.huge math.ldexp math.log math.log10 math.max ' +
                        'math.min math.modf math.pi math.pow math.rad math.random ' +
                        'math.randomseed math.sin math.sinh math.sqrt math.tan math.tanh ' +
                        'os.clock os.date os.difftime os.execute os.exit os.getenv os.remove ' +
                        'os.rename os.setlocale os.time os.tmpname ' +
                        'package.cpath package.loaded package.loaders package.loadlib ' +
                        'package.path package.preload package.seeall ' +
                        'string.byte string.char string.dump string.find string.format ' +
                        'string.gmatch string.gsub string.len string.lower string.match ' +
                        'string.rep string.reverse string.sub string.upper table.concat ' +
                        'table.insert table.maxn table.remove table.sort';
        this.regexList = [
            { regex: /--.*/gm,                                                  css: 'comments' },  // one line comments
            { regex: /--\[\[[\S\s]*\]\]/gm,                                     css: 'comments' },  // multi line comments
            { regex: SyntaxHighlighter.regexLib.doubleQuotedString,             css: 'string' },    // strings
            { regex: SyntaxHighlighter.regexLib.singleQuotedString,             css: 'string' },    // strings
            { regex: SyntaxHighlighter.regexLib.multiLineDoubleQuotedString,    css: 'string' },    // strings
            { regex: SyntaxHighlighter.regexLib.multiLineSingleQuotedString,    css: 'string' },    // strings
            { regex: new RegExp(this.getKeywords(keywords), 'gm'),              css: 'keyword' },   // keywords
            { regex: new RegExp(this.getKeywords(functions), 'gm'),             css: 'functions' }  // functions
            ];
    }
    Brush.prototype = new SyntaxHighlighter.Highlighter();
    Brush.aliases   = ['lua'];
    SyntaxHighlighter.brushes.Lua = Brush;
    // CommonJS
    typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();

3、 在该目录创建另一个shBrushLua.php文件,添加如下内容:

<?php
/*
Plugin Name: SyntaxHighlighter Evolved: Lua
Description: Adds support for the Lua language to the SyntaxHighlighter Evolved plugin.
Author: Benny
Version: 1.0.0
*/

// SyntaxHighlighter Evolved doesn't do anything until early in the "init" hook, so best to wait until after that
add_action( 'init', 'syntaxhighlighter_lua_regscript' );

// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_lua_addlang' );

// Register the brush file with WordPress
function syntaxhighlighter_lua_regscript() {
    wp_register_script( 'syntaxhighlighter-brush-lua', plugins_url( 'shBrushLua.js', __FILE__ ), array('syntaxhighlighter-core'), '1.1.1' );
}

// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_lua_addlang( $brushes ) {
    $brushes['lua'] = 'lua';
    return $brushes;
}
?>

以上俩个文件已上传到本网站的网盘空间,大家可以去取。

4、文件都准备完了,OK,进入到WordPress后台管理的Plugins下,应该能看到新添加的一项syntaxhighlighter-lua,激活它。

5、尽情的使用Lua语言的高亮显示吧,只需要在写代码的时候将代码放到lua标签之间即可。

SyntaxHighlighter插件支持Lua语言高亮显示