2012-07-27 61 views
1

是否有一個使用C#編寫的開源項目,它可以以與shjs類似的方式將語法突出顯示應用於各種語言?靈活的語法高亮寫在C#

下面是一些僞代碼:

public string HighlightSourceInHTML(string html) { 
    return Highlighter.HighlightHTML(html); 
} 

當輸入HTML是沿着線的東西:

<!DOCTYPE html> 
<html> 
<head>...</head> 
<body> 
    <p>Here is a function written using C#:</p> 
    <pre class="source lang-csharp">public void foo(int a, int b) { 
    return a + b; 
}</pre> 

    <p>Here is the same function written using JavaScript:</p> 
    <pre class="source lang-javascript">function foo(a, b) { 
    return a + b; 
}</pre> 
</body> 
</html> 

當上述將基本上返回整個HTML文件,其中所有pre元素與類別source在源語言被定義的地方突出顯示語法

否te:這不是服務器端腳本,而是性能不太重要的離線應用程序的一部分。

+0

我要建議[Pygments來做](http://pygments.org/)(這是什麼Ma9ic的答案使用),其是用Python編寫的。語法突出顯示器*是否用C#編寫?有許多用其他語言書寫的語法熒光筆:[Google code prettify](http://code.google.com/p/google-code-prettify/),[highlight.js](http:// softwaremaniacs。 org/soft/highlight/en /)和[SyntaxHighlighter](http://alexgorbatchev.com/SyntaxHighlighter/)等等。 – Chris 2012-07-27 08:39:50

回答

2

找到一個!我的硬盤上有一個在託管DLL中的Sandcastle Help File Builder(SHFB)文件夾中,名爲「ColorizerLibrary.dll」。

只需添加對此DLL的引用,並且語法着色變得非常簡單。

下面是一個使用示例:

ColorizerLibrary.CodeColorizer colorizer = new ColorizerLibrary.CodeColorizer(
    @"C:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\Colorizer\highlight.xml", 
    @"C:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\Colorizer\highlight.xsl" 
); 
colorizer.Init(); 

string htmlText = "<!DOCTYPE html><html><head><title>Test Page</title></head><body><pre codelanguage=\"CSharp\">public string Foo(string a, int b = 4) {\n\treturn a + b * 3;\n}</pre></body></html>"; 
return colorizer.ProcessAndHighlightText(htmlText); 

注:請記住,鏈接到CSS文件中head可視化的語法顏色。

補充:請從這裏找到源代碼ColorizerLibraryhttp://shfb.codeplex.com/SourceControl/changeset/view/98645#1672960