1414

  1 using ActiproSoftware.CodeHighlighter;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Web;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 
  9 public partial class Highligh : System.Web.UI.Page
 10 {
 11     private bool IsCollapse = false;
 12     private string codeTitle = "View Code";
 13     protected void Page_Load(object sender, EventArgs e)
 14     {
 15         if (!IsPostBack)
 16         {
 17             if (Request.Form["CodeText"] != null && Request.Form["CodeText"].ToString() != "")
 18             {
 19                 Highlighter.Text = Request.Form["CodeText"].ToString();
 20             }
 21             else
 22             {
 23                 throw new HttpException();
 24             }
 25 
 26             if (Request.Form["Language"] != null && Request.Form["Language"].ToString() != "")
 27             {
 28                 Highlighter.LanguageKey = Request.Form["Language"].ToString();
 29             }
 30             else
 31             {
 32                 Highlighter.LanguageKey = "C#";
 33             }
 34 
 35             if (Request.Form["IsCollapse"] != null && Request.Form["IsCollapse"].ToString() != "")
 36             {
 37                 IsCollapse = Convert.ToBoolean(Request.Form["IsCollapse"]);
 38             }
 39 
 40             if (Request.Form["IsShowLineNumber"] != null && Request.Form["IsShowLineNumber"].ToString() != "")
 41             {
 42                 Highlighter.LineNumberMarginVisible = Convert.ToBoolean(Request.Form["IsShowLineNumber"]);
 43             }
 44             else
 45             {
 46                 Highlighter.LineNumberMarginVisible = false;
 47             }
 48             Highlighter.OutliningEnabled = false;
 49 
 50             if (Request.Form["CodeTitle"] != null && Request.Form["CodeTitle"].ToString() != "")
 51             {
 52                 codeTitle = Request.Form["CodeTitle"].ToString();
 53             }
 54         }
 55     }
 56 
 57     protected void CodeHighlighter_PostRender(object sender, EventArgs e)
 58     {
 59         string highligh = ((Highlighter.Output).Substring(117));
 60         highligh = highligh.Substring(0, highligh.Length - 6);
 61         string html = string.Format(normalTemplate, highligh);
 62         if (IsCollapse)
 63         {
 64             html = string.Format(collapseTemplate,
 65                 System.Guid.NewGuid().ToString(),
 66                 Highlighter.Text,
 67                 highligh,
 68                 codeTitle
 69                 );
 70         }
 71         /*
 72         var outo = new
 73         {
 74             Html = (Highlighter.Output).Substring(117),
 75             Text = Highlighter.Text
 76         };*/
 77         Response.Clear();
 78         //Response.ContentType = "application/json";
 79         Response.Write(html);
 80         Response.End();
 81     }
 82 
 83     private readonly string normalTemplate = @"
 84         <div class='cnblogs_code'><pre>{0}</pre></div>";
 85 
 86     private readonly string collapseTemplate = @"
 87         <div class='cnblogs_code'>            
 88             <img id='code_img_closed_{0}' class='code_img_closed' 
 89                 src='/CodeHighlighter/Images/ContractedBlock.gif' 
 90                 alt='' 
 91                 style='display: inline;' />
 92             <span class='cnblogs_code_collapse' style='display: inline;'>{3}</span>
 93             <img id='code_img_opened_{0}' class='code_img_opened' 
 94                 style='display: none;' 
 95                 src='/CodeHighlighter/Images/ExpandedBlockStart.gif' 
 96                 alt='' />
 97             <div id='cnblogs_code_open_{0}' class='cnblogs_code_hide'>                
 98                 <div class='cnblogs_code_toolbar'><span class='cnblogs_code_copy'><img src='/CodeHighlighter/Images/copycode.gif' alt='复制代码'/></span></div>
 99                 <pre>{2}</pre>
100                 <div class='cnblogs_code_toolbar'><span class='cnblogs_code_copy'><img src='/CodeHighlighter/Images/copycode.gif' alt='复制代码'/></span></div>
101             </div>
102             <div id='cnblogs_code_copy_{0}' class='cnblogs_code_hide'>
103                 <div>按 Ctrl+C 复制代码</div>
104                 <textarea style='width: 99%; height: 100px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5;'>{1}</textarea>
105                 <div>按 Ctrl+C 复制代码</div>
106             </div>
107         </div>";
108 }
View Code

 

 posted on 2015-01-22 10:17  enbos  阅读(118)  评论(0编辑  收藏  举报