From 77a4f86953bac0124b509eb7045a4930254ff83a Mon Sep 17 00:00:00 2001 From: Miguel Date: Sun, 16 Jun 2024 20:28:56 +0200 Subject: [PATCH] Cambiado los Botones por ToolBarTray y agregada a la solucion libObsidean como dependencia para que se compile automaticamente --- MarkdownTableParser.cs | 60 --------------------- Obsidean.cs | 118 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 62 deletions(-) delete mode 100644 MarkdownTableParser.cs diff --git a/MarkdownTableParser.cs b/MarkdownTableParser.cs deleted file mode 100644 index 8a1dbf3..0000000 --- a/MarkdownTableParser.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System.Text.RegularExpressions; -using System.IO; -using Newtonsoft.Json.Linq; - -namespace libObsidean -{ - public class MarkdownTableParser - { - public static string[,] ParseTableFromMarkdown(string filePath) - { - var lines = File.ReadAllLines(filePath); - var tableLines = ExtractTableLines(lines); - return ConvertTableLinesToArray(tableLines); - } - - private static List ExtractTableLines(string[] lines) - { - List tableLines = new List(); - bool inTable = false; - - foreach (var line in lines) - { - if (line.StartsWith("|") && line.EndsWith("|")) - { - inTable = true; - tableLines.Add(line.Trim()); - } - else if (inTable) - { - break; - } - } - - return tableLines; - } - - private static string[,] ConvertTableLinesToArray(List tableLines) - { - if (tableLines.Count == 0) - return new string[0, 0]; - - var header = tableLines[0].Split('|').Select(h => h.Trim()).Where(h => !string.IsNullOrEmpty(h)).ToArray(); - int columns = header.Length; - int rows = tableLines.Count - 1; - - string[,] tableArray = new string[rows, columns]; - - for (int i = 1; i <= rows; i++) - { - var row = tableLines[i].Split('|').Select(r => r.Trim()).Where(r => !string.IsNullOrEmpty(r)).ToArray(); - for (int j = 0; j < columns; j++) - { - tableArray[i - 1, j] = row[j]; - } - } - - return tableArray; - } - } -} diff --git a/Obsidean.cs b/Obsidean.cs index ba4fb06..2297fe2 100644 --- a/Obsidean.cs +++ b/Obsidean.cs @@ -5,7 +5,7 @@ using Newtonsoft.Json.Linq; namespace libObsidean { - internal class Obsidean + public class Obsidean { static HashSet technicalTerms; @@ -62,6 +62,31 @@ namespace libObsidean // Ahora puedes usar technicalTerms para tu lógica de corrección } + public string[,] LeerPasswords() + { + // C:\Users\migue\OneDrive\Miguel\Obsidean\Trabajo\VM\DB\Passwords + string pathToMarkdown = GetVaultPath("VM"); + if (!string.IsNullOrEmpty(pathToMarkdown)) + { + string pathToTabla = pathToMarkdown + "\\DB\\Passwords\\HMI passwords.md"; + string[,] tableArray = ParseTableFromMarkdown(pathToTabla); + return tableArray; + } + return null; + } + + public void EscribirPasswords(string[,] tabla) + { + // C:\Users\migue\OneDrive\Miguel\Obsidean\Trabajo\VM\DB\Passwords + string pathToMarkdown = GetVaultPath("VM"); + if (!string.IsNullOrEmpty(pathToMarkdown)) + { + string pathToTabla = pathToMarkdown + "\\DB\\Passwords\\HMI passwords.md"; + SaveTableToMarkdown(pathToTabla, tabla); + } + } + + public HashSet ExtractTechnicalTerms(string[] lines) { var terms = new HashSet(); @@ -134,7 +159,96 @@ namespace libObsidean } + public static string[,] ParseTableFromMarkdown(string filePath) + { + var lines = File.ReadAllLines(filePath); + var tableLines = ExtractTableLines(lines); + return ConvertTableLinesToArray(tableLines); + } + + private static List ExtractTableLines(string[] lines) + { + List tableLines = new List(); + bool inTable = false; + + foreach (var line in lines) + { + if (line.StartsWith("|") && line.EndsWith("|")) + { + inTable = true; + tableLines.Add(line.Trim()); + } + else if (inTable) + { + break; + } + } + + return tableLines; + } + + private static string[,] ConvertTableLinesToArray(List tableLines) + { + if (tableLines.Count == 0) + return new string[0, 0]; + + var header = tableLines[0].Split('|').Select(h => h.Trim()).Where(h => !string.IsNullOrEmpty(h)).ToArray(); + int columns = header.Length; + int rows = tableLines.Count - 1; + + string[,] tableArray = new string[rows, columns]; + + for (int i = 1; i < rows; i++) + { + var row = tableLines[i+1].Split('|').ToArray(); + for (int j = 0; j < columns; j++) + { + if (i >= rows || j >= columns || i<0 || j<0) + return null; + tableArray[i - 1, j] = row[j+1]; + } + } + + return tableArray; + } + + public static void SaveTableToMarkdown(string filePath, string[,] tableArray) + { + var tableLines = ConvertArrayToTableLines(tableArray); + File.WriteAllLines(filePath, tableLines); + } + + private static List ConvertArrayToTableLines(string[,] tableArray) + { + List tableLines = new List(); + + if (tableArray.GetLength(0) == 0 || tableArray.GetLength(1) == 0) + return tableLines; + + // Add header line + List headerLine = new List(); + for (int j = 0; j < tableArray.GetLength(1); j++) + { + headerLine.Add(tableArray[0, j]); + } + tableLines.Add("| " + string.Join(" | ", headerLine) + " |"); + + // Add separator line (assumes all columns have same width header) + tableLines.Add("|" + string.Join("|", headerLine.Select(h => new string('-', h.Length + 2))) + "|"); + + // Add data lines + for (int i = 1; i < tableArray.GetLength(0); i++) + { + List rowLine = new List(); + for (int j = 0; j < tableArray.GetLength(1); j++) + { + rowLine.Add(tableArray[i, j]); + } + tableLines.Add("| " + string.Join(" | ", rowLine) + " |"); + } + + return tableLines; + } } - }