WordPress配置支持Go和Lua语法高亮

一直使用WordPress的插件SyntaxHighlighter Evolved支持各种语言的高亮显示。但是并不支持Golang和Lua。特整理如下,希望能对你有所帮助。

3.2.1版本支持的语言列表如下(左边是别名,右边是语言)

       'as3'           => 'as3',
			'actionscript3' => 'as3',
			'bash'          => 'bash',
			'shell'         => 'bash',
			'coldfusion'    => 'coldfusion',
			'cf'            => 'coldfusion',
			'clojure'       => 'clojure',
			'clj'           => 'clojure',
			'cpp'           => 'cpp',
			'c'             => 'cpp',
			'c-sharp'       => 'csharp',
			'csharp'        => 'csharp',
			'css'           => 'css',
			'delphi'        => 'delphi',
			'pas'           => 'delphi',
			'pascal'        => 'delphi',
			'diff'          => 'diff',
			'patch'         => 'diff',
			'erl'           => 'erlang',
			'erlang'        => 'erlang',
			'fsharp'        => 'fsharp',
			'groovy'        => 'groovy',
			'java'          => 'java',
			'jfx'           => 'javafx',
			'javafx'        => 'javafx',
			'js'            => 'jscript',
			'jscript'       => 'jscript',
			'javascript'    => 'jscript',
			'latex'         => 'latex', // Not used as a shortcode
			'tex'           => 'latex',
			'matlab'        => 'matlabkey',
			'objc'          => 'objc',
			'obj-c'         => 'objc',
			'perl'          => 'perl',
			'pl'            => 'perl',
			'php'           => 'php',
			'plain'         => 'plain',
			'text'          => 'plain',
			'ps'            => 'powershell',
			'powershell'    => 'powershell',
			'py'            => 'python',
			'python'        => 'python',
			'r'             => 'r', // Not used as a shortcode
			'splus'         => 'r',
			'rails'         => 'ruby',
			'rb'            => 'ruby',
			'ror'           => 'ruby',
			'ruby'          => 'ruby',
			'scala'         => 'scala',
			'sql'           => 'sql',
			'vb'            => 'vb',
			'vbnet'         => 'vb',
			'xml'           => 'xml',
			'xhtml'         => 'xml',
			'xslt'          => 'xml',
			'html'          => 'xml',

从上面的列表中,我们没有看到golang和lua两种语言。可以自己添加一个插件。

一、步骤
0、创建插件目录

cd wordpress/wp-content/plugins/
mkdir syntaxhighlighter-evolved-brush-pack

1、下载Golang Brush文件

cd syntaxhighlighter-evolved-brush-pack
wget https://github.com/xeonx/shBrushGo/blob/master/shBrushGo.js

2、下载Lua Brush文件
github上仅有的一个shBrushLua.js文件下载后不能高亮显示。需要从另外一个网站下载,地址

wget -c 'https://www.ikpil.com/attachment/cfile1.uf@1473B4264C322E6D08D520.js' -O shBrushLua.js

3、编写插件文件syntaxhighlighter_brush.php
把插件文件放到插件目录syntaxhighlighter-evolved-brush-pack,内容如下

/**
* 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-go', plugins_url( 'shBrushGo.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );
    wp_register_script( 'syntaxhighlighter-brush-lua', plugins_url( 'shBrushLua.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );
}
  
// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_pack_addlang( $brushes ) {
    $brushes['go'] = 'go';
    $brushes['lua'] = 'lua';
     
    return $brushes;
}

4、在WordPress后台插件管理中启用刚才定义的插件,即可看到golang和lua的高亮显示
golang使用中括号go标志

package main

import (
	"fmt"
	"testing"
)

func TestHello(t *testing.T) {
	fmt.Println("2333")
	t.Log("pass")
}

lua使用中括号lua标志

array = {"Google", "Sina"}

for key,value in ipairs(array)
do
   print(key, value)
end

二、思考。
当然,你也可以之间修改插件SyntaxHighlighter Evolved的配置,但不推荐,因为插件升级可能带来问题。

三、后记
博客哒哒熊的山洞中的博文《为SyntaxHighlighter Evolved插件添加新的语言支持》记录的非常详细了,本来完全没必要记录自己的这篇博文,原由在于自己配置Golang和Lua的过程中,从找资料到修改配置,还是有一些坑,并且目前网上没有完整对Golang和Lua高亮显示的说明,故记录。

参考
How to Easily Display Code on Your WordPress Site
github shBrushGo

发表评论

电子邮件地址不会被公开。 必填项已用*标注