ColorCode-Universal
repository·main·Indexed 42 days ago
https://github.com/communitytoolkit/colorcode-universalA .NET Standard port of ColorCode that provides syntax highlighting for various output formats. It separates core parsing logic from output formatters to support targets including HTML, UWP, and WindowsAppSDK via packages such as ColorCode.Core, ColorCode.HTML, and ColorCode.UWP.
What's inside ColorCode-Universal
- ColorCode-Universal is a .NET Standard port of the original ColorCode library. It provides syntax highlighting for code by separating the core parsing logic from the output formatters. This allows the library to produce colorized code for various targets, including HTML, UWP, and WindowsAppSDK.
Generate colorized HTML with inline styles
mainTo generate HTML with syntax highlighting using inline CSS styles, install the
ColorCode.HTMLNuGet package and use theHtmlFormatterclass. TheGetHtmlStringmethod takes the source code string and aLanguagesidentifier.var formatter = new HtmlFormatter(); var html = formatter.GetHtmlString(csharpstring, Languages.CSharp);Generate colorized HTML with external CSS classes
mainTo generate HTML that uses CSS classes instead of inline styles, use the
HtmlClassFormatterfrom theColorCode.HTMLpackage. You must callGetCSSString()to retrieve the required CSS and manually include it in your HTML<head>.var formatter = new HtmlClassFormatter(); var html = formatter.GetHtmlString(csharpstring, Languages.CSharp); var css = formatter.GetCSSString();Render colorized code to UWP RichTextBlock
mainTo render syntax-highlighted code directly into a UWP
RichTextBlock, use theRichTextBlockFormatterfrom theColorCode.UWPpackage. You can either format into a specificPresentationBlockor append to an existingInlineCollection.var csharpstring = "public void Method()\n{\n}"; var formatter = new RichTextBlockFormatter(); formatter.FormatRichTextBlock(csharpstring, Languages.CSharp, PresentationBlock); // Option 2: Append to an existing InlineCollection var paragraph = new Paragraph(); var csharpstring = "public void Method()\n{\n}"; var formatter = new RichTextBlockFormatter(); formatter.FormatInlines(csharpstring, Languages.CSharp, paragraph.Inlines);Find a language by its identifier
mainYou can manually retrieve a language object by providing its string identifier using
ColorCode.Languages.FindById(string id). Refer toLanguageId.csin theColorCode.Corepackage for the full list of supported language identifiers.var language = ColorCode.Languages.FindById("java");ColorCode-Universal NuGet Packages
mainThe project is split into several packages depending on your target output:
Package Description Supports ColorCode.CoreThe Core Library, containing the Parser, and the classes to create your own formatter. .NET Standard 1.4 ColorCode.HTMLThe Library containing the HtmlFormatter, and theHtmlClassFormatter, for rendering Html from the Colorized Code..NET Standard 1.4 ColorCode.UWPThe Library containing the RichTextBlockFormatter, for rendering the Colorized Code to a RichTextBlock.Tested against 10.0.14393.0 and up.