Initially I wanted to be able to easily highlight syntax on my website while writing things. I took a look at Microsoft's QuickStart highlighter, and even based my first implementation on it. But I was quite disappointed about the endresult: it didn't really give me what I was looking for.
So I started with my own implementation from scratch. My requirements were:
- Extensibility - it should be easy to extend existing highlighters.
- Reusability - it should be easy to reuse parts of the highlighters.
- Configuration support - highlighters must be able to be build from a configuration file and/or store their configuration.
- Separated functionality - a highlighter shouldn't for example parse the syntax in HTML.
- Easy to use - the client of the component should be able to parse code for a specific language with just a few lines of code.
- ASP.NET support - it should be possible to easily highlight code on a page, either in-line or code-only.
I started modelling the things I had in mind and finally came up with the following model:
In this model you can see that an highlighter works like a facade: the client doesn't have to worry about setting up the right scanners the right way. It's all done in each highlighter. This means that the requirement 'easy to use' is met in this model.
Each highlighter contains a set of scanners, which together basically form an highlighter. Scanners can be written once and be used within all the highlighters. Because I've implemented a chain of responsibility for the relationship between the scanners, highlighters or clients of highlighters can easily modify the chain. All this meets the requirements 'reusability', 'separated functionality' and 'extensibility'.
Since highlighters don't actually parse the syntax, but parser implementations do, requirement 'separated functionality' is met again. At any time someone could add a parser and still use all the highlighters.
After building this model, I started with the implementation of the design. When the core was done, I started implementing a set of common scanners, such as an XmlScanner, and finally implemented some highlighters, such as a CSharpHighlighter for highlighting C# code, and an HTML parser.
The requirement 'ASP.NET support' was not met yet at this point. So I made a ASP.NET server control which basically acted as a Label server control, except that it had 3 modes: all text, text and in-line source, and all source.
To meet the last requirement I extended the interface for scanners. With this extended interface, scanners could both load and save their configuration. By providing an implementation configuration manager which makes use of this extension, the last requirement was met too.
License:
This project is released under Ms-PL. Please contact me directly for questions or exceptions.
Downloads:
Recent updates (May 22nd, 07):
- Fixed: CSS failed to highlight when plain text was used. Credits go to Andrew Powell for finding and fixing this.
- Updated: Incorporated changes provided by Atif Aziz to take away a major performance bottleneck.
- Fixed: XML attributes which contain double quotes and single quotes are now properly processed.