Adding SyntaxHighlighter to BlogEngine.NET

by Scott Dougherty 1/23/2008 5:26:00 PM

Just about every technical article or blog that posts source code typically implements some kind of tool to render source code syntax highlighting for a variety of programming languages. The benefits of this are obvious, most importantly readability, followed by easy access to copy and paste snippets of source code without intermixing CSS and/or HTML markup. Although BlogEngine.NET 1.3 comes with an extension to highlight source code syntax, I prefer a tool called SyntaxHighlighter which is entirely CSS and JavaScript based and can be used within virtually any web application. SyntaxHighlighter uses JavaScript regular expressions to highlight syntax and it has been noted that with large blocks of code performance can be slow.

You can add source code using the CodeFormatterExtension that is available with BlogEnginge.NET 1.3 and utilizes bb style markup to generate the desired highlighted syntax. The CodeFormatterExtension makes it relatively easy to add source code to a post using the bb markup method, however the caveat to this is that it transforms the HTML output of your post by adding CSS class attributes to the code which is probably not an issue for most.

Install SyntaxHighlighter

To get started visit the Google Code project web site to download the latest release of SyntaxHighlighter. This post applies to the SyntaxHighlighter 1.5.1 release. After unpackaging the files I chose to rename the directory from "dp.SyntaxHighlighter" to "SyntaxHighlighter" and only uploaded the Scripts and Styles sub directories to the BlogEngine.NET root path. I chose to keep these sub directories within a SyntaxHighlighter parent directory only for organizational purposes, however it does not matter where you upload the files as long as you remember the paths so the stylesheet and scripts can be referenced. The Uncompressed subdirectory does not need to be uploaded as it is a copy of all of the JavaScript files before they were compressed or trimmed of white space in order to minimize the file sizes.

The next step is to sign in to your BlogEngine.NET account and within the Settings Control Panel add references to the SyntaxHighlighter stylesheet and scripts. The references can be added to either the HTML Head Section or the Tracking Script section and should be referenced using the full path, instead of relative paths. This will prevent any problems loading these files from all of the various locations within BlogEngine.NET.

For example:




In the Tracking Script section copy the following JavaScript, by adding the script to the bottom of the page this helps to ensure that the required stylesheet and script files have been loaded by the browser.


Posting source code within BlogEngine.NET using SyntaxHighlighter is accomplished by entering your code in HTML mode of the TinyMCE text editor that comes default with BlogEngine.NET. Simply wrap the source code in the following <pre name="code" class="c-sharp"> tags to achieve the following C# syntax highlighting:

public static string SayHello(string name)
{
  return String.Format("Hello, {0}!", name);
}

The caveat with this is that the TinyMCE text editor "cleans up" and reformats your markup and ultimately removes the name="code" attribute that is required by SyntaxHighlighter. Without the name="code" attribute SyntaxHighlighter cannot apply the appropriate styles. By adding the name="code" attribute to the <pre> tags it also invalidates XHTML Strict implementations and therefore will not work with BlogEngine.NET themes such as "Leaves" or "Indigo" for example. This appears to be a limitation to the SyntaxHighlighter implementation.

Configure TinyMCE

The way to fix this is to modify the TinyMCE JavaScript to allow the name attribute for all <pre> tags. The TinyMCE script can be found in the BlogEngine.NET\admin\tiny_mce\tiny_mce.js or tiny_mce_src.js for the uncompressed script file. The valid_elements settings list in the init function should be modified to include the name attribute.

//valid_elements settings 
-pre[class|align|style] 
//changed to include "name" attribute, appears to invalidate xhtml strict but not transitional 
-pre[class|align|style|name] 


At this point SyntaxHighlighter should now be working, but before you upload the modified script file there is another modification that is required to prevent the removal of carriage returns. Within the formatHTML function the TinyMCE text editor removes carriage returns resulting in every line of source code within a <pre> tag running onto a single line.

//delete this line to allow carriage returns, within a pre tag this jumbles everything onto a single line

h = h.replace(/\r/g, ''); // Windows sux, isn't carriage return a thing of the past :)

Now the modified TinyMCE script can be uploaded and replaced and SyntaxHighlighter should be ready to highlight your code on XHTML Transitional pages.

Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

BlogEngine.NET | Tools

Related posts

Comments

1/23/2008 11:36:33 PM

Wally

A timely article, I was thinking about solving this same problem, Scott you definitely saved me some time! Two tips to add for readers:
1) For those rushing as I was, the examples above don't include the link to the SyntaxHighlighter.css and the brush files, you have to add those yourself. For example, if you want to highlight ruby code, then add in the shBrushRuby.js file. Needless to say I felt stupid when I realized my mistake.
2) As to the carriage return issue, which is definitely quirky for newbies, BlogEngine.NET by default links to the tiny_mce.js file from the admin\tinyMCE.ascx file. I find that it's much easier to simply update tiny_mce_src.js and repoint the javascript link instead of working with the compressed source. Then again, the compressed file is there to save bandwidth.

Wally us

1/24/2008 3:32:27 AM

pingback

Pingback from blog.grobelsek.net

Adding SyntaxHighlighter to BlogEngine.NET

blog.grobelsek.net

1/25/2008 5:23:27 PM

Scott Dougherty

Wally,

Thanks for clarifying and pointing out the missing reference to the CSS. After looking into this I found myself scratching my head, it appears that the TinyMCE editor is removing the <link> tags from my HTML!! It appeared to be missing from the list of valid elements in the TinyMCE script.

Scott

Scott Dougherty us

2/7/2008 6:12:25 PM

Paul Tumelty

Hi Scott,

I followed the instructions and it kind of works, but the indenting gets lost and everything ends up left aligned. Any idea as to what might be going wrong?

Paul.

Paul Tumelty gb

2/8/2008 8:16:32 PM

Jared Wennstrom

Excellent post, however I'm having a weird problem in IE7. For some reason, the list-item attribute of the rendered OL/LI elements is not being applied, and IE is rendering the default 'disc' bullets instead of line numbers.

Any ideas? The only thing I can guess is that there's something wrong in the CSS hierarchy; perhaps I'll try downloading your style sheet for comparison, but I have confirmed locally that I can create a static HTML page using 'decimal' as the style type, so it's not my PC...

Frustrating!

Jared Wennstrom us

2/8/2008 9:15:58 PM

Jared Wennstrom

Okay - I got things ironed out. It wasn't the style sheet (although the style sheet could use a little pruning). It was the theme.

For what it's worth: the 'Dirtylicious' theme will break line numbering on SyntaxFormatter for IE7 (but not FF). I have changed themes and all is well.

Jared Wennstrom us

2/12/2008 9:17:58 PM

Scott Dougherty

With most of the formatting issues I've encountered it appears to be the TinyMCE editor JavaScript. The script removes various elements, most likely in an attempt to produce XHTML strict markup. It's frustrating especially because I posted coded within a pre tag and the TinyMCE script removed lines of code from my post!

I have been thinking of trying other rich text editors in place of TinyMCE for BlogEngine.NET. The FreeTextBox ASP.NET web control is the first that comes to my mind.

Any suggestions on alternative rich text editors?

Scott Dougherty us

3/15/2008 11:42:51 PM

pingback

Pingback from tomwindows.com

Test using SyntaxHighligher

tomwindows.com

5/2/2008 3:05:02 AM

pingback

Pingback from dscoduc.com

SyntaxHighlighter Extension for BlogEngine.NET

dscoduc.com

6/6/2008 5:50:11 PM

pingback

Pingback from nurasto.com

SyntaxHighligther in BlogEngine.Net

nurasto.com

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/20/2008 9:04:04 AM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Google Ads

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Pages

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in