From 6d2d75a833c9efe3713b925e70a89c339cbf18f9 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 14 Jun 2025 17:47:22 +0200 Subject: [PATCH] Improve Obsidean.cs with new methods and comments - Updated comments for clarity and corrected encoding issues. - Added `LeerShortcuts` to read shortcuts from markdown files. - Introduced `LeerShortcutsPorAplicacion` to filter shortcuts by application name. - Implemented `AgregarOSobrescribirShortcut` for adding or overwriting shortcuts. - Added `RemoveDoubleBrackets` utility method to clean up text. - Enhanced readability of existing methods' comments. --- Obsidean.cs | 142 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 121 insertions(+), 21 deletions(-) diff --git a/Obsidean.cs b/Obsidean.cs index 2297fe2..9bb5e7d 100644 --- a/Obsidean.cs +++ b/Obsidean.cs @@ -1,4 +1,3 @@ - using System.Text.RegularExpressions; using System.IO; using Newtonsoft.Json.Linq; @@ -31,7 +30,7 @@ namespace libObsidean string path = (string)vault.Value["path"]; if (!string.IsNullOrEmpty(path)) { - // Añadir una barra al final asegura que Path.GetDirectoryName funcione correctamente + // Añadir una barra al final asegura que Path.GetDirectoryName funcione correctamente string lastDirectoryName = Path.GetFileName(Path.GetDirectoryName(path.TrimEnd('\\') + "\\")); if (lastDirectoryName.Equals(vaultName, StringComparison.OrdinalIgnoreCase)) @@ -59,7 +58,7 @@ namespace libObsidean technicalTerms = ExtractTechnicalTerms(lines); } - // Ahora puedes usar technicalTerms para tu lógica de corrección + // Ahora puedes usar technicalTerms para tu lógica de corrección } public string[,] LeerPasswords() @@ -86,13 +85,111 @@ namespace libObsidean } } + public string[,] LeerShortcuts() + { + string pathToMarkdown = GetVaultPath("VM"); + if (!string.IsNullOrEmpty(pathToMarkdown)) + { + string pathToTabla = Path.Combine(pathToMarkdown, "DB", "Shortcuts", "Shortcuts.md"); + // Si el archivo no existe, devolver cabecera vacía + if (!File.Exists(pathToTabla)) + { + string[,] vacio = new string[2, 3] { { "Application", "Shortcut", "Description" }, { "", "", "" } }; + SaveTableToMarkdown(pathToTabla, vacio); + return vacio; + } + return ParseTableFromMarkdown(pathToTabla); + } + return null; + } + + public string[,] LeerShortcutsPorAplicacion(string applicationName) + { + var tabla = LeerShortcuts(); + if (tabla == null) + return null; + + // Filtrar aplicando la primera columna (Application) + List filas = new List(); + for (int i = 1; i < tabla.GetLength(0); i++) + { + if (string.Equals(tabla[i, 0], applicationName, StringComparison.OrdinalIgnoreCase)) + { + filas.Add(new[] { tabla[i, 0], tabla[i, 1], tabla[i, 2] }); + } + } + + if (filas.Count == 0) + return new string[0, 0]; + + string[,] resultado = new string[filas.Count + 1, 3]; + // Copiar cabeceras + resultado[0, 0] = "Application"; + resultado[0, 1] = "Shortcut"; + resultado[0, 2] = "Description"; + for (int i = 0; i < filas.Count; i++) + { + resultado[i + 1, 0] = filas[i][0]; + resultado[i + 1, 1] = filas[i][1]; + resultado[i + 1, 2] = filas[i][2]; + } + return resultado; + } + + public void AgregarOSobrescribirShortcut(string applicationName, string shortcut, string description) + { + if (string.IsNullOrWhiteSpace(applicationName) || string.IsNullOrWhiteSpace(shortcut)) + return; + + var tabla = LeerShortcuts(); + if (tabla == null) + return; + + bool encontrado = false; + for (int i = 1; i < tabla.GetLength(0); i++) + { + if (string.Equals(tabla[i, 0], applicationName, StringComparison.OrdinalIgnoreCase) && + string.Equals(tabla[i, 1], shortcut, StringComparison.OrdinalIgnoreCase)) + { + // Sobrescribir descripción + tabla[i, 2] = description; + encontrado = true; + break; + } + } + + if (!encontrado) + { + // Añadir nueva fila + int filas = tabla.GetLength(0); + string[,] nuevaTabla = new string[filas + 1, 3]; + for (int i = 0; i < filas; i++) + { + for (int j = 0; j < 3; j++) + { + nuevaTabla[i, j] = tabla[i, j]; + } + } + nuevaTabla[filas, 0] = applicationName; + nuevaTabla[filas, 1] = shortcut; + nuevaTabla[filas, 2] = description; + tabla = nuevaTabla; + } + + string pathToMarkdown = GetVaultPath("VM"); + if (!string.IsNullOrEmpty(pathToMarkdown)) + { + string pathToTabla = Path.Combine(pathToMarkdown, "DB", "Shortcuts", "Shortcuts.md"); + SaveTableToMarkdown(pathToTabla, tabla); + } + } public HashSet ExtractTechnicalTerms(string[] lines) { var terms = new HashSet(); foreach (var line in lines) { - // Suponiendo que cada línea contiene un término técnico + // Suponiendo que cada línea contiene un término técnico terms.Add(line.Trim()); } return terms; @@ -104,27 +201,27 @@ namespace libObsidean return Regex.Replace(text, @"\b(\w+)\b", match => { string word = match.Groups[1].Value; - // Verificar si la palabra está en el conjunto de términos técnicos + // Verificar si la palabra está en el conjunto de términos técnicos if (technicalTerms.Contains(word)) { - return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica + return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica } - return word; // Devolver la palabra sin modificar si no es técnica + return word; // Devolver la palabra sin modificar si no es técnica }); } public string MarkTechnicalTerms_IgnoreCase(string text) { - // Utilizar Regex para identificar palabras individualmente, ignorando mayúsculas y minúsculas + // Utilizar Regex para identificar palabras individualmente, ignorando mayúsculas y minúsculas return Regex.Replace(text, @"\b(\w+)\b", match => { string word = match.Groups[1].Value; - // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas + // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas if (technicalTerms.Contains(word, StringComparer.OrdinalIgnoreCase)) { - return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica + return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica } - return word; // Devolver la palabra sin modificar si no es técnica + return word; // Devolver la palabra sin modificar si no es técnica }, RegexOptions.IgnoreCase); } @@ -134,12 +231,12 @@ namespace libObsidean return Regex.Replace(text, @"\[\[(.*?)\]\]", match => { string word = match.Groups[1].Value; - // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas + // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas if (technicalTerms.Contains(word)) { - return word; // Devolver la palabra sin corchetes si es técnica + return word; // Devolver la palabra sin corchetes si es técnica } - return match.Value; // Devolver la palabra con corchetes si no es técnica + return match.Value; // Devolver la palabra con corchetes si no es técnica }); } @@ -149,15 +246,21 @@ namespace libObsidean return Regex.Replace(text, @"\[\[(.*?)\]\]", match => { string word = match.Groups[1].Value; - // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas + // Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas if (technicalTerms.Contains(word.ToLowerInvariant(), StringComparer.OrdinalIgnoreCase)) { - return word; // Devolver la palabra sin corchetes si es técnica + return word; // Devolver la palabra sin corchetes si es técnica } - return match.Value; // Devolver la palabra con corchetes si no es técnica + return match.Value; // Devolver la palabra con corchetes si no es técnica }); } + public string RemoveDoubleBrackets(string text) + { + // Utilizar Regex para encontrar y remover los dobles corchetes + // manteniendo solo el contenido dentro de ellos + return Regex.Replace(text, @"\[\[(.*?)\]\]", "$1"); + } public static string[,] ParseTableFromMarkdown(string filePath) { @@ -203,9 +306,7 @@ namespace libObsidean 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]; + tableArray[i - 1, j] = row[j+1].Trim(); } } @@ -250,5 +351,4 @@ namespace libObsidean return tableLines; } } - }