Code Formatter Extension

25. March 2009 1:00 PM

When I composing the Headline News Control topic last week, and I keep suffering from formatting the sample code with the 'Code Formatter Extension' developed by manoli. After spent some time digging in and out from Google, and finally I found a posting about the missing CodeFormatterExtension.cs was actually put into a wrong folder (App_Code\Extensions\CodeFormatter) with a wrong filename (CodeFormatter.cs) too.

Next, I encounter another new problem... that is the code snippet does not formatted at all. So, I continue to digging through Google again. Last, I found this posting! Again... it does not solve my problem! Therefore, I took a visit manoli website and try out the version in his website and compare the formatted code with the output from the 'Code Formatter Extension' I have, and I notice there are differents! Hence, I step into the code and fixed it up and proceed to added the code, so that it will automatic include the missing CSS (csharp.css that I download from manoli website) link tag in the output HTML.

After a long struggling with this 'Code Formatter Extension', finally I get everything up and running with a minor touch up the csharp.css to have a better code snippet window being render in the blog. But, there is one problem I failed to resolve it! that is the alternative line background color does not cover the full code snippet when the code is too long as show in sample below.

/// <summary>
    /// An event that handles ServingEventArgs
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void ServingContent(object sender, ServingEventArgs e)
    {
      if (e.Body.Contains("XYZ"))
      {
          HtmlLink cssLink = new HtmlLink();
          cssLink.Attributes["rel"] = "stylesheet";
          cssLink.Attributes["type"] = "text/css";
          cssLink.Attributes["href"] = String.Concat(Utils.RelativeWebRoot, "CodeFormatter/csharp.css");
          System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
          page.Header.Controls.Add(cssLink);
          
          e.Body = codeRegex.Replace(e.Body, new MatchEvaluator(CodeEvaluator));
          e.Body = codeBeginTagRegex.Replace(e.Body, @"<div class=""code"">");
          e.Body = codeEndTagRegex.Replace(e.Body, @"</div>");
      }
    }

To Do...

  1. Unzip the CodeFormatterExtensions.zip
  2. Copy the respective files into your blog root directory.

 

Syntaxs... 

Here are those syntax supported by this 'Code Formatter Extensions'

[code:c#|vb|js|html|xml|tsql|msh;ln=on|off;alt=on|off]

   Put your code snippet here

[⁄code]

 

Since I have spent the time to fixed most the problem in this 'Code Formatter Extension', and wish to save those BlogEngine.NET newbies time to fixed this problem...  I am upload the updated copy of 'Code Formatter Extensions' for those wish to download and use it. Do enjoy this extension origin from manoli.  

 

CodeFormatterExtensions.zip (19.13 kb)

BlogEngine.NET

Comments (2) -

Lê Tuấn Kiên
5/22/2009 1:40:51 PM #
I do it same your guid but it not running.
The code below is testing your blog only Laughing

[code:c#;ln=on;alt=on]
void ServingContent(object sender, ServingEventArgs e)
      {
        if (e.Body.Contains("XYZ"))
        {
            HtmlLink cssLink = new HtmlLink();
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["type"] = "text/css";
            cssLink.Attributes["href"] = String.Concat(Utils.RelativeWebRoot, "CodeFormatter/csharp.css");
            System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
            page.Header.Controls.Add(cssLink);
            
            e.Body = codeRegex.Replace(e.Body, new MatchEvaluator(CodeEvaluator));
            e.Body = codeBeginTagRegex.Replace(e.Body, @"<div class=""code"">");
            e.Body = codeEndTagRegex.Replace(e.Body, @"</div>");
        }
      }
[⁄code]
8/13/2010 1:51:25 AM #
I know that this solution might be dated but it is what i used to make the code tag work on my site.  Assuming i did it right, it appeared to break my rss feed.  I just quickly added the following to fix it.

try
          {
              System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
              page.Header.Controls.Add(cssLink);
          }
          catch (Exception)
          {
              
              // do nothing this must of not of been an html page
          }

Johnathan Bracken
http://blog.barkalot.com

Add comment



  Country flag
biuquote
Loading