Skip to content

Commit

Permalink
Fixed example code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdForeign committed Aug 3, 2024
1 parent ac202ee commit 8849df9
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 97 deletions.
32 changes: 17 additions & 15 deletions src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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|\")", $"<mark>$&</mark>");
}
}
else
{
read = Regex.Replace(read, $"{value}(?=\\s|\")", $"<mark>$&</mark>");
read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"<mark>$&</mark>");
}
}
else
{
read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"<mark>$&</mark>");
}
}

builder.AddMarkupContent(0, HttpUtility.HtmlDecode(read));
builder.AddMarkupContent(0, read);
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<pre>
<span class="keyword">using</span> Cropper.Blazor.Extensions;

<span class="localVar">builder</span>.Services.<span class="function">AddServerSideBlazor</span>()
.<span class="function">AddHubOptions</span>(<span class="localVar">options</span> =>
<span class="local-var">builder</span>.Services.<span class="function">AddServerSideBlazor</span>()
.<span class="function">AddHubOptions</span>(<span class="local-var">options</span> =>
{
<span class="localVar">options</span>.MaximumReceiveMessageSize = <span class="number">32</span> * <span class="number">1024</span> * <span class="number">100</span>;
<span class="local-var">options</span>.MaximumReceiveMessageSize = <span class="number">32</span> * <span class="number">1024</span> * <span class="number">100</span>;
});

<span class="localVar">app</span>.<span class="function">MapBlazorHub</span>();
<span class="local-var">app</span>.<span class="function">MapBlazorHub</span>();
</pre>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<pre>
<span class="keyword">using</span> Cropper.Blazor.Extensions;

<span class="localVar">builder</span>.Services.<span class="function">AddCropper</span>();
<span class="local-var">builder</span>.Services.<span class="function">AddCropper</span>();

</pre>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="keyword">using</span> Cropper.Blazor.Extensions;

<span class="comment">// Override full global path to cropperJSInterop.min.js module</span>
<span class="localVar">builder</span>.Services.<span class="function">AddCropper</span>(<span class="keyword">new</span> <span class="class">CropperJsInteropOptions</span>()
<span class="local-var">builder</span>.Services.<span class="function">AddCropper</span>(<span class="keyword">new</span> <span class="class">CropperJsInteropOptions</span>()
{
IsActiveGlobalPath = <span class="sharp-variable">true</span>,
GlobalPathToCropperModule = <span class="string">"{StartUrlWithPath}/_content/Cropper.Blazor/cropperJsInterop.min.js"</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="keyword">using</span> Cropper.Blazor.Extensions;

<span class="comment">// Override internal path to cropperJSInterop.min.js module</span>
<span class="localVar">builder</span>.Services.<span class="function">AddCropper</span>(<span class="keyword">new</span> <span class="class">CropperJsInteropOptions</span>()
<span class="local-var">builder</span>.Services.<span class="function">AddCropper</span>(<span class="keyword">new</span> <span class="class">CropperJsInteropOptions</span>()
{
DefaultInternalPathToCropperModule = <span class="string">"{YourPath}/_content/Cropper.Blazor/cropperJsInterop.min.js"</span>
});
Expand Down
12 changes: 6 additions & 6 deletions src/Cropper.Blazor/Client/Styles/layout/_markdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
background-color: rgb(255 255 255 / 15%);
}

& .atSign {
& .at-sign {
color: #8323d8;
}

Expand All @@ -50,7 +50,7 @@
}

& .csharp {
& .atSign {
& .at-sign {
color: #000;
background-color: #d2d295;
}
Expand All @@ -71,7 +71,7 @@
color: #4ec9b0;
}

& .localVar {
& .local-var {
color: #9cdcfe;
}

Expand Down Expand Up @@ -119,7 +119,7 @@
}

& .csharp {
& .atSign {
& .at-sign {
color: #8323d8;
}

Expand All @@ -139,7 +139,7 @@
color: #1ec8a5;
}

& .localVar {
& .local-var {
color: #2196f3;
}

Expand Down Expand Up @@ -236,7 +236,7 @@
background-color: var(--mud-palette-grey-light);
}

& .atSign {
& .at-sign {
color: #8323d8;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
56 changes: 34 additions & 22 deletions src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/ExamplesMarkup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
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;
var noOfFilesCreated = 0;

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))
{
Expand All @@ -50,13 +50,13 @@ public bool Execute()

var src = StripComponentSource(entry.FullName);
var blocks = src.Split("@code");
var blocks0 = Regex.Replace(blocks[0], @"</?DocsFrame>", 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("@", "<span class=\"atSign\">&#64;</span>");
html = AttributePostprocessing(html).Replace("@", "<span class=\"at-sign\">&#64;</span>");

var currentCode = string.Empty;
if (File.Exists(markupPath))
Expand All @@ -73,7 +73,7 @@ public bool Execute()
{
cb.AddLine(
formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp)
.Replace("@", "<span class=\"atSign\">&#64;</span>")
.Replace("@", "<span class=\"at-sign\">&#64;</span>")
.ToLfLineEndings());
}

Expand All @@ -94,7 +94,7 @@ public bool Execute()
}
}

File.WriteAllText(paths.NewFilesToBuildPath, newFiles.ToString());
File.WriteAllText(Paths.NewFilesToBuildPath, newFiles.ToString());
}
catch (Exception e)
{
Expand All @@ -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,
@"<span class=""html-attribute-value"">&quot;(?'value'.*?)&quot;</span>",
new MatchEvaluator(m =>
{
var value = m.Groups["value"].Value;
return
$@"<span class=""quot"">&quot;</span>{AttributeValuePostprocessing(value)}<span class=""quot"">&quot;</span>";
}));
return HtmlAttributeValueSpanRegularExpression().Replace(html, new MatchEvaluator(m =>
{
var value = m.Groups["value"].Value;
return
$@"<span class=""quot"">&quot;</span>{AttributeValuePostprocessing(value)}<span class=""quot"">&quot;</span>";
}));
}

private static string AttributeValuePostprocessing(string value)
Expand All @@ -133,18 +130,33 @@ private static string AttributeValuePostprocessing(string value)
return value;
if (value is "true" or "false")
return $"<span class=\"keyword\">{value}</span>";
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 $"<span class=\"enum\">{tokens[0]}</span><span class=\"enum-value\">.{tokens[1]}</span>";
}

if (Regex.IsMatch(value, "^@[A-Za-z0-9]+$"))
if (AlphanumericRegularExpression().IsMatch(value))
{
return $"<span class=\"sharp-variable\">{value}</span>";
}

return $"<span class=\"html-attribute-value\">{value}</span>";
}

[GeneratedRegex(@"</?DocsFrame>")]
private static partial Regex DocsFrameEndTagRegularExpression();

[GeneratedRegex("@(namespace|layout|page) .+?\n")]
private static partial Regex NamespaceLayoutOrPageRegularExpression();

[GeneratedRegex(@"<span class=""html-attribute-value"">&quot;(?'value'.*?)&quot;</span>")]
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();
}
}
Loading

0 comments on commit 8849df9

Please sign in to comment.