2012-07-27 60 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