62,074
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="codeHighlighter" requirePermission="false" type="ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, ActiproSoftware.CodeHighlighter.Net20"/>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
<codeHighlighter>
<cache languageTimeout="3"/>
<keywordLinking enabled="true" target="_blank" defaultKeywordCollectionKey="ActiproKeywords">
<keywordCollection key="ActiproKeywords">
<explicitKeyword tokenKey="IdentifierToken" patternValue="Actipro" url="http://www.actiprosoftware.com" caseSensitive="false"/>
<explicitKeyword tokenKey="IdentifierToken" patternValue="CodeHighlighter" url="http://www.codehighlighter.com" caseSensitive="false"/>
</keywordCollection>
</keywordLinking>
<languages>
<language key="Assembly" definitionPath="~/Languages/Lexers/ActiproSoftware.Assembly.xml"/>
<language key="BatchFile" definitionPath="~/Languages/Lexers/ActiproSoftware.BatchFile.xml"/>
<language key="C#" definitionPath="~/Languages/Lexers/ActiproSoftware.CSharp.xml"/>
<language key="CSS" definitionPath="~/Languages/Lexers/ActiproSoftware.CSS.xml"/>
<language key="HTML" definitionPath="~/Languages/Lexers/ActiproSoftware.HTML.xml"/>
<language key="INIFile" definitionPath="~/Languages/Lexers/ActiproSoftware.INIFile.xml"/>
<language key="Java" definitionPath="~/Languages/Lexers/ActiproSoftware.Java.xml"/>
<language key="JScript" definitionPath="~/Languages/Lexers/ActiproSoftware.JScript.xml"/>
<language key="Lua" definitionPath="~/Languages/Lexers/ActiproSoftware.Lua.xml"/>
<language key="MSIL" definitionPath="~/Languages/Lexers/ActiproSoftware.MSIL.xml"/>
<language key="Pascal" definitionPath="~/Languages/Lexers/ActiproSoftware.Pascal.xml"/>
<language key="Perl" definitionPath="~/Languages/Lexers/ActiproSoftware.Perl.xml"/>
<language key="PHP" definitionPath="~/Languages/Lexers/ActiproSoftware.PHP.xml"/>
<language key="PowerShell" definitionPath="~/Languages/Lexers/ActiproSoftware.PowerShell.xml"/>
<language key="Python" definitionPath="~/Languages/Lexers/ActiproSoftware.Python.xml"/>
<language key="SQL" definitionPath="~/Languages/Lexers/ActiproSoftware.SQL.xml"/>
<language key="VB.NET" definitionPath="~/Languages/Lexers/ActiproSoftware.VBDotNet.xml"/>
<language key="VBScript" definitionPath="~/Languages/Lexers/ActiproSoftware.VBScript.xml"/>
<language key="XAML" definitionPath="~/Languages/Lexers/ActiproSoftware.XAML.xml"/>
<language key="XML" definitionPath="~/Languages/Lexers/ActiproSoftware.XML.xml"/>
</languages>
<lineNumberMargin foreColor="Teal" paddingCharacter=" " visible="true"/>
<outlining enabled="true" imagesPath="~/Images/OutliningIndicators/"/>
<spacesInTabs count="4"/>
</codeHighlighter>
</configuration>
1、注册自定义控件
<%@ Register TagPrefix="CH" Namespace="ActiproSoftware.CodeHighlighter"
Assembly="ActiproSoftware.CodeHighlighter.Net20"
%>
2、添加控件
<pre>
<CH:CodeHighlighter runat="server" ID="CssCodeHighlighter">
</pre>
3、后台调用
[code=C#]
CssCodeHighlighter.Text = "select * from Employee--简单查询";
CssCodeHighlighter.LanguageKey = "SQL";
1、 引用命名空间
using ActiproSoftware.SyntaxEditor;
using ActiproSoftware.CodeHighlighter;
2、使用方法:
ActiproSoftware.CodeHighlighter.CodeHighlighter codeH =new ActiproSoftware.CodeHighlighter.CodeHighlighter();
codeH.Text = "select * from Employee--简单查询";
codeH.LanguageKey = "SQL";
Panel1.Controls.Add(codeH);
使用方法三:
直接返回高亮的HTML代码:
/// <summary>
/// 解析代码高亮
/// </summary>
/// <param name="code">代码</param>
/// <param name="languageKey">语法类型</param>
/// <param name="isShowLineNum">是否显示行号</param>
/// <returns>将代码解析高亮后组织成HTML的字符串</returns>
public static string GeneralCodeHighlight(string code, string languageKey, bool isShowLineNum)
{
SyntaxLanguage lang = null;
//尝试从缓存获取配置节
CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
if (config == null)
{
//缓存不存在,重新从 web.config 获取并保存缓存
config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
}
//获取语言
foreach (string key in config.LanguageConfigs.Keys)
{
if (key.ToLower() == languageKey.ToLower())
{
lang = CodeHighlighter.GetLanguage(config, key);
break;
}
}
//未知语言,不返回
if (lang == null)
return code;
CodeHighlighterEngine engine = new CodeHighlighterEngine();
engine.OutliningEnabled = false;
engine.LineNumberMarginVisible = isShowLineNum;
return engine.GenerateHtmlInline(string.Empty, code, lang);
}
good