极念网欢迎您!
代码高亮工具CodeHighlighter 使用配置
作者:翅膀的初衷来源:本站原创发布时间:2013/9/25 22:55:17查看数:61899

  CodeHighlighter 是一款基于.NET平台的优秀的代码高亮工具,相比Syntaxhighlight,后者是在页面运行时,由javascript对代码进行高亮处理,如果代码量很多时有可能会有点卡,且每加次都须重新处理,而CodeHighlighter 会在后台直接生成HTML,不依赖javascript与css,使用更加方便!

  当然,凡事有利必有弊,在灵活性上CodeHighlighter 略差,本文在此不对此做优劣评比,仅分享如何配置使用CodeHighlighter !

  首先:我们必须下载CodeHighlighter ,它的最新版本,我们可以在http://www.actiprosoftware.com/Products/DotNet/ASPNET/CodeHighlighter/Default.aspx 下载得到!

  第二步:将下载的文件包解压,把 “

ActiproSoftware.Shared.Net20.dll
ActiproSoftware.CodeHighlighter.Net20.dll
CodeHighlighterTest.dll

  ” 三个DLL文件引用到Bin目录下,然后将整个Languages文件夹拷贝到网站根目录!

  第三步:打开Web.Config,在configurations节点下增加如下配置:

1 <configSections> 2 <section name="codeHighlighter" requirePermission="false" type="ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, ActiproSoftware.CodeHighlighter.Net20"/> 3 </configSections> 4 5 <codeHighlighter> 6 <cache languageTimeout="3"/> 7 <keywordLinking enabled="true" target="_blank" defaultKeywordCollectionKey="ActiproKeywords"> 8 <keywordCollection key="ActiproKeywords"> 9 <explicitKeyword tokenKey="IdentifierToken" patternValue="Actipro" url="http://www.actiprosoftware.com" caseSensitive="false"/> 10 <explicitKeyword tokenKey="IdentifierToken" patternValue="CodeHighlighter" url="http://www.codehighlighter.com" caseSensitive="false"/> 11 </keywordCollection> 12 </keywordLinking> 13 <languages> 14 <language key="Assembly" definitionPath="~/Languages/Lexers/ActiproSoftware.Assembly.xml"/> 15 <language key="BatchFile" definitionPath="~/Languages/Lexers/ActiproSoftware.BatchFile.xml"/> 16 <language key="C#" definitionPath="~/Languages/Lexers/ActiproSoftware.CSharp.xml"/> 17 <language key="CSS" definitionPath="~/Languages/Lexers/ActiproSoftware.CSS.xml"/> 18 <language key="HTML" definitionPath="~/Languages/Lexers/ActiproSoftware.HTML.xml"/> 19 <language key="INIFile" definitionPath="~/Languages/Lexers/ActiproSoftware.INIFile.xml"/> 20 <language key="Java" definitionPath="~/Languages/Lexers/ActiproSoftware.Java.xml"/> 21 <language key="JScript" definitionPath="~/Languages/Lexers/ActiproSoftware.JScript.xml"/> 22 <language key="Lua" definitionPath="~/Languages/Lexers/ActiproSoftware.Lua.xml"/> 23 <language key="MSIL" definitionPath="~/Languages/Lexers/ActiproSoftware.MSIL.xml"/> 24 <language key="Pascal" definitionPath="~/Languages/Lexers/ActiproSoftware.Pascal.xml"/> 25 <language key="Perl" definitionPath="~/Languages/Lexers/ActiproSoftware.Perl.xml"/> 26 <language key="PHP" definitionPath="~/Languages/Lexers/ActiproSoftware.PHP.xml"/> 27 <language key="PowerShell" definitionPath="~/Languages/Lexers/ActiproSoftware.PowerShell.xml"/> 28 <language key="Python" definitionPath="~/Languages/Lexers/ActiproSoftware.Python.xml"/> 29 <language key="SQL" definitionPath="~/Languages/Lexers/ActiproSoftware.SQL.xml"/> 30 <language key="VB.NET" definitionPath="~/Languages/Lexers/ActiproSoftware.VBDotNet.xml"/> 31 <language key="VBScript" definitionPath="~/Languages/Lexers/ActiproSoftware.VBScript.xml"/> 32 <language key="XAML" definitionPath="~/Languages/Lexers/ActiproSoftware.XAML.xml"/> 33 <language key="XML" definitionPath="~/Languages/Lexers/ActiproSoftware.XML.xml"/> 34 </languages> 35 <lineNumberMargin foreColor="Teal" paddingCharacter=" " visible="true"/> 36 <outlining enabled="true" imagesPath="~/Images/OutliningIndicators/"/> 37 <spacesInTabs count="4"/> 38 </codeHighlighter>

  第四步:配置完成,然后直接在后台对代码进行高亮即可:

1 protected void Page_Load(object sender, EventArgs e) 2 { 3 string code = "代码";//待高亮的代码 4 string language = "语言";//支持c#,html,sql,css,php,msil,python,perl,javascript 等,具体见web.config 的languages配置 5 bool isShowLineNum = false;//是否显示行号 6 Response.Write("<pre>"); 7 Response.Write(GeneralCodeHighlight(code, language, isShowLineNum)); 8 Response.Write("</pre>"); 9 Response.End(); 10 } 11 12 public string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum) 13 { 14 15 SyntaxLanguage lang = null; 16 //尝试从缓存获取配置节 17 CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration; 18 if (config == null) 19 { 20 //缓存不存在,重新从 web.config 获取并保存缓存 21 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter"); 22 HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config); 23 } 24 //获取语言 25 foreach (string key in config.LanguageConfigs.Keys) 26 { 27 if (key.ToLower() == languageKey.ToLower()) 28 { 29 lang = CodeHighlighter.GetLanguage(config, key); 30 break; 31 } 32 } 33 //不明语言,不理会返回 34 if (lang == null) 35 return code; 36 CodeHighlighterEngine engine = new CodeHighlighterEngine(); 37 engine.OutliningEnabled = false; 38 engine.LineNumberMarginVisible = isShowLineNum; 39 return engine.GenerateHtmlInline(string.Empty, code, lang); 40 }