FormatC syntax highlighting

Download

Requirements

Documentation

Web application

Ookii.FormatC is a .Net class library that allows you to add syntax highlighting to your C#, Visual Basic, C++, XML, HTML, Transact-SQL or PowerShell source code, so you can publish it on a web page or blog post. All code samples on ookii.org have been highlighted using FormatC.

You can use the online syntax highlighter to add syntax highlighting to your source code for use in a HTML document, or you can download the FormatC class library to add syntax highlighting support to your own application.

If you have any feedback or comments about FormatC, please leave a comment on this blog post.

Contextual keywords and types in C# and Visual Basic

Some keywords in C# and Visual Basic are so-called contextual keywords. These are treated as keywords in some contexts, but as regular identifiers in others. For example from is a keyword in a Linq expression, but not elsewhere; you can still have a variable named from without escapeing it with @ in C# or by enclosing it in [] in Visual Basic.

Because of the limitations of the syntax highlighting method used by FormatC, it cannot determine if a contextual keyword should be a keyword or not, so it always treats them as keywords. To prevent a contextual keyword from being highlighted, prefix it with ` (e.g. `from). The ` character will not appear in the output, and the contextual keyword will not be highlighted.

For both C# and Visual Basic you can specify a set of identifiers that should be highlighted as type names. For generic types, just specify the base name of the type without the generic arguments. For example, if you want to highlight List<T>, simply add List to the list of type names.

Like with contextual keywords, FormatC cannot determine the context of type name identifiers and will always highlight them. Prefix them with ` to prevent them from being highlighted. The ` character will not appear in the output.

XML Literals in Visual Basic

Visual Basic XML literals are supported; however, the XML literals must be marked explicitly with with [xml][/xml]. For example, this would look like this with a simple XML literal: Dim xml = [xml]<Foo />[/xml]

The [xml][/xml] tags will not be included in the output, and the contents of those tags will be formatted as XML literals. Embedded expressions in XML literals (which are delimited by <%= %> blocks) are also supported, and the contents of embedded expressions will be formatted as Visual Basic code.

A default stylesheet is provided. You can customize the syntax highlighting by modifying this stylesheet.