diff --git a/ColorCode.Core/Common/LanguageId.cs b/ColorCode.Core/Common/LanguageId.cs index 4458b08..6b5d6d3 100644 --- a/ColorCode.Core/Common/LanguageId.cs +++ b/ColorCode.Core/Common/LanguageId.cs @@ -18,6 +18,7 @@ namespace ColorCode.Common public const string Html = "html"; public const string Java = "java"; public const string JavaScript = "javascript"; + public const string Json = "json"; public const string TypeScript = "typescript"; public const string Php = "php"; public const string PowerShell = "powershell"; diff --git a/ColorCode.Core/Common/ScopeName.cs b/ColorCode.Core/Common/ScopeName.cs index 6267bda..e5c9100 100644 --- a/ColorCode.Core/Common/ScopeName.cs +++ b/ColorCode.Core/Common/ScopeName.cs @@ -19,6 +19,10 @@ namespace ColorCode.Common public const string HtmlOperator = "HTML Operator"; public const string HtmlServerSideScript = "HTML Server-Side Script"; public const string HtmlTagDelimiter = "Html Tag Delimiter"; + public const string JsonKey = "Json Key"; + public const string JsonString = "Json String"; + public const string JsonNumber = "Json Number"; + public const string JsonConst = "Json Const"; public const string Keyword = "Keyword"; public const string LanguagePrefix = "&"; public const string PlainText = "Plain Text"; diff --git a/ColorCode.Core/Compilation/Languages/JavaScript.cs b/ColorCode.Core/Compilation/Languages/JavaScript.cs index 3bd3d4a..ed1ef50 100644 --- a/ColorCode.Core/Compilation/Languages/JavaScript.cs +++ b/ColorCode.Core/Compilation/Languages/JavaScript.cs @@ -78,9 +78,6 @@ namespace ColorCode.Compilation.Languages { case "js": return true; - - case "json": - return true; default: return false; diff --git a/ColorCode.Core/Compilation/Languages/Json.cs b/ColorCode.Core/Compilation/Languages/Json.cs new file mode 100644 index 0000000..ab568d9 --- /dev/null +++ b/ColorCode.Core/Compilation/Languages/Json.cs @@ -0,0 +1,78 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using ColorCode.Common; + +namespace ColorCode.Compilation.Languages +{ + /// + /// Leverage the regex from https://gist.github.com/goodmami/02f344e8c9a22fc9ac879230a9b9e071#version-2 + /// for parsing the key, string value, number, and constant for a JSON code block. + /// + public class Json : ILanguage + { + private const string Regex_String = @"""[^""\\]*(?:\\[^\r\n]|[^""\\]*)*"""; + private const string Regex_Number = @"-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?"; + + public string Id + { + get { return LanguageId.Json; } + } + + public string Name + { + get { return "JSON"; } + } + + public string CssClassName + { + get { return "json"; } + } + + public string FirstLinePattern + { + get { return null; } + } + + public IList Rules + { + get + { + return new List + { + new LanguageRule( + $@"[,\{{]\s*({Regex_String})\s*:", + new Dictionary + { + {1, ScopeName.JsonKey} + }), + new LanguageRule( + Regex_String, + new Dictionary + { + {0, ScopeName.JsonString} + }), + new LanguageRule( + Regex_Number, + new Dictionary + { + {0, ScopeName.JsonNumber} + }), + new LanguageRule( + @"\b(true|false|null)\b", + new Dictionary + { + {1, ScopeName.JsonConst} + }), + }; + } + } + + public bool HasAlias(string lang) + { + return false; + } + } +} diff --git a/ColorCode.Core/Languages.cs b/ColorCode.Core/Languages.cs index 447d727..c67a648 100644 --- a/ColorCode.Core/Languages.cs +++ b/ColorCode.Core/Languages.cs @@ -42,6 +42,7 @@ namespace ColorCode Load(); Load(); Load(); + Load(); Load(); Load(); Load(); diff --git a/ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs b/ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs index 3ff666b..cee0469 100644 --- a/ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs +++ b/ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs @@ -117,6 +117,26 @@ namespace ColorCode.Styling Foreground = Red, ReferenceName = "htmlEntity" }, + new Style(ScopeName.JsonKey) + { + Foreground = DarkOrange, + ReferenceName = "jsonKey" + }, + new Style(ScopeName.JsonString) + { + Foreground = DarkCyan, + ReferenceName = "jsonString" + }, + new Style(ScopeName.JsonNumber) + { + Foreground = BrightGreen, + ReferenceName = "jsonNumber" + }, + new Style(ScopeName.JsonConst) + { + Foreground = BrightPurple, + ReferenceName = "jsonConst" + }, new Style(ScopeName.XmlAttribute) { Foreground = VSDarkXMLAttribute, diff --git a/ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs b/ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs index dd0b59b..b8ee1c5 100644 --- a/ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs +++ b/ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs @@ -101,6 +101,26 @@ namespace ColorCode.Styling Foreground = Red, ReferenceName = "htmlEntity" }, + new Style(ScopeName.JsonKey) + { + Foreground = DarkOrange, + ReferenceName = "jsonKey" + }, + new Style(ScopeName.JsonString) + { + Foreground = DarkCyan, + ReferenceName = "jsonString" + }, + new Style(ScopeName.JsonNumber) + { + Foreground = BrightGreen, + ReferenceName = "jsonNumber" + }, + new Style(ScopeName.JsonConst) + { + Foreground = BrightPurple, + ReferenceName = "jsonConst" + }, new Style(ScopeName.XmlAttribute) { Foreground = Red, diff --git a/ColorCode.Core/Styling/StyleDictionary.cs b/ColorCode.Core/Styling/StyleDictionary.cs index 7dfcedd..507c883 100644 --- a/ColorCode.Core/Styling/StyleDictionary.cs +++ b/ColorCode.Core/Styling/StyleDictionary.cs @@ -39,5 +39,8 @@ namespace ColorCode.Styling public const string OliveDrab = "#FF6B8E23"; public const string DarkOliveGreen = "#FF556B2F"; public const string DarkCyan = "#FF008B8B"; + public const string DarkOrange = "#FFFF8700"; + public const string BrightGreen = "#FF00d700"; + public const string BrightPurple = "#FFaf87ff"; } } \ No newline at end of file