zoukankan      html  css  js  c++  java
  • codehightlight 代码高亮显示

     博客园后台就是用这个codehightlight显示漂亮的代码片段,在我们博客系统中也做支持。

    下载到 codehightlight 组件,具体的使用如下:

    1.在webconfig中设置好配置 

    2.就是window.open(url)一个新的窗口,贴好代码之后将生成和的代码返回原来的编辑器(window.opener)。具体的要看你自己的编辑器怎么调用用了:我用tinymce:

    window.opener.tinyMCE.execCommand()

    具体可以把博客园dudu开源的博客园代码看下,不过里面是老版的codehightlight,可到官方站下个最新版本。

    代码:  


    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();
            }
        }
     
    }
  • 相关阅读:
    郎咸平 马行空 - 郎咸平说:萧条下的希望(2014年10月25日)
    豆豆 - 遥远的救世主(2014年10月18日)
    高铭 - 天才在左 疯子在右(2014年10月3日)
    张庭斌 艾经纬 - 中国富人为何变穷:金融危机攻略(2014年5月20日)
    王晋康 - 终极爆炸 ▪ 王晋康科幻小说精选集3(2014年4月24日)
    刘慈欣 - 乡村教师 ▪ 刘慈欣科幻自选集(2014年4月2日)
    王晋康 - 替天行道 ▪ 王晋康科幻小说精选集2(2014年3月27日)
    易之 - 我是个算命先生(2014年3月23日)
    宋鸿兵 - 货币战争5(2014年3月20日)
    【英】阿瑟 ▪ 克拉克 - 神的九十九亿个名字(2014年3月16日)
  • 原文地址:https://www.cnblogs.com/lulu/p/1274882.html
Copyright © 2011-2022 走看看