//
you're reading...
.NET 1.x

SyntaxHighlighter for .NET

After some prodding from Nish I ported the SyntaxHighlighter in use here and on his blog over to .NET. It spits out HTML so it is really meant to be used from ASP.NET, but I don’t have the true ASP.NET control written yet. For now you just have to settle for calling a few methods manually.

Since this one is mainly meant to be used with articles and not blog-like text it doesn’t do any fixing up of the text before processing it.

This is a very basic port; with only the “plain text” and “C#” languages added. Source code is included so anyone can modify or add their own languages.

Download links, features, bugs, coming features, and sample code follow the break.

Known problems:

  • If “show line numbers” is turned on and the pre block ends with the </pre>> on a line by itself, the blank line will get its own line numbers. Workaround: Don’t end your pre blocks on a line by themselves.

Upcoming features:

  • ASP.NET control for drag & drop goodness
  • More languages ported
  • Language initialization handled in config files
  • Possibly a request handler added for no-hassle highlighting
  • More code comments and samples

Current features:

  • OO design
  • Optimized handling of multiple snippets in one request
  • Highlights:
    • Line comments
    • Block comments
    • Strings
    • Keywords
    • Numbers
  • Multiple highlighting schemes possible.
    For example I currently have implemented a GenericLanguageDefinition class which will handle C like languages. But this highlighter is going to work poorly when dealing with HTML which would work best by just highlighting tag names. With the new OO design it is possible to implement this now….Just not by me, right now

I’ve been awake since midnight now; with only a few hours of sleep before that so I’m going to take a nap now.

But first, you may want the links to download it.

SyntaxHighlighter.zip (7Kb) – Compiled DLL.
SyntaxHighlighter_src.zip (13Kb) – Source code

You can look at the ConsoleDemo/Program.cs to see how it can be used in a console app; but the same thing can be done in an ASP.NET application.

To use it on your website all you should need to do is drop the dll in your site’s bin folder. Then simply add a few lines of code.

using MrDotNet.Web.SyntaxHighlighter;
using MrDotNet.Web.SyntaxHighlighter.Languages;

private void Initialize()
{
    LanguageDefinition ld;

    ld = new PlainTextLanguageDefinition();

    //SyntaxHighlighter.RegisterLanguageDefinition( "", ld );
    SyntaxHighlighter.RegisterLanguageDefinition( "text", ld );

    ld = new CSharpLanguageDefinition();
    SyntaxHighlighter.RegisterLanguageDefinition( "", ld );
    SyntaxHighlighter.RegisterLanguageDefinition( "cs", ld );
    SyntaxHighlighter.RegisterLanguageDefinition( "c#", ld );
}

Call Initialize from within your Page_Load, then where you want to highlight the code:

string highlightedCode = SyntaxHighlighter.HighlightFile( "c:\\sample.txt" );

This will return the HTML needed to highlight the code found in C:\sample.txt. There are also HighlightSnippet and HighlightText methods. HighlightText will only highlight text found withing <pre> tags, while HighlightSnippet will highlight everything passed in.

Advertisements

About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archive

%d bloggers like this: