博客园后台就是用这个codehightlight显示漂亮的代码片段,在我们博客系统中也做支持。
下载到 codehightlight 组件,具体的使用如下:
1.在webconfig中设置好配置
2.就是window.open(url)一个新的窗口,贴好代码之后将生成和的代码返回原来的编辑器(window.opener)。具体的要看你自己的编辑器怎么调用用了:我用tinymce:
window.opener.tinyMCE.execCommand()
具体可以把博客园dudu开源的博客园代码看下,不过里面是老版的codehightlight,可到官方站下个最新版本。
代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ActiproSoftware.CodeHighlighter;
public partial class InsertCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CodeHighlighterConfiguration codeConfig = (CodeHighlighterConfiguration)System.Configuration.ConfigurationSettings.GetConfig("codeHighlighter");
foreach (string key in codeConfig.LanguageConfigs.Keys)
{
LanguageDropDownList.Items.Add(key);
if (key == "C#")
{
LanguageDropDownList.SelectedIndex = LanguageDropDownList.Items.Count - 1;
}
}
}
}
protected void HighlightButton_Click(object sender, EventArgs e)
{
Codehighlighter1.Text = CodeText.Text;
Codehighlighter1.OutliningEnabled = true;
Codehighlighter1.LanguageKey = LanguageDropDownList.SelectedItem.Value;
}
public void CodeHighlighter_PostRender(object sender, System.EventArgs e)
{
if (IsPostBack)
{
string html = Codehighlighter1.Output.Replace("\"", "\\\"");
html = html.Replace("\r\n", "<br>\"+\r\n\"");
html = html.Replace("/Images/", "http://blog.ncuhome.cn/Images/");
string divstr = @"<div style='BORDER-RIGHT: windowtext 0.5pt solid;PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px;PADDING-TOP: 4px; BORDER-LEFT: windowtext 0.5pt solid;WIDTH: 98%; BORDER-BOTTOM: windowtext 0.5pt solid;word-break:break-all'>";
Response.Write("<script language='javascript'>"+
"window.opener.tinyMCE.execCommand(\"mceInsertContent\",false,\""+divstr + html + "</div>\");"+
"window.parent.close();"+
"</script>");
Response.End();
}
}
}