在cnblogs和许多其他论坛、Blog上都有插入程序代码高亮显示的功能。现参考cnblogs的为例,以discuz!NT 2.0 rc2为例,说明如何增加插入代码高亮功能。
cnblogs的代码高亮功能是通过一个共享软件实现的,可以到以下网址下载使用
http://www.actiprosoftware.com/Download/Freeware.aspx
此软件的Api很简单,就是一个Asp.net的控件。可以在网页上输出高亮显示的代码。但我们不想让高亮代码在网页上输出,而是插入到文章中。可以对discuz自带的Editor进行改造,以达到响应的功能。
首先将 ActiproSoftware.CodeHighlighter.Net20.dll、ActiproSoftware.Shared.Net20.dll、CodeHighlighterTest.dll Copy到discuz的bin目录。然后在discuz的页面文件价(aspx/1)下添加InsertCode.aspx文件,内容如下
1
<%@ Page Language="C#" %>
2
<%@ Import Namespace="ActiproSoftware.CodeHighlighter" %>
3
<%@ Import Namespace="System.Reflection" %>
4
5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7
<script runat="server">
8
protected string ReturnCode;
9
10
protected override void OnLoad(EventArgs e)
11
然后修改discuz的webconfig,如下所示
1<?xml version="1.0" encoding="utf-8" ?>
2<configuration>
3 <configSections>
4 <section name="codeHighlighter" requirePermission="false" type="ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, ActiproSoftware.CodeHighlighter.Net20"/>
5 </configSections>
6
7 <system.web>
8
9 <compilation
10 defaultLanguage="c#"
11 debug="false"
12 />
13
14 <authorization>
15 <allow users="*" /> <!-- 允许所有用户 -->
16 </authorization>
17
18 <trace
19 enabled="false"
20 requestLimit="10"
21 pageOutput="false"
22 traceMode="SortByTime"
23 localOnly="true"
24 />
25
26 <!-- 以下为Discuz!NT相关的一些系统设置, 如果有疑问, 请访问 http://nt.discuz.net/config 获得更详细的说明
27 -->
28
29
30
31 <!-- 注意:此节设置错误信息的显示
32
33 "On" 始终显示自定义(友好的)信息。
34 "Off" 始终显示详细的 ASP.NET 错误信息。
35 "RemoteOnly" 只对不在本地 Web 服务器上运行的
36 -->
37 <customErrors
38 mode="RemoteOnly"
39 />
40
41 <!-- 注意:此节设置全球化,Discuz!NT由此支持多语言。
42 -->
43 <globalization
44 requestEncoding="utf-8"
45 responseEncoding="utf-8"
46 fileEncoding="utf-8"
47 />
48
49 <!-- 注意:此节设置是否使用ASP.net表单安全验证,Discuz!NT使用自己的验证。
50 -->
51 <pages
52 validateRequest="false"
53 enableEventValidation="false"
54 enableViewStateMac="false"
55 viewStateEncryptionMode ="Never"
56 />
57
58 <!-- 注意:此节设置由Discuz!NT接管http请求。不会干涉对非Discuz!NT论坛路径下的请求。
59 -->
60 <httpModules>
61 <add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />
62 </httpModules>
63
64 <xhtmlConformance mode="Legacy"/>
65
66 <httpRuntime maxRequestLength="2097151" executionTimeout="3600"/>
67
68 <webServices>
69 <protocols>
70 <add name="HttpGet" />
71 <add name="HttpPost"/>
72 </protocols>
73 </webServices>
74
75
76 </system.web>
77
78 <codeHighlighter>
79 <cache languageTimeout="3"/>
80 <keywordLinking enabled="true" target="_blank" defaultKeywordCollectionKey="ActiproKeywords">
81 <keywordCollection key="ActiproKeywords">
82 <explicitKeyword tokenKey="IdentifierToken" patternValue="Actipro" url="http://www.actiprosoftware.com" caseSensitive="false"/>
83 <explicitKeyword tokenKey="IdentifierToken" patternValue="CodeHighlighter" url="http://www.codehighlighter.com" caseSensitive="false"/>
84 </keywordCollection>
85 </keywordLinking>
86 <languages>
87 <language key="Assembly" definitionPath="~/Languages/Lexers/ActiproSoftware.Assembly.xml"/>
88 <language key="BatchFile" definitionPath="~/Languages/Lexers/ActiproSoftware.BatchFile.xml"/>
89 <language key="C#" definitionPath="~/Languages/Lexers/ActiproSoftware.CSharp.xml"/>
90 <language key="CSS" definitionPath="~/Languages/Lexers/ActiproSoftware.CSS.xml"/>
91 <language key="HTML" definitionPath="~/Languages/Lexers/ActiproSoftware.HTML.xml"/>
92 <language key="INIFile" definitionPath="~/Languages/Lexers/ActiproSoftware.INIFile.xml"/>
93 <language key="Java" definitionPath="~/Languages/Lexers/ActiproSoftware.Java.xml"/>
94 <language key="JScript" definitionPath="~/Languages/Lexers/ActiproSoftware.JScript.xml"/>
95 <language key="Lua" definitionPath="~/Languages/Lexers/ActiproSoftware.Lua.xml"/>
96 <language key="MSIL" definitionPath="~/Languages/Lexers/ActiproSoftware.MSIL.xml"/>
97 <language key="Pascal" definitionPath="~/Languages/Lexers/ActiproSoftware.Pascal.xml"/>
98 <language key="Perl" definitionPath="~/Languages/Lexers/ActiproSoftware.Perl.xml"/>
99 <language key="PHP" definitionPath="~/Languages/Lexers/ActiproSoftware.PHP.xml"/>
100 <language key="PowerShell" definitionPath="~/Languages/Lexers/ActiproSoftware.PowerShell.xml"/>
101 <language key="Python" definitionPath="~/Languages/Lexers/ActiproSoftware.Python.xml"/>
102 <language key="SQL" definitionPath="~/Languages/Lexers/ActiproSoftware.SQL.xml"/>
103 <language key="VB.NET" definitionPath="~/Languages/Lexers/ActiproSoftware.VBDotNet.xml"/>
104 <language key="VBScript" definitionPath="~/Languages/Lexers/ActiproSoftware.VBScript.xml"/>
105 <language key="XAML" definitionPath="~/Languages/Lexers/ActiproSoftware.XAML.xml"/>
106 <language key="XML" definitionPath="~/Languages/Lexers/ActiproSoftware.XML.xml"/>
107 </languages>
108 <lineNumberMargin foreColor="Teal" paddingCharacter=" " visible="true"/>
109 <outlining enabled="true" imagesPath="~/Images/OutliningIndicators/"/>
110 <spacesInTabs count="4"/>
111 </codeHighlighter>
112
113</configuration>
Copy高亮组件的 Languages\Lexers、Images\OutliningIndicators 目录到discuz的指定目录
在discuz的编辑器文件 editor\editor.js,在function discuzcode(cmd, arg)函数中if else组里添加处理insertcode的代码
if (cmd == "insertcode")
{
window.open('../InsertCode.aspx', null, 'height=500, width=600, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
}
修改discuz编辑器文件 editor\cp_editor.htm 在插入图片后变添加插入代码的命令
<a id="posteditor_cmd_insertcode">插入代码</a>
现在,discuz的blog里就有插入高亮代码的功能了。