ColorfulCode is a syntax highlighter library for C#/.NET Core that uses sublime-text syntax definitions. It is a wrapper for native rust library syntect.
Benefit from syntect syntax highlighting engine, ColorfulCode is very fast. See the performance of syntect for more info.
ColorfulCode is available as a NuGet package from nuget.org. It can be added to a project in a numbers of ways, depending on the project type and tools used:
$ dotnet add package ColorfulCode --version 1.0.0-preview1 PM> Install-Package ColorfulCode -Version 1.0.0-preview1
<PackageReference Include="ColorfulCode" Version="1.0.0-preview1" />ColorfulCode runs on the following platforms and .NET Core versions:
| OS | Version | Architectures | .NET Runtimes |
|---|---|---|---|
| macOS | > 10.12 | x64 | 3.1 / 3.0 / 2.1 |
| Linux | x86 / x64 | 3.1 / 3.0 / 2.1 | |
| Windows | x86 / x64 | 3.1 / 3.0 / 2.1 |
Prints highlighted html of C# source code to the terminal:
using ColorfulCode;
// get a syntax
SyntaxSet ss = SyntaxSet.LoadDefaults();
Syntax syntax = ss.FindByExtension("cs"); // syntax for C#
// get a theme
ThemeSet ts = ThemeSet.LoadDefaults();
Theme theme = ts["InspiredGitHub"]; // github syntax highlighting theme
// highlight source code to html
string sourceCode = @"using ColorfulCode;
namespace HelloWorld {
class Program {
static Main(string[] args) {
Console.WriteLine(""Hello World"");
}
}
}
";
string html = syntax.HighlightToHtml(sourceCode, theme);
Console.WriteLine(html);Run the code, you can see the highlighted html on your console:
<pre style="background-color:#ffffff;">
<span style="font-weight:bold;color:#a71d5d;">using </span><span style="color:#323232;">ColorfulCode;
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">namespace </span><span style="color:#323232;">HelloWorld {
</span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">class </span><span style="color:#0086b3;">Program </span><span style="color:#323232;">{
</span><span style="color:#323232;"> </span><span style="font-weight:bold;color:#a71d5d;">static </span><span style="font-weight:bold;color:#795da3;">Main</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">string</span><span style="color:#323232;">[] args) {
</span><span style="color:#323232;"> Console.WriteLine(</span><span style="color:#183691;">"Hello World"</span><span style="color:#323232;">);
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> }
</span><span style="color:#323232;">}
</span></pre>Default SyntaxSet:
Plain Text, ASP, HTML (ASP), ActionScript, AppleScript, Batch File, NAnt Build File, C#, C++, C, CSS, Clojure, D, Diff, Erlang, HTML (Erlang), Go, Graphviz (DOT), Groovy, HTML, Haskell, Literate Haskell, Java Server Page (JSP), Java, JavaDoc, Java Properties, JSON, JavaScript, Regular Expressions (Javascript), BibTeX, LaTeX Log, LaTeX, TeX, Lisp, Lua, Make Output, Makefile, Markdown, MultiMarkdown, MATLAB, OCaml, OCamllex, OCamlyacc, camlp4, Objective-C++, Objective-C, PHP Source, PHP, Pascal, Perl, Python, Regular Expressions (Python), R Console, R, Rd (R Documentation), HTML (Rails), JavaScript (Rails), Ruby Haml, Ruby on Rails, SQL (Rails), Regular Expression, reStructuredText, Ruby, Cargo Build Results, Rust, SQL, Scala, Bourne Again Shell (bash), Shell-Unix-Generic, commands-builtin-shell-bash, HTML (Tcl), Tcl, Textile, XML, YAML
Default ThemeSet:
InspiredGitHubSolarized (dark)Solarized (light)base16-eighties.darkbase16-mocha.darkbase16-ocean.darkbase16-ocean.light
- Support highlight code to HTML
- Syntax: Include more syntax definitions of commonly used language into default syntax set
- Syntax: Support custom syntax definitions
- Theme: Support custom theme definitions
- Support highlight code for terminal
ColorfulCode is licensed under the Apache-2.0 license.