355 lines
13 KiB
C#
355 lines
13 KiB
C#
using System.Text.RegularExpressions;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace libObsidean
|
|
{
|
|
public class Obsidean
|
|
{
|
|
static HashSet<string> technicalTerms;
|
|
|
|
static string GetObsidianConfigPath()
|
|
{
|
|
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
return Path.Combine(appDataPath, "obsidian", "obsidian.json");
|
|
}
|
|
|
|
static string GetVaultPath(string vaultName)
|
|
{
|
|
try
|
|
{
|
|
string pathToJsonFile = GetObsidianConfigPath();
|
|
string jsonContent = File.ReadAllText(pathToJsonFile);
|
|
JObject jsonObject = JObject.Parse(jsonContent);
|
|
JObject vaults = (JObject)jsonObject["vaults"];
|
|
|
|
foreach (var vault in vaults)
|
|
{
|
|
// 'vault' es un KeyValuePair<string, JToken>
|
|
// 'vault.Key' es la clave (ID del vault), 'vault.Value' es el valor (detalles del vault como JToken)
|
|
string path = (string)vault.Value["path"];
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
// 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))
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error al leer o parsear el archivo JSON: " + ex.Message);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void LeerPalabrasTecnicas()
|
|
{
|
|
// "C:\Users\migue\OneDrive\Miguel\Obsidean\Trabajo\VM\Palabras Tecnicas\Lista.md"
|
|
string pathToMarkdown = GetVaultPath("VM");
|
|
if (!string.IsNullOrEmpty(pathToMarkdown))
|
|
{
|
|
string pathToLista = pathToMarkdown + "\\Palabras Tecnicas\\Lista.md";
|
|
string[] lines = File.ReadAllLines(pathToLista);
|
|
technicalTerms = ExtractTechnicalTerms(lines);
|
|
}
|
|
|
|
// 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 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<string[]> filas = new List<string[]>();
|
|
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<string> ExtractTechnicalTerms(string[] lines)
|
|
{
|
|
var terms = new HashSet<string>();
|
|
foreach (var line in lines)
|
|
{
|
|
// Suponiendo que cada línea contiene un término técnico
|
|
terms.Add(line.Trim());
|
|
}
|
|
return terms;
|
|
}
|
|
|
|
public string MarkTechnicalTerms(string text)
|
|
{
|
|
// Utilizar Regex para identificar palabras individualmente
|
|
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
|
|
if (technicalTerms.Contains(word))
|
|
{
|
|
return $"[[{word}]]"; // Encerrar la palabra en corchetes si 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
|
|
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
|
|
if (technicalTerms.Contains(word, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica
|
|
}
|
|
return word; // Devolver la palabra sin modificar si no es técnica
|
|
}, RegexOptions.IgnoreCase);
|
|
}
|
|
|
|
public string RemoveTechnicalTermMarkers(string text)
|
|
{
|
|
// Utilizar Regex para encontrar y remover los dobles corchetes
|
|
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
|
|
if (technicalTerms.Contains(word))
|
|
{
|
|
return word; // Devolver la palabra sin corchetes si es técnica
|
|
}
|
|
return match.Value; // Devolver la palabra con corchetes si no es técnica
|
|
});
|
|
}
|
|
|
|
public string RemoveTechnicalTermMarkers_IgnoreCase(string text)
|
|
{
|
|
// Utilizar Regex para encontrar y remover los dobles corchetes
|
|
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
|
|
if (technicalTerms.Contains(word.ToLowerInvariant(), StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
return word; // Devolver la palabra sin corchetes si 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)
|
|
{
|
|
var lines = File.ReadAllLines(filePath);
|
|
var tableLines = ExtractTableLines(lines);
|
|
return ConvertTableLinesToArray(tableLines);
|
|
}
|
|
|
|
private static List<string> ExtractTableLines(string[] lines)
|
|
{
|
|
List<string> tableLines = new List<string>();
|
|
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<string> 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++)
|
|
{
|
|
tableArray[i - 1, j] = row[j+1].Trim();
|
|
}
|
|
}
|
|
|
|
return tableArray;
|
|
}
|
|
|
|
public static void SaveTableToMarkdown(string filePath, string[,] tableArray)
|
|
{
|
|
var tableLines = ConvertArrayToTableLines(tableArray);
|
|
File.WriteAllLines(filePath, tableLines);
|
|
}
|
|
|
|
private static List<string> ConvertArrayToTableLines(string[,] tableArray)
|
|
{
|
|
List<string> tableLines = new List<string>();
|
|
|
|
if (tableArray.GetLength(0) == 0 || tableArray.GetLength(1) == 0)
|
|
return tableLines;
|
|
|
|
// Add header line
|
|
List<string> headerLine = new List<string>();
|
|
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<string> rowLine = new List<string>();
|
|
for (int j = 0; j < tableArray.GetLength(1); j++)
|
|
{
|
|
rowLine.Add(tableArray[i, j]);
|
|
}
|
|
tableLines.Add("| " + string.Join(" | ", rowLine) + " |");
|
|
}
|
|
|
|
return tableLines;
|
|
}
|
|
}
|
|
}
|