为SyntaxHighlighter 添加程序语言

SyntaxHighlighter 是对各种代码进行语法着色的独立 JavaScript 库。今天计划为博客提前发布一些Golang的代码。可惜的是SyntaxHighlighter 目前还不支持Go语言(囧!!)。新一为了给大家带来更好的查看Golang代码,新一让SyntaxHighlighter支持Go代码高亮,显示效果不是很完善,正则好的同学来帮忙完善一下。SyntaxHighlighter 有2个大版本,新一在wordpress中使用的是3.X的版本。所以这次也只是针对3.X的添加了,其本意是一致的。看完本章你也可以为自己喜欢而没有的语言添加进去。

创建shBrushGolang.js语言文件

路径:syntaxhighlighter/syntaxhighlighter3/scripts/shBrushGolang.js

/**
 * SyntaxHighlighter
 * http://alexgorbatchev.com/SyntaxHighlighter
 *
 * SyntaxHighlighter is donationware. If you are using it, please donate.
 * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
 *
 * @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 funcs	=	'make';

		var keywords =	'break case chan const continue default defer else fallthrough for func go ' +
						'goto if import interface map package range return select struct switch type var ';

		this.regexList = [
			{ regex: SyntaxHighlighter.regexLib.singleLineCComments,	css: 'comments' },			// one line comments
			{ regex: SyntaxHighlighter.regexLib.multiLineCComments,		css: 'comments' },			// multiline comments
			{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,		css: 'string' },			// double quoted strings
			{ regex: SyntaxHighlighter.regexLib.singleQuotedString,		css: 'string' },			// single quoted strings
			{ regex: /^var \w+/g,										css: 'variable' },			// variables
			{ regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi,				css: 'value' },			// numbers
			{ regex: new RegExp(this.getKeywords(funcs), 'gmi'),		css: 'functions' },			// common functions
			{ regex: new RegExp(this.getKeywords(keywords), 'gm'),		css: 'keyword' }			// keyword
			];

		this.forHtmlScript(SyntaxHighlighter.regexLib.phpScriptTags);
	};

	Brush.prototype	= new SyntaxHighlighter.Highlighter();
	Brush.aliases	= ['golang'];

	SyntaxHighlighter.brushes.Golang = Brush;

	// CommonJS
	typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();

如果是wordpress使用的同学还要在syntaxhighlighter.php中添加关联。

		$this->brushes = (array) apply_filters( 'syntaxhighlighter_brushes', array(

			'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',

			'xhtml'         => 'xml',
			'golang'		=>	'golang',

		) );

//在286行左右添加
wp_register_script( 'syntaxhighlighter-brush-golang',        plugins_url('syntaxhighlighter/' . $this->shfolder . '/scripts/shBrushGolang.js'),        array('syntaxhighlighter-core'), $this->agshver );

大功告成,其实新一就复制了SyntaxHighlighter中PHP的那份JS进行二次修改的。

转载请注明来源:新一 » 为SyntaxHighlighter 添加程序语言

赞 (0) 评论 (0) 分享 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址