diff --git a/File-Folder-Helper.csproj b/File-Folder-Helper.csproj index e14b967..bd3b9d5 100644 --- a/File-Folder-Helper.csproj +++ b/File-Folder-Helper.csproj @@ -9,7 +9,6 @@ eb9e8f58-fcb5-45bb-9d4d-54f064c485b1 - diff --git a/Helpers/HelperKanbanMetadata.cs b/Helpers/HelperKanbanMetadata.cs index 2d81cd5..c476d37 100644 --- a/Helpers/HelperKanbanMetadata.cs +++ b/Helpers/HelperKanbanMetadata.cs @@ -1,11 +1,19 @@ using File_Folder_Helper.Models; using Microsoft.Extensions.Logging; +using System.Text; +using System.Text.RegularExpressions; namespace File_Folder_Helper.Helpers; -internal static class HelperKanbanMetadata +internal static partial class HelperKanbanMetadata { + [GeneratedRegex("([A-Z]+(.))")] + private static partial Regex UpperCase(); + + [GeneratedRegex("[\\s!?.,@:;|\\\\/\"'`£$%\\^&*{}[\\]()<>~#+\\-=_¬]+")] + private static partial Regex InvalidCharacter(); + private static List<(int, int, string, FileInfo)> GetCollectionFromIndex(string sourceDirectory, string[] lines) { List<(int, int, string, FileInfo)> results = new(); @@ -38,13 +46,66 @@ internal static class HelperKanbanMetadata return results; } + private static string GetParamCase(string value) + { + string result; + StringBuilder stringBuilder = new(value); + Match[] matches = UpperCase().Matches(value).ToArray(); + for (int i = matches.Length - 1; i > -1; i--) + _ = stringBuilder.Insert(matches[i].Index, '-'); + string[] segments = InvalidCharacter().Split(stringBuilder.ToString().ToLower()); + result = string.Join('-', segments).Trim('-'); + return result; + } + + private static void TestParamCases() + { + if (GetParamCase("PascalCase") != "pascal-case") + throw new Exception("PascalCase"); + if (GetParamCase("camelCase") != "camel-case") + throw new Exception("camelCase"); + if (GetParamCase("snake_case") != "snake-case") + throw new Exception("snake_case"); + if (GetParamCase("No Case") != "no-case") + throw new Exception("No Case"); + if (GetParamCase("With 2 numbers 3") != "with-2-numbers-3") + throw new Exception("With 2 numbers 3"); + if (GetParamCase("Multiple spaces") != "multiple-spaces") + throw new Exception("Multiple spaces"); + if (GetParamCase("Tab\tCharacter") != "tab-character") + throw new Exception("Tab\tCharacter"); + if (GetParamCase("New\nLine") != "new-line") + throw new Exception("New\nLine"); + if (GetParamCase("Punctuation, Characters") != "punctuation-characters") + throw new Exception("Punctuation, Characters"); + if (GetParamCase("M!o?r.e, @p:u;n|c\\t/u\"a\'t`i£o$n% ^c&h*a{r}a[c]t(e)r ~l#i+k-e= _t¬hese") != "m-o-r-e-p-u-n-c-t-u-a-t-i-o-n-c-h-a-r-a-c-t-e-r-s-l-i-k-e-t-hese") + throw new Exception("M!o?r.e, @p:u;n|c\\t/u\"a\'t`i£o$n% ^c&h*a{r}a[c]t(e)r ~l#i+k-e= _t¬hese"); + if (GetParamCase("This string ends with punctuation!") != "this-string-ends-with-punctuation") + throw new Exception("This string ends with punctuation!"); + if (GetParamCase("?This string starts with punctuation") != "this-string-starts-with-punctuation") + throw new Exception("?This string starts with punctuation"); + if (GetParamCase("#This string has punctuation at both ends&") != "this-string-has-punctuation-at-both-ends") + throw new Exception("#This string has punctuation at both ends&"); + if (GetParamCase("軟件 測試") != "軟件-測試") + throw new Exception("軟件 測試"); + if (GetParamCase("実験 試し") != "実験-試し") + throw new Exception("実験 試し"); + if (GetParamCase("יקספּערמענאַל פּרובירן") != "יקספּערמענאַל-פּרובירן") + throw new Exception("יקספּערמענאַל פּרובירן"); + if (GetParamCase("я надеюсь, что это сработает") != "я-надеюсь-что-это-сработает") + throw new Exception("я надеюсь, что это сработает"); + } + internal static void SetMetadata(ILogger log, AppSettings appSettings, string sourceDirectory) { + bool? match; + string? paramCase; string statusLine; List lines; LineNumber lineNumber; if (log is null) throw new NullReferenceException(); + TestParamCases(); string fullPath = Path.GetFullPath(sourceDirectory); if (!Directory.Exists(fullPath)) _ = Directory.CreateDirectory(fullPath); @@ -61,14 +122,18 @@ internal static class HelperKanbanMetadata if (!lines.Any()) continue; statusLine = $"status: \"{groupCount}-{group}\""; + paramCase = lineNumber.H1 is null ? null : GetParamCase(lines[lineNumber.H1.Value]); indexFileLines[itemLineNumber] = $"{fileInfo.LastWriteTime.Ticks}~~~{indexFileLines[itemLineNumber]}"; + match = lineNumber.H1 is null || paramCase is null ? null : Path.GetFileNameWithoutExtension(fileInfo.Name) == paramCase; if (lineNumber.FrontMatterYamlEnd is null) throw new NotSupportedException($"{nameof(SetMetadata)} must be executed first!"); + if (lineNumber.H1 is not null && paramCase is not null && match is not null && !match.Value) + lines[lineNumber.H1.Value] = $"# {paramCase}"; if (lineNumber.Status is null) lines.Insert(lineNumber.FrontMatterYamlEnd.Value, statusLine); else { - if (lines[lineNumber.Status.Value] == statusLine) + if ((match is null || match.Value) && lines[lineNumber.Status.Value] == statusLine) continue; lines[lineNumber.Status.Value] = statusLine; } diff --git a/Worker.cs b/Worker.cs index b7ffa6d..bb7f6e9 100644 --- a/Worker.cs +++ b/Worker.cs @@ -78,8 +78,6 @@ public class Worker : BackgroundService _Args.RemoveAt(singleCharIndex.Value); if (!_Args.Any()) _Logger.LogInformation("Must pass a argument!"); - else if (_Args[0].EndsWith(".kanbn") && Directory.Exists(_Args[0])) - Helpers.HelperKanbanMetadata.SetMetadata(_Logger, _AppSettings, _Args[0]); else if (Directory.Exists(_Args[0]) && File.Exists(Path.Combine(_Args[0], string.Concat(Path.GetFileName(_Args[0]), ".dll")))) Helpers.HelperILMerge.ILMerge(_Args[0]); else if (Directory.Exists(_Args[0])) @@ -138,6 +136,8 @@ public class Worker : BackgroundService Helpers.HelperCreateNoteFiles.CreateNoteFiles(_Args[0]); break; case ConsoleKey.M: + if (_Args[0].EndsWith(".kanbn") && Directory.Exists(_Args[0])) + Helpers.HelperKanbanMetadata.SetMetadata(_Logger, _AppSettings, _Args[0]); Helpers.HelperMarkdown.MarkdownWikiLinkVerification(_AppSettings, _Logger, _Args); break; case ConsoleKey.R: