File Icon Extractor Data Extraction Overview
Apr 16

Printer Friendly Version

Download Source Code: CSharpSyntaxHighlighter.zip - 11.54KB

---
  Full C# implementation of a C# Syntax Highlighter, that returns output as formatted HTML code. Generic base class for syntax highlighters, NOT using regular expressions, that will be later used for other languages as well. Interactive live demo. Attached open-source ASP.NET solution, which may include other syntax highlighters implemented to date and offered for free.  
---
Principle of a Syntax Highlighter, here for some C# source code
Principle of a Syntax Highlighter, here for some C# source code

Overview

Syntax Highlighters parse text source code and, based on the semantics of the words and specific constructs defined by the programming language syntax, apply similar text formatting attributes - colors, bold, italic - to the tokens. Compiler and IDE (Integrated Development Environments) source code editors go even further: they check the full syntax for errors, look for type references and make sure all functions and method calls are valid. Syntax Highlighters do not usually go so far. If you look at the different colors of your source code in Visual Studio or one of its free Express editions, you'll see that type members do not get a different color, so we may also skip the metadata check.

There are many syntax highlighters on the market you can buy, for all possible language. But just a few free open source highlighters, especially for .NET. And when you find one, chances are it's using regular expressions.

By all means, this is certainly the best way to implement syntax highlighters and, in general, text parsing. But if you're not familiar with regular expressions and you need code you can quickly understand, to change it and adapt it to your needs, you're in trouble.

Such a simple, clean and efficient free open source implementation is offered by squishyWARE, for three different languages: C#, VB.NET and XML. It's using one regular expression for each kind of semantic construct, such as string value, single-line comment, multi-line comment, documentation comment, keyword, XML tag. For embedded structures and each match found, it recursively parses the inner text.

SyntaxHighlighters Demo Project

We will provide an alternative to this kind of syntax highlighters, NOT using regular expressions. Generated output is usually HTML or RTF, but we will provide for now only HTML result.

If you want to expose web pages with formatted source code from your web site, our project offers you a ready to use implementation. It has the exact same classes we use, on the server side, to render formatted source code on our site. We basically save portions of code in separate files, and when a page is requested, we dynamically load each file, pass its contents to the syntax highlighter for that language (in most cases C#), and its highlighted HTML is embedded in the result page.

The demo project - you can download and use it in Visual Studio or Web Developer Express - provides an ASP.NET web site implementation, but you can easily use the inner classes to build standalone Windows applications. In our demo, each HTML page where you show syntax highlighted code must include, in the HEAD section, the SyntaxHighlighters.css style sheet and SyntaxHighlighters.js JavaScript code. For a standalone application that generates HTML pages, you can include the full content of the style sheet into a STYLES HTML block. The JavaScript code is necessary only when you use collapsible blocks, and you can also copy its full source content into a SCRIPT HTML section.

In a generated HTML page, you can include one or more highlighted code sequences, for the same language or different languages. Just make sure that, for each different language, you instantiate, once, the appropriate class derived from SyntaxHighlighter.

To change the colors of your highlighted text, change their values in SyntaxHighlighters.css only. Take care, some colors are common for different kinds of languages and implementations.

Continue reading »

Subscribe and Share: Subscribe using any feed reader Bookmark and Share

Leave a Reply