Giter Site home page Giter Site logo

communitytoolkit / colorcode-universal Goto Github PK

View Code? Open in Web Editor NEW
212.0 9.0 40.0 2.45 MB

This is a port of ColorCode to .NET Standard. The original Html only formatter has been separated from the Logic, so now it can produce Syntax Highlighted code for any output. This Project can currently produce HTML, and Render to UWP RichTextBlocks.

License: Other

C# 95.18% PowerShell 4.82%
colorcode uwp formatter html colorcode-universal uwp-richtextblocks dotnet dotnet-standard

colorcode-universal's Introduction

ColorCode-Universal

This is a port of ColorCode to .NET Standard. The original Html only formatter has been separated from the Logic, so now it can produce Syntax Highlighted code for any output.

This Project can currently produce HTML, and Render to UWP and WindowsAppSDK RichTextBlocks.

Usage

HTML

To use ColorCode to create colorised HTML, ensure you have installed the ColorCode.HTML NuGet package, then you can use the following code:

var csharpstring = "public void Method()\n{\n}";
var formatter = new HtmlFormatter();
var html = formatter.GetHtmlString(csharpstring, Languages.CSharp);

This will create the formatting into the HTML, via inline styling. To use CSS to format the HTML instead, use the following:

var csharpstring = "public void Method()\n{\n}";
var formatter = new HtmlClassFormatter();
var html = formatter.GetHtmlString(csharpstring, Languages.CSharp);
var css = formatter.GetCSSString();

You will then have to manually reference the css, from the HTML head.

UWP

To use ColorCode to render colorized code to a RichTextBlock, then you can use the following code:

var csharpstring = "public void Method()\n{\n}";
var formatter = new RichTextBlockFormatter();
formatter.FormatRichTextBlock(csharpstring, Languages.CSharp, PresentationBlock);

Or you can append onto an existing InlineCollection, with the following code:

var paragraph = new Paragraph();
var csharpstring = "public void Method()\n{\n}";
var formatter = new RichTextBlockFormatter();
formatter.FormatInlines(csharpstring, Languages.CSharp, paragraph.Inlines);

Determining the Programming Language

To get the Programming Language manually, you can provide the identifier name, with the following code:

var language = ColorCode.Languages.FindById("java");

See LanguageId.cs for the list of available Languages to parse.

Packages

Package Description Supports
ColorCode.Core The Core Library, containing the Parser, and the classes to create your own formatter. .NET Standard 1.4
ColorCode.HTML The Library containing the HtmlFormatter, and the HtmlClassFormatter, for rendering Html from the Colorized Code. .NET Standard 1.4
ColorCode.UWP The Library containing the RichTextBlockFormatter, for rendering the Colorized Code to a RichTextBlock. Tested against 10.0.14393.0 and up.

Feedback and Requests

Please use GitHub issues for bug reports and feature requests.

Contributing

Want to help out and add some more parsing support, or add a new Formatter platform? Submit a PR!

License

MIT

colorcode-universal's People

Contributors

0xced avatar arlodotexe avatar arthurrump avatar azchohfi avatar bashirsouid-ms avatar calman102 avatar csainty avatar daanx avatar daxian-dbw avatar half-ogre avatar michael-hawker avatar nmetulev avatar williamabradley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

colorcode-universal's Issues

Nuget installation issues targeting .Net Framework 4.5.2

Hello, i tried to add the ColorCode.HTML to my wpf project using the NuGet Package Manager Console but got this error :
"Install-Package : Could not install package 'ColorCode.HTML 2.0.6'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
At line:1 char:16

  • Install-Package <<<< ColorCode.HTML -IgnoreDependencies
  • CategoryInfo : NotSpecified: (:) [Install-Package], Exception
  • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand".

I couldn't find any solutions from the internet, please help!!

JSON Parsing - deadlock/stuck on parsing Json to HTML

Im trying to parse this JSON sipped to my Blazor page. But because of some weird RegEx parsing issue the process get stuck without any Exception. Can somebody tell me where there could be a Problem?
here you can find my test project: ColorCodeTest

This is my Test Code

string _jsonString3 = "{\r\n \"raw_causes\": [\r\n      \"Winterglatter Fahrbahn\",\r\n      \"Nicht angepasste Geschwindigkeit\",\r\n      \"test3\"\r\n    ]\r\n      }";
try
{
    var _formatter = new HtmlFormatter();
    var language = ColorCode.Languages.FindById("json");
    _jsonHtml = _formatter.GetHtmlString(_jsonString3, language);

}
catch (Exception)
{

    throw;
}

It stuck after the array Element \"Winterglatter Fahrbahn\", when it call regexMatch = regexMatch.NextMatch(); and I have no idear why this happends

        private void Parse(string sourceCode,
                           CompiledLanguage compiledLanguage,
                           Action<string, IList<Scope>> parseHandler)
        {
            Match regexMatch = compiledLanguage.Regex.Match(sourceCode);

            if (!regexMatch.Success)
                parseHandler(sourceCode, new List<Scope>());
            else
            {
                int currentIndex = 0;

                try
                {
                    while (regexMatch.Success)
                    {
                        string sourceCodeBeforeMatch = sourceCode.Substring(currentIndex, regexMatch.Index - currentIndex);
                        if (!string.IsNullOrEmpty(sourceCodeBeforeMatch))
                            parseHandler(sourceCodeBeforeMatch, new List<Scope>());

                        string matchedSourceCode = sourceCode.Substring(regexMatch.Index, regexMatch.Length);
                        if (!string.IsNullOrEmpty(matchedSourceCode))
                        {
                            List<Scope> capturedStylesForMatchedFragment = GetCapturedStyles(regexMatch, regexMatch.Index, compiledLanguage);
                            List<Scope> capturedStyleTree = CreateCapturedStyleTree(capturedStylesForMatchedFragment);
                            parseHandler(matchedSourceCode, capturedStyleTree);
                        }

                        currentIndex = regexMatch.Index + regexMatch.Length;
                        regexMatch = regexMatch.NextMatch();
                    }
                }
                catch (Exception ex)
                {

                    throw;
                }

                string sourceCodeAfterAllMatches = sourceCode.Substring(currentIndex);
                if (!string.IsNullOrEmpty(sourceCodeAfterAllMatches))
                    parseHandler(sourceCodeAfterAllMatches, new List<Scope>());
            }
        }

Wrap source code in code tags

Referencing this issue on markdown-colorcode, this package overwrites some of the behavior used in Markdig that appears to be in line with the CommonMark spec, that is, this package does not wrap code using a code element. Additionally, if a language is present, the spec specifies a class="language-[lang]" attribute as well.

I have not started a fork because I am not sure whether this would be a breaking change or not, however, the general change seems rather simple, adding the following methods to HtmlClassFormatter and then wrapping the inner text in WriteHeader and WriteFooter.

private void WriteHeaderCodeStart(ILanguage language)
{
    WriteElementStart("code", language.CssClassName);
}

private void WriteHeaderCodeEnd()
{
    WriteElementEnd("code");
}

In my case, this would make it easier to enforce consistent styling across my app.

Is it possible to add this DarkStyleSheet to the package so that people can use a darkmode?

//
// DarkModeStyleSheet.cs
//
// Author: Kees van Spelde <[email protected]>
//
// Copyright (c) 2021 Magic-Sessions. (www.magic-sessions.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System.Drawing;
using ColorCodeStandard.Common;

namespace ColorCodeStandard.Styling.StyleSheets
{
    /// <summary>
    /// Add's a dark style to the formatting
    /// </summary>
    public class DarkModeStyleSheet : IStyleSheet
    {
        /// <summary>
        /// Gets the dictionary of styles for the style sheet.
        /// </summary>
        public StyleDictionary Styles { get; }

        #region Constructor
        /// <summary>
        /// Add's a dark style to the formatting
        /// </summary>
        public DarkModeStyleSheet()
        {
            Styles = new StyleDictionary
            {
                new Style(ScopeName.PlainText) {Foreground = Color.White, CssClassName = "plainText"},
                // HTML
                new Style(ScopeName.HtmlServerSideScript) {Foreground = Color.Yellow, CssClassName = "htmlServerSideScript"},
                new Style(ScopeName.HtmlComment) {Foreground = Color.Green, CssClassName = "htmlComment"},
                new Style(ScopeName.HtmlTagDelimiter) {Foreground = "#FF9D00".HexToColor(), CssClassName = "htmlTagDelimiter"},
                new Style(ScopeName.HtmlElementName) {Foreground = "#5DF1FF".HexToColor(), CssClassName = "htmlElementName"},
                new Style(ScopeName.HtmlAttributeName) {Foreground = "#EFDD22".HexToColor(), CssClassName = "htmlAttributeName"},
                new Style(ScopeName.HtmlAttributeValue) {Foreground = "#3AD922".HexToColor(), CssClassName = "htmlAttributeValue"},
                new Style(ScopeName.HtmlOperator) {Foreground = Color.Red, CssClassName = "htmlOperator"},
                new Style(ScopeName.HtmlEntity) {Foreground = Color.White, CssClassName = "htmlEntity"},
                // XML
                new Style(ScopeName.XmlAttribute) {Foreground = "#EFDD22".HexToColor(), CssClassName = "xmlAttribute"},
                new Style(ScopeName.XmlAttributeQuotes) {Foreground = "#41A449".HexToColor(), CssClassName = "xmlAttributeQuotes"},
                new Style(ScopeName.XmlAttributeValue) {Foreground = "#3AD922".HexToColor(), CssClassName = "xmlAttributeValue"},
                new Style(ScopeName.XmlCDataSection) {Foreground = Color.Gray, CssClassName = "xmlCDataSection"},
                new Style(ScopeName.XmlDelimiter) {Foreground = "#FF9D00".HexToColor(), CssClassName = "xmlDelimiter"},
                new Style(ScopeName.XmlName) {Foreground = "#5DF1FF".HexToColor(), CssClassName = "xmlName"},

                // Custom
                new Style(ScopeName.Keyword) {Foreground = "#569CD6".HexToColor(), CssClassName = "keyword"},
                new Style(ScopeName.PreprocessorKeyword) {Foreground = "#9B9B9B".HexToColor(), CssClassName = "preprocessorKeyword"},

                // Comments
                new Style(ScopeName.Comment) {Foreground = Color.Green, CssClassName = "comment"},
                new Style(ScopeName.XmlDocComment) {Foreground = Color.Green, CssClassName = "xmlDocComment"},
                new Style(ScopeName.XmlDocTag) {Foreground = Color.Green, CssClassName = "xmlDocTag"},
                new Style(ScopeName.XmlComment) {Foreground = Color.Green, CssClassName = "xmlComment"},
                new Style(ScopeName.ClassName) {Foreground = "#4EC9B0".HexToColor(), CssClassName = "className"},

                new Style(ScopeName.String) {Foreground = "#D69D85".HexToColor(), CssClassName = "string"},
                new Style(ScopeName.StringCSharpVerbatim) {Foreground = "#D69D85".HexToColor(), CssClassName = "stringCSharpVerbatim"},
                
                // SQL
                new Style(ScopeName.SqlSystemFunction) {Foreground = "#EF6273".HexToColor(), CssClassName = "sqlSystemFunction"},

                // CSS
                new Style(ScopeName.CssSelector) {Foreground = "#3AD900".HexToColor(), CssClassName = "cssSelector"},
                new Style(ScopeName.CssPropertyName) {Foreground = "#80FFBA".HexToColor(), CssClassName = "cssPropertyName"},
                new Style(ScopeName.CssPropertyValue) {Foreground = "#EF6273".HexToColor(), CssClassName = "cssPropertyValue"},

                // PowerShell
                new Style(ScopeName.PowerShellAttribute) {Foreground = "#EFDD22".HexToColor(), CssClassName = "powershellAttribute"},
                new Style(ScopeName.PowerShellOperator) {Foreground = Color.Red, CssClassName = "powershellOperator"},
                new Style(ScopeName.PowerShellType) {Foreground = "#9EFFFF".HexToColor(), CssClassName = "powershellType"},
                new Style(ScopeName.PowerShellVariable) {Foreground = "#5DF1FF".HexToColor(), CssClassName = "powershellVariable"},
            };
        }
        #endregion
    }
}

Exception "An item with the same key has already been added."

When using multiple instances of HtmlFormatter an error can occur with the message "An item with the same key has already been added." when adding a compiled language. The following snippet reproduces this reliably (at least for me):

var tasks = new Task[100];
for (int i = 0; i < 100; i++)
{
    tasks[i] = Task.Run(() =>
    {
        var code = "public void Method()\n{\n}";
        var formatter = new HtmlFormatter();
        var html = formatter.GetHtmlString(code, Languages.CSharp);
        Console.WriteLine(html);
    });
}
Task.WaitAll(tasks);

Stacktrace:

   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at ColorCode.Compilation.LanguageCompiler.Compile(ILanguage language) in C:\projects\colorcode-universal\ColorCode.Core\Compilation\LanguageCompiler.cs:line 67
   at ColorCode.Parsing.LanguageParser.Parse(String sourceCode, ILanguage language, Action`2 parseHandler) in C:\projects\colorcode-universal\ColorCode.Core\Parsing\LanguageParser.cs:line 30
   at ColorCode.HtmlFormatter.GetHtmlString(String sourceCode, ILanguage language) in C:\projects\colorcode-universal\ColorCode.HTML\HtmlFormatter.cs:line 47

I think this happens because the dictionary of compiled languages is locked per instance of the LanguageParser, so when multiple instances are used the static dictionary may get a race condition.

Add support for the lua language

Hey there, this project is used in another project that needs coloring for the lua language. Could this be added soon? Thanks!

Issue with ColorCode Build

C:\hostedtoolcache\windows\dotnet\sdk\6.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(358,5): error NETSDK1127: The targeting pack Microsoft.NETCore.App is not installed. Please restore and try again. [D:\a\1\s\ColorCode.WinUI\ColorCode.WinUI.csproj]

Something happened in the last couple of weeks and now something is wrong in the build environment.

@Arlodotexe mind taking a look?

Add UnitTests

We should have more robust Unit Tests that we can run in the CI pipeline (like the main Toolkit repo has now).

relicense MIT for project

I have reapplied mit license im not sure who to report to because some how lost communication with ocm supervisor

Why are there multiple CSS classes that mean the same thing?

There's the following pairs with the same semantic meaning:

  • htmlElementName and xmlName
  • htmlAttributeName and xmlAttribute
  • comment and htmlComment
  • string, jsonString, xmlAttributeValue and htmlAttributeValue
  • cssPropertyName and jsonKey

Is there a reason these are separated out instead of just using the same class name?

Relicense Project to MIT

The ColorCode maintainers are considering changing the licensing on the project. Currently, the project is MS-PL, but in order to spur wider adoption and possibly a greater contributor base, we'd like to change the license to MIT, a highly-popular OSI-approved license. This license change will also align with the rest of the .NET Foundation work we do within the Toolkit organization. This contemplated license change is in part why we haven't merged any pending PRs within the project since we took ownership of the project.

We are checking with all past contributors to get an acknowledgement that they agree to this license change. Can you please respond to this issue with your agreement (or indicating you do not agree that your contributions may be licensed under MIT)?


FYI @arthurrump @csainty

@lukehoban according to your GitHub you used to work at Microsoft, so I believe you're the same Luke Hoban who made contributions previously when the project was on GitHub as part of your work at the company?

@WilliamABradley @daanx @bashirsouid-ms @csano @half-ogre (also attributed as AnglicanGeek/Drew Miller from CodePlex times) - I know I've spoken with you offline about this, but having a formal response here from you too would help for completeness and history.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.