ColorCode-Universal

repository·main·Indexed 42 days ago

https://github.com/communitytoolkit/colorcode-universal

A .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.

Tokens
919
Snippets
4
Records
6
Agent score
4%

What's inside ColorCode-Universal

  1. Overview of ColorCode-Universal

    main
    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.
  2. Generate colorized HTML with inline styles

    main

    To generate HTML with syntax highlighting using inline CSS styles, install the ColorCode.HTML NuGet package and use the HtmlFormatter class. The GetHtmlString method takes the source code string and a Languages identifier.

    var formatter = new HtmlFormatter();
    var html = formatter.GetHtmlString(csharpstring, Languages.CSharp);
  3. Generate colorized HTML with external CSS classes

    main

    To generate HTML that uses CSS classes instead of inline styles, use the HtmlClassFormatter from the ColorCode.HTML package. You must call GetCSSString() 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();
  4. Render colorized code to UWP RichTextBlock

    main

    To render syntax-highlighted code directly into a UWP RichTextBlock, use the RichTextBlockFormatter from the ColorCode.UWP package. You can either format into a specific PresentationBlock or append to an existing InlineCollection.

    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);
  5. Find a language by its identifier

    main

    You can manually retrieve a language object by providing its string identifier using ColorCode.Languages.FindById(string id). Refer to LanguageId.cs in the ColorCode.Core package for the full list of supported language identifiers.

    var language = ColorCode.Languages.FindById("java");
  6. ColorCode-Universal NuGet Packages

    main

    The project is split into several packages depending on your target output:

    PackageDescriptionSupports
    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 the HtmlClassFormatter, 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.