当前位置: 移动技术网 > IT编程>开发语言>JavaScript > SyntaxHighlighter自动识别并加载脚本语言 原创

SyntaxHighlighter自动识别并加载脚本语言 原创

2019年03月27日  | 移动技术网IT编程  | 我要评论
syntaxhighlighter是一个使用javascript编写的支持多种编程语言语法高亮的js插件,很多大型网站或博客都在使用。 syntaxhighlighter

syntaxhighlighter是一个使用javascript编写的支持多种编程语言语法高亮的js插件,很多大型网站或博客都在使用。

syntaxhighlighter插件默认是每种编程语言加载对应js语言库,那么如何实现syntaxhighlighter自动加载不同语言的js语言库

syntaxhighlighter官方已经给出了解决方案 shautoloader.js 自动加载库;

在html文件中引用syntaxhighlighter核心运行库

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>hello syntaxhighlighter</title>
<script type="text/javascript" src="scripts/shcore.js"></script>
<script type="text/javascript" src="scripts/shautoloader.js"></script>
<link type="text/css" rel="stylesheet" href="styles/shcore.css" rel="external nofollow" rel="external nofollow" />
<link type="text/css" rel="stylesheet" href="styles/shcoredefault.css" rel="external nofollow" rel="external nofollow" />

</head>

shcore.js     syntaxhighlighter插件的核心运行库

shautoloader.js   自动加载运行库要实现自动加载必须引入此文件

shcore.css     核心css样式

shcoredefault.css  主题css样式

以上四个文件必须引用到文档中

需要高亮显示的代码请入在 <pre class="brush:js;"></pre>

代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>hello syntaxhighlighter</title>
<script type="text/javascript" src="scripts/shcore.js"></script>
<script type="text/javascript" src="scripts/shautoloader.js"></script>
<link type="text/css" rel="stylesheet" href="styles/shcore.css" rel="external nofollow" rel="external nofollow" />
<link type="text/css" rel="stylesheet" href="styles/shcoredefault.css" rel="external nofollow" rel="external nofollow" />
</head>
 
<body>
<h1>hello syntaxhighlighter</h1>
<pre class="brush: js;">
function hellosyntaxhighlighter()
{
return "hi!";
}
</pre>

<pre class="brush: js;"> 中的 class="brush: js;"是根据显示代码语言而定

下面是实现syntaxhighlighter自动加载的主要代码

<script language="javascript">
function path(){
	var args = arguments,
		result = [];
	for(var i = 0; i < args.length; i++)
		result.push(args[i].replace('@', '/pub/sh/current/scripts/'));//请替换成自己项目中syntaxhighlighter的具体路径
	return result
};
syntaxhighlighter.autoloader.apply(null, path(
'applescript		@shbrushapplescript.js',
'actionscript3 as3		@shbrushas3.js',
'bash shell		@shbrushbash.js',
'coldfusion cf		@shbrushcoldfusion.js',
'cpp c			@shbrushcpp.js',
'c# c-sharp csharp		@shbrushcsharp.js',
'css			@shbrushcss.js',
'delphi pascal		@shbrushdelphi.js',
'diff patch pas		@shbrushdiff.js',
'erl erlang		@shbrusherlang.js',
'groovy			@shbrushgroovy.js',
'java			@shbrushjava.js',
'jfx javafx		@shbrushjavafx.js',
'js jscript javascript	@shbrushjscript.js',
'perl pl			@shbrushperl.js',
'php			@shbrushphp.js',
'text plain		@shbrushplain.js',
'py python			@shbrushpython.js',
'ruby rails ror rb		@shbrushruby.js',
'sass scss			@shbrushsass.js',
'scala			@shbrushscala.js',
'sql			@shbrushsql.js',
'vb vbnet			@shbrushvb.js',
'xml xhtml xslt html		@shbrushxml.js'
));
syntaxhighlighter.all();
</script>

以上就是实现syntaxhighlighter自动识别语言并自动载语言库的代码,需要的同学可以测试下;

本文实例下载:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网