From 8849df92e1c52c653a999961778ce4cbc191ea56 Mon Sep 17 00:00:00 2001 From: ColdForeign Date: Sat, 3 Aug 2024 18:36:16 +0300 Subject: [PATCH] Fixed example code styles --- .../Components/Docs/SectionContent.razor.cs | 32 ++++++----- ...tallServicesForBlazorServerManualCode.html | 8 +-- .../InstallServicesManualCode.html | 2 +- .../InstallServicesOverrideGlobalCode.html | 2 +- .../InstallServicesOverrideInternalCode.html | 2 +- .../Client/Styles/layout/_markdown.scss | 12 ++-- .../CodeSnippetsCompiler.cs | 12 ++-- .../DocStrings.cs | 9 ++- .../ExamplesMarkup.cs | 56 +++++++++++-------- .../Cropper.Blazor.Client.Compiler/Paths.cs | 42 ++------------ .../StyleDictionary.KebabCase.cs | 53 ++++++++++++++++++ 11 files changed, 133 insertions(+), 97 deletions(-) create mode 100644 src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/StyleDictionary.KebabCase.cs diff --git a/src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs b/src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs index 63082e25..bd87194b 100644 --- a/src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs +++ b/src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs @@ -125,28 +125,30 @@ RenderFragment CodeComponent(string code) => builder => throw new KeyNotFoundException($"'.{code}Code.html' code not exist"); } - using var stream = typeof(SectionContent).Assembly.GetManifestResourceStream(key!); - using var reader = new StreamReader(stream!); - var read = reader.ReadToEnd(); - - if (!string.IsNullOrEmpty(HighLight)) + using (var stream = typeof(SectionContent).Assembly.GetManifestResourceStream(key)) + using (var reader = new StreamReader(stream)) { - if (HighLight.Contains(',')) + var read = reader.ReadToEnd(); + + if (!string.IsNullOrEmpty(HighLight)) { - var highlights = HighLight.Split(","); + if (HighLight.Contains(',')) + { + var highlights = HighLight.Split(","); - foreach (var value in highlights) + foreach (var value in highlights) + { + read = Regex.Replace(read, $"{value}(?=\\s|\")", $"$&"); + } + } + else { - read = Regex.Replace(read, $"{value}(?=\\s|\")", $"$&"); + read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"$&"); } } - else - { - read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"$&"); - } - } - builder.AddMarkupContent(0, HttpUtility.HtmlDecode(read)); + builder.AddMarkupContent(0, read); + } } catch (Exception ex) { diff --git a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesForBlazorServerManualCode.html b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesForBlazorServerManualCode.html index 8ea69232..71121755 100644 --- a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesForBlazorServerManualCode.html +++ b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesForBlazorServerManualCode.html @@ -3,13 +3,13 @@
 using Cropper.Blazor.Extensions;
 
-builder.Services.AddServerSideBlazor()
-	.AddHubOptions(options => 
+builder.Services.AddServerSideBlazor()
+	.AddHubOptions(options => 
 	{
-		options.MaximumReceiveMessageSize = 32 * 1024 * 100;
+		options.MaximumReceiveMessageSize = 32 * 1024 * 100;
 	});
 
-app.MapBlazorHub();
+app.MapBlazorHub();
 
diff --git a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesManualCode.html b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesManualCode.html index 6514b805..09e359d0 100644 --- a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesManualCode.html +++ b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesManualCode.html @@ -3,7 +3,7 @@
 using Cropper.Blazor.Extensions;
 
-builder.Services.AddCropper();
+builder.Services.AddCropper();
 
 
diff --git a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideGlobalCode.html b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideGlobalCode.html index a318c0e5..81b2e465 100644 --- a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideGlobalCode.html +++ b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideGlobalCode.html @@ -4,7 +4,7 @@ using Cropper.Blazor.Extensions; // Override full global path to cropperJSInterop.min.js module -builder.Services.AddCropper(new CropperJsInteropOptions() +builder.Services.AddCropper(new CropperJsInteropOptions() { IsActiveGlobalPath = true, GlobalPathToCropperModule = "{StartUrlWithPath}/_content/Cropper.Blazor/cropperJsInterop.min.js" diff --git a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideInternalCode.html b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideInternalCode.html index 2a55eb52..56f136c4 100644 --- a/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideInternalCode.html +++ b/src/Cropper.Blazor/Client/Pages/ManualMarkdown/InstallServicesOverrideInternalCode.html @@ -4,7 +4,7 @@ using Cropper.Blazor.Extensions; // Override internal path to cropperJSInterop.min.js module -builder.Services.AddCropper(new CropperJsInteropOptions() +builder.Services.AddCropper(new CropperJsInteropOptions() { DefaultInternalPathToCropperModule = "{YourPath}/_content/Cropper.Blazor/cropperJsInterop.min.js" }); diff --git a/src/Cropper.Blazor/Client/Styles/layout/_markdown.scss b/src/Cropper.Blazor/Client/Styles/layout/_markdown.scss index 5726b7f7..04064d4b 100644 --- a/src/Cropper.Blazor/Client/Styles/layout/_markdown.scss +++ b/src/Cropper.Blazor/Client/Styles/layout/_markdown.scss @@ -40,7 +40,7 @@ background-color: rgb(255 255 255 / 15%); } - & .atSign { + & .at-sign { color: #8323d8; } @@ -50,7 +50,7 @@ } & .csharp { - & .atSign { + & .at-sign { color: #000; background-color: #d2d295; } @@ -71,7 +71,7 @@ color: #4ec9b0; } - & .localVar { + & .local-var { color: #9cdcfe; } @@ -119,7 +119,7 @@ } & .csharp { - & .atSign { + & .at-sign { color: #8323d8; } @@ -139,7 +139,7 @@ color: #1ec8a5; } - & .localVar { + & .local-var { color: #2196f3; } @@ -236,7 +236,7 @@ background-color: var(--mud-palette-grey-light); } - & .atSign { + & .at-sign { color: #8323d8; } diff --git a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/CodeSnippetsCompiler.cs b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/CodeSnippetsCompiler.cs index ebc63027..df8d8faf 100644 --- a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/CodeSnippetsCompiler.cs +++ b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/CodeSnippetsCompiler.cs @@ -10,14 +10,14 @@ public static partial class CodeSnippetsCompiler { public static bool Execute() { - var paths = new Paths(); + var success = true; try { var currentCode = string.Empty; - if (File.Exists(paths.SnippetsFilePath)) + if (File.Exists(Paths.SnippetsFilePath)) { - currentCode = File.ReadAllText(paths.SnippetsFilePath); + currentCode = File.ReadAllText(Paths.SnippetsFilePath); } var cb = new CodeBuilder(); @@ -29,7 +29,7 @@ public static bool Execute() cb.AddLine("{"); cb.IndentLevel++; - foreach (var entry in Directory.EnumerateFiles(paths.DocsDirPath, "*.razor", SearchOption.AllDirectories) + foreach (var entry in Directory.EnumerateFiles(Paths.DocsDirPath, "*.razor", SearchOption.AllDirectories) .OrderBy(e => e.Replace("\\", "/"), StringComparer.Ordinal)) { var filename = Path.GetFileName(entry); @@ -46,12 +46,12 @@ public static bool Execute() if (currentCode != cb.ToString()) { - File.WriteAllText(paths.SnippetsFilePath, cb.ToString()); + File.WriteAllText(Paths.SnippetsFilePath, cb.ToString()); } } catch (Exception e) { - Console.WriteLine($"Error generating {paths.SnippetsFilePath} : {e.Message}"); + Console.WriteLine($"Error generating {Paths.SnippetsFilePath} : {e.Message}"); success = false; } diff --git a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/DocStrings.cs b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/DocStrings.cs index 2e6302f1..e5d57f88 100644 --- a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/DocStrings.cs +++ b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/DocStrings.cs @@ -15,14 +15,13 @@ public partial class DocStrings public bool Execute() { - var paths = new Paths(); var success = true; try { var currentCode = string.Empty; - if (File.Exists(paths.DocStringsFilePath)) + if (File.Exists(Paths.DocStringsFilePath)) { - currentCode = File.ReadAllText(paths.DocStringsFilePath); + currentCode = File.ReadAllText(Paths.DocStringsFilePath); } var cb = new CodeBuilder(); @@ -110,12 +109,12 @@ public bool Execute() if (currentCode != cb.ToString()) { - File.WriteAllText(paths.DocStringsFilePath, cb.ToString()); + File.WriteAllText(Paths.DocStringsFilePath, cb.ToString()); } } catch (Exception e) { - Console.WriteLine($"Error generating {paths.DocStringsFilePath} : {e.Message}"); + Console.WriteLine($"Error generating {Paths.DocStringsFilePath} : {e.Message}"); success = false; } diff --git a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/ExamplesMarkup.cs b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/ExamplesMarkup.cs index e1affea5..0fb25302 100644 --- a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/ExamplesMarkup.cs +++ b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/ExamplesMarkup.cs @@ -3,14 +3,14 @@ using System.Text; using System.Text.RegularExpressions; using ColorCode; +using ColorCode.Styling; namespace Cropper.Blazor.Client.Compiler { - public class ExamplesMarkup + public partial class ExamplesMarkup { public bool Execute() { - var paths = new Paths(); var newFiles = new StringBuilder(); var success = true; var noOfFilesUpdated = 0; @@ -18,15 +18,15 @@ public bool Execute() try { - var formatter = new HtmlClassFormatter(); + var formatter = new HtmlClassFormatter(StyleDictionaryKebabCase.KebabCase); var lastCheckedTime = new DateTime(); - if (File.Exists(paths.NewFilesToBuildPath)) + if (File.Exists(Paths.NewFilesToBuildPath)) { - var lastNewFilesToBuild = new FileInfo(paths.NewFilesToBuildPath); + var lastNewFilesToBuild = new FileInfo(Paths.NewFilesToBuildPath); lastCheckedTime = lastNewFilesToBuild.LastWriteTime; } - var directoryInfo = new DirectoryInfo(paths.DocsDirPath); + var directoryInfo = new DirectoryInfo(Paths.DocsDirPath); foreach (var entry in directoryInfo.GetFiles("*.razor", SearchOption.AllDirectories)) { @@ -50,13 +50,13 @@ public bool Execute() var src = StripComponentSource(entry.FullName); var blocks = src.Split("@code"); - var blocks0 = Regex.Replace(blocks[0], @"", string.Empty) + var blocks0 = DocsFrameEndTagRegularExpression().Replace(blocks[0], string.Empty) .Replace("@", "PlaceholdeR") .Trim(); // Note: the @ creates problems and thus we replace it with an unlikely placeholder and in the markup replace back. var html = formatter.GetHtmlString(blocks0, Languages.Html).Replace("PlaceholdeR", "@"); - html = AttributePostprocessing(html).Replace("@", "@"); + html = AttributePostprocessing(html).Replace("@", "@"); var currentCode = string.Empty; if (File.Exists(markupPath)) @@ -73,7 +73,7 @@ public bool Execute() { cb.AddLine( formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp) - .Replace("@", "@") + .Replace("@", "@") .ToLfLineEndings()); } @@ -94,7 +94,7 @@ public bool Execute() } } - File.WriteAllText(paths.NewFilesToBuildPath, newFiles.ToString()); + File.WriteAllText(Paths.NewFilesToBuildPath, newFiles.ToString()); } catch (Exception e) { @@ -110,21 +110,18 @@ public bool Execute() private static string StripComponentSource(string path) { var source = File.ReadAllText(path, Encoding.UTF8); - source = Regex.Replace(source, "@(namespace|layout|page) .+?\n", string.Empty); + source = NamespaceLayoutOrPageRegularExpression().Replace(source, string.Empty); return source.Trim(); } public static string AttributePostprocessing(string html) { - return Regex.Replace( - html, - @""(?'value'.*?)"", - new MatchEvaluator(m => - { - var value = m.Groups["value"].Value; - return - $@""{AttributeValuePostprocessing(value)}""; - })); + return HtmlAttributeValueSpanRegularExpression().Replace(html, new MatchEvaluator(m => + { + var value = m.Groups["value"].Value; + return + $@""{AttributeValuePostprocessing(value)}""; + })); } private static string AttributeValuePostprocessing(string value) @@ -133,18 +130,33 @@ private static string AttributeValuePostprocessing(string value) return value; if (value is "true" or "false") return $"{value}"; - if (Regex.IsMatch(value, "^[A-Z][A-Za-z0-9]+[.][A-Za-z][A-Za-z0-9]+$")) + if (AlphanumericDotAlphanumericRegularExpression().IsMatch(value)) { var tokens = value.Split('.'); return $"{tokens[0]}.{tokens[1]}"; } - if (Regex.IsMatch(value, "^@[A-Za-z0-9]+$")) + if (AlphanumericRegularExpression().IsMatch(value)) { return $"{value}"; } return $"{value}"; } + + [GeneratedRegex(@"")] + private static partial Regex DocsFrameEndTagRegularExpression(); + + [GeneratedRegex("@(namespace|layout|page) .+?\n")] + private static partial Regex NamespaceLayoutOrPageRegularExpression(); + + [GeneratedRegex(@""(?'value'.*?)"")] + private static partial Regex HtmlAttributeValueSpanRegularExpression(); + + [GeneratedRegex("^[A-Z][A-Za-z0-9]+[.][A-Za-z][A-Za-z0-9]+$")] + private static partial Regex AlphanumericDotAlphanumericRegularExpression(); + + [GeneratedRegex("^@[A-Za-z0-9]+$")] + private static partial Regex AlphanumericRegularExpression(); } } diff --git a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/Paths.cs b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/Paths.cs index 6da8f118..168e6dba 100644 --- a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/Paths.cs +++ b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/Paths.cs @@ -3,7 +3,7 @@ namespace Cropper.Blazor.Client.Compiler; -public class Paths +public static class Paths { private const string DocsDirectory = "Client"; private const string DocStringsFile = "DocStrings.generated.cs"; @@ -27,43 +27,13 @@ public static string SrcDirPath } } - public string DocsDirPath - { - get - { - return Directory.EnumerateDirectories(SrcDirPath, DocsDirectory).FirstOrDefault(); - } - } + public static string DocsDirPath => Directory.EnumerateDirectories(SrcDirPath, DocsDirectory).FirstOrDefault(); - public string DocsStringSnippetsDirPath - { - get - { - return Path.Join(DocsDirPath, "Models"); - } - } + public static string DocsStringSnippetsDirPath => Path.Join(DocsDirPath, "Models"); - public string SnippetsFilePath - { - get - { - return Path.Join(DocsStringSnippetsDirPath, SnippetsFile); - } - } + public static string SnippetsFilePath => Path.Join(DocsStringSnippetsDirPath, SnippetsFile); - public string DocStringsFilePath - { - get - { - return Path.Join(DocsStringSnippetsDirPath, DocStringsFile); - } - } + public static string DocStringsFilePath => Path.Join(DocsStringSnippetsDirPath, DocStringsFile); - public string NewFilesToBuildPath - { - get - { - return Path.Join(DocsDirPath, NewFilesToBuild); - } - } + public static string NewFilesToBuildPath => Path.Join(DocsDirPath, NewFilesToBuild); } diff --git a/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/StyleDictionary.KebabCase.cs b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/StyleDictionary.KebabCase.cs new file mode 100644 index 00000000..72dbbfae --- /dev/null +++ b/src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/StyleDictionary.KebabCase.cs @@ -0,0 +1,53 @@ +using ColorCode.Common; +using ColorCode.Styling; + +namespace Cropper.Blazor.Client.Compiler +{ + public class StyleDictionaryKebabCase + { + /// + /// A theme with reference names in kebab-case style. + /// + public static StyleDictionary KebabCase + { + get + { + return new StyleDictionary + { + new Style(ScopeName.String) + { + ReferenceName = "string" + }, + new Style(ScopeName.Keyword) + { + ReferenceName = "keyword" + }, + new Style(ScopeName.HtmlElementName) + { + ReferenceName = "html-element-name" + }, + new Style(ScopeName.HtmlAttributeName) + { + ReferenceName = "html-attribute-name" + }, + new Style(ScopeName.HtmlAttributeValue) + { + ReferenceName = "html-attribute-value" + }, + new Style(ScopeName.HtmlOperator) + { + ReferenceName = "html-operator" + }, + new Style(ScopeName.Comment) + { + ReferenceName = "comment" + }, + new Style(ScopeName.HtmlTagDelimiter) + { + ReferenceName = "html-tag-delimiter" + } + }; + } + } + } +}