是否有一个使用 C# 编写的开源项目,可以以与 shjs 类似的方式将语法突出显示应用于多种语言?
这是一些伪代码:
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 文件,其中所有具有 source
类的 pre
元素都在定义源语言的地方语法突出显示
注意:这不适用于服务器端脚本,而是离线应用程序的一部分,其中性能不太重要。
最佳答案
找到了!我的 HDD 上一直有一个名为“ColorizerLibrary.dll”的托管 DLL 中的 SandcaSTLe Help File Builder (SHFB) 文件夹中的一个。
只需添加对此 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);
注意:请记住链接到 head
中的 CSS 文件以可视化语法颜色。
已添加:请从此处查找 ColorizerLibrary
的源代码:http://shfb.codeplex.com/SourceControl/changeset/view/98645#1672960
关于c# - 用 C# 编写的灵活语法荧光笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680057/