ブログC#3.0構文の強調表示

プログラミングに関する一連の記事を書く予定です。 C#コードの多くの例があります。 また、コードを読み取るには、強調表示する必要があります。



ただし、ブログにコードを貼り付けると、構文の強調表示は消えます。 特別なプログラムを使用して、同等の強調表示でHTMLを生成する必要があります。 Ookii.FormatCソースコードハイ ライターの 2つのプロジェクトについて知っています。



しかし、私は両方の機能が十分ではありませんでした。 LJおよびHabréで(つまり、cssを使用せずに)発行する機能と、LINQの強調表示が必要でした。 Ookii.FormatCを変更することにし、構文ハイライターを作成しました。







バイナリ: Syntax Highlighter.rar

ソース: SyntaxHighlighter.src.rar

要件: .NET Framework 3.5 SP1がインストールされている(それより低い場合があり、チェックしなかった)



例:



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;

}

}

}








スクリーンショット:







ソースコードハイライターとの違い



-LINQ構文の強調表示。

-行とコメントの正しい処理。

-オープンソース。



Ookii.FormatCとの違い



-cssおよびpreタグを使用しません。

-先頭のスペースをnbspに置き換えます。



注釈



Ookii.FormatCコードを、LJとHabréで動作するC#3.0バックライトに必要なだけ修正しました。 プロジェクトが需要がある場合、多分私は:

-コードを通常のホストに転送します。

-リファクタリングとコードの再編成に従事します。

-Ookii.FormatCライブラリでサポートされている他の言語(C#、Visual Basic、C ++、XML、HTML、Transact-SQL、PowerShell)の強調表示を完了します。



All Articles