📜 ⬆️ ⬇️

C # 3.0 syntax highlighting in blogs

I have plans to write a series of articles on programming. There will be many examples of C # code. And in order to read the code, it must be highlighted.

But syntax highlighting disappears when the code is inserted into the blog. It is necessary to use special programs for generating HTML with equivalent highlighting. I know about two such projects - Ookii.FormatC and Source Code Highlighter .

However, the functionality of both of them was not enough for me. I needed the ability to publish in LJ and on Habré (that is, without using css) plus LINQ highlighting. I decided to modify Ookii.FormatC and wrote my syntax highlighter.
')


Binary: Syntax Highlighter.rar
Sources: SyntaxHighlighter.src.rar
Requirements: Installed .NET Framework 3.5 SP1 (maybe lower, did not check)

Example:


using System;
// A single line comment starting at the beginning of the line
using System.IO;
/* A comment block starting at the beginning of the line */
using MyNamespace;

namespace CSharpTest
{
// A comment containing what looks like a "string"
// A comment containg /* what looks like */ a block comment
/* A multiline comment
* also containing a // regular comment
* And containing what looks like a "string"
*/


public class Program
{
/// <summary>
/// An XML comment <see cref="System.String" />.
/// </summary>
public static void Main()
{
int Int; // case sensitive test
int @int ; // escaping test
Console .WriteLine( "A string \" \\\" hello \t\\" );
Console .WriteLine( "A string containing what // looks like a comment." ); // followed by a real comment
Console .WriteLine( @"An @ string"" \" ); /* and another comment */
Console .WriteLine( @"Another @ string """"" );
Console .WriteLine( "{0} {1} {2}" , "more than one\\" , /* embedded comment */ @"string on ""the same" , "line" );
#if PREPROCESSORTEST
for ( int x = 0; x < 10; ++x )
{
Console .WriteLine( 'x' );
Console .WriteLine( '\'' );
Console .WriteLine( '\\' );
}
#endif
}

public static void Linq()
{
IObservable < Event < MouseEventArgs >> draggingEvent =
from mouseLeftDownEvent in control.GetMouseLeftDown( )
from mouseMoveEvent in control.GetMouseMove( ).Until( control.GetMouseLeftUp( ) )
let comparer = new MouseEventComparer( mouseMoveEvent )
group mouseMoveEvent by comparer into cluster
select cluster;
}
}
}


Screenshot:




Differences from Source Code Highlighter


- LINQ syntax highlighting.
- Correct processing of lines and comments.
- Open Source.

Differences from Ookii.FormatC


- Does not use css and pre tag.
- Replaces leading spaces with nbsp.

Notes


I modified the Ookii.FormatC code just as much as I needed for running C # 3.0 backlight in LiveJournal and on Habré. If the project will be in demand, maybe I:
- I will transfer the code to a normal host.
- I will do refactoring and code reorganization.
- I’ll finish highlighting for other languages ​​supported by the Ookii.FormatC library (C #, Visual Basic, C ++, XML, HTML, Transact-SQL, PowerShell).

Source: https://habr.com/ru/post/78106/


All Articles