From 3888ae1083c57994d2ae2967294374ab79f005d2 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Mon, 10 Jul 2023 10:28:23 -0700 Subject: [PATCH] CreateImportFiles --- .vscode/launch.json | 5 +- Helpers/HelperCreateNoteFiles.cs | 113 ++++++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f2c8d74..2926d90 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -44,6 +44,7 @@ // dotnet run "s" "D:/Documents/Obsidian/Infineon/.kanbn" // dotnet run "s" "L:/Git/View-by-Distance-MKLink-Console/.kanbn" // dotnet run "s" "T:/MESAFIBACKLOG/06_SourceCode/MESAFIBACKLOG/Adaptation/.kanbn" -// dotnet run "s" "D:/Documents/Projects/EC Documentation - Foam/.kanbn" +// dotnet run "s" "D:/Documents/Projects/EC-Documentation - Foam/.kanbn" // dotnet run "s" "D:/Documents/Notes/Infineon" -// dotnet run "s" "D:/5-Other-Small/Notes/Infineon/.kanbn" \ No newline at end of file +// dotnet run "s" "D:/5-Other-Small/Notes/Infineon/.kanbn" +// dotnet run "s" "D:/Documents/Projects/EC-Documentation" \ No newline at end of file diff --git a/Helpers/HelperCreateNoteFiles.cs b/Helpers/HelperCreateNoteFiles.cs index 0aa822a..645a83f 100644 --- a/Helpers/HelperCreateNoteFiles.cs +++ b/Helpers/HelperCreateNoteFiles.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace File_Folder_Helper.Helpers; @@ -52,14 +53,12 @@ internal static class HelperCreateNoteFiles _ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(directory); } - internal static void CreateNoteFiles(string argsZero) + private static void CreateDailyNotes(string argsZero, long ticks) { string file; string directory; string weekOfYear; DateTime dateTime; - long ticks = DateTime.Now.Ticks; - CleanExistingFiles(argsZero, ticks); string lastDirectory = string.Empty; DateTime startDateTime = DateTime.Now.AddDays(1); Calendar calendar = new CultureInfo("en-US").Calendar; @@ -97,4 +96,112 @@ internal static class HelperCreateNoteFiles } } + private static void CreateImportFiles(long ticks, List importFiles) + { + bool csv; + bool tsv; + string file; + string type; + string title; + string[] lines; + string[] links; + int bodyKey = 4; + int typeKey = 0; + string linkText; + int linksKey = 3; + int titleKey = 1; + string[] columns; + string? directory; + string[] segments; + int descriptionKey = 2; + string destinationDirectory; + DateTime dateTime = new(ticks); + StringBuilder stringBuilder = new(); + string csvHeader = "type,title,description,links,body"; + string tsvHeader = "type\ttitle\tdescription\tlinks\tbody"; + int expectedCount = csvHeader.Length - csvHeader.Replace(",", string.Empty).Length + 1; + foreach (string importFile in importFiles) + { + csv = false; + tsv = false; + directory = Path.GetDirectoryName(importFile); + if (directory is null) + continue; + lines = File.ReadAllLines(importFile); + for (int i = 0; i < lines.Length; i++) + { + if (i == 0) + { + if (lines[i] == csvHeader) + (csv, tsv) = (true, false); + else if (lines[i] == tsvHeader) + (csv, tsv) = (false, true); + else + break; + continue; + } + if (csv) + columns = lines[i].Split(','); + else if (tsv) + columns = lines[i].Split('\t'); + else + continue; + if (columns.Length != expectedCount) + continue; + _ = stringBuilder.Clear(); + title = columns[titleKey].Trim(); + linkText = columns[linksKey].Trim(); + type = columns[typeKey].Trim().ToLower().Replace(' ', '-'); + if (string.IsNullOrEmpty(linkText)) + links = Array.Empty(); + else + links = linkText.Split(';', StringSplitOptions.RemoveEmptyEntries); + foreach (string link in links) + { + segments = link.Split(':'); + if (segments.Length == 1) + _ = stringBuilder.AppendLine($"- [[{segments.First()}]]"); + else if (segments.Length == 2) + _ = stringBuilder.AppendLine($"- [{segments.First()}]({segments.Last()})"); + else + continue; + } + destinationDirectory = Path.Combine(directory, type); + if (!Directory.Exists(destinationDirectory)) + _ = Directory.CreateDirectory(destinationDirectory); + file = Path.Combine(destinationDirectory, $"{title.ToLower().Replace(' ', '-')}.md"); + File.WriteAllLines(file, new string[] + { + "---", + $"type: '{type}'", + $"title: '{title}'", + $"description: '{columns[descriptionKey].Trim()}'", + $"created: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}", + $"updated: {dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}", + "---", + string.Empty, + $"# {title}", + string.Empty, + stringBuilder.ToString(), + string.Empty, + columns[bodyKey].Trim(), + string.Empty, + }); + } + } + } + + internal static void CreateNoteFiles(string argsZero) + { + long ticks = DateTime.Now.Ticks; + CleanExistingFiles(argsZero, ticks); + List importFiles = new(); + importFiles.AddRange(Directory.GetFiles(argsZero, "*.csv", SearchOption.TopDirectoryOnly)); + importFiles.AddRange(Directory.GetFiles(argsZero, "*.tsv", SearchOption.TopDirectoryOnly)); + if (!importFiles.Any()) + CreateDailyNotes(argsZero, ticks); + else + CreateImportFiles(ticks, importFiles); + } + } \ No newline at end of file