codehightlight 代码高亮显示

 博客园后台就是用codehightlight组件显示漂亮的代码片段,在我们校园博客系统中也支持一下。

 

组件是免费开源的,通过官网链接 codehightlight 下载完成后:

1. 在webconfig中设置好配置

2. 往前端编辑器中添加一个按钮,点击window.open(url)一个新的窗口,用户贴好代码之后将生成和的代码返回原来的编辑器(window.opener)。具体要看使用编辑器怎么调用:我用tinymce:window.opener.tinyMCE.execCommand() 

参考代码:  


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();
        }
    }
 
}

 

posted @ 2008-08-23 23:59  LittlePeng  阅读(562)  评论(0编辑  收藏  举报