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自动识别语言并自动载语言库的代码,需要的同学可以测试下;
本文实例下载:SyntaxHighlighter自动识别并加载
新闻热点
疑难解答