首页 > 代码库 > 666

666

using System.Text;using System.Web;using System.Web.UI.HtmlControls;using BlogEngine.Core;using BlogEngine.Core.Web.Controls;using Page=System.Web.UI.Page;[Extension("Adds SyntaxHighlighter script to your blog posts", "1.1", "<a target=\"_blank\" href=http://www.mamicode.com/"http://www.davidpokluda.com/blog/">David Pokluda")]public class SyntaxHighlighter{    private const string ExtensionName = "SourceCode SyntaxHighlighter";    public SyntaxHighlighter()    {        Post.Serving += AddSyntaxHighlighter;        ExtensionSettings settings = new ExtensionSettings(ExtensionName);    }    private void AddSyntaxHighlighter(object sender, ServingEventArgs e)    {        HttpContext context = HttpContext.Current;        if ((context.CurrentHandler is Page == false) || (context.Items[ExtensionName] != null))        {            return;        }        Page page = (Page) context.CurrentHandler;        AddJavaScriptToPage(page);        AddStylesheetToPage(page);        AddSyntaxHighlighterScriptToPage(page);        context.Items[ExtensionName] = 1;    }    private void AddJavaScriptToPage(Page page)    {        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shCore.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushCpp.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushCSharp.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushCss.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushJScript.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushPlain.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushSql.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushXml.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushPowerShell.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushFSharp.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushBash.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushDelphi.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushDiff.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushGroovy.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushJava.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushPerl.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushPhp.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushPython.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushRuby.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushScala.js"));        page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shBrushVb.js"));        #region // Unused brushes        /*                page.Header.Controls.Add(JavaScriptUrl("SyntaxHighlighter/Scripts/shLegacy.js"));        */        #endregion    }    private void AddStylesheetToPage(Page page)    {        HtmlLink css = new HtmlLink();        css.Attributes["type"] = "text/css";        css.Attributes["rel"] = "stylesheet";        css.Attributes["href"] = GetAbsoluteFromRelativeUrl("SyntaxHighlighter/Styles/shCore.css");        page.Header.Controls.Add(css);        css = new HtmlLink();        css.Attributes["type"] = "text/css";        css.Attributes["rel"] = "stylesheet";        css.Attributes["href"] = GetAbsoluteFromRelativeUrl("SyntaxHighlighter/Styles/shThemeDefault.css");        page.Header.Controls.Add(css);    }    private void AddSyntaxHighlighterScriptToPage(Page page)    {        StringBuilder sb = new StringBuilder();        sb.AppendLine("\n\n<script type=\"text/javascript\">");        sb.AppendLine("\tSyntaxHighlighter.all();");        sb.AppendLine("</script>\n\n");        page.ClientScript.RegisterStartupScript(page.GetType(), "SyntaxHighlighter", sb.ToString(), false);    }    private HtmlGenericControl JavaScriptUrl(string url)    {        HtmlGenericControl script = new HtmlGenericControl("script");        script.Attributes["type"] = "text/javascript";        script.Attributes["src"] = GetAbsoluteFromRelativeUrl(url);        return script;    }    private string GetAbsoluteFromRelativeUrl(string url)    {        return Utils.RelativeWebRoot + url;    }}

666