Cambiado los Botones por ToolBarTray y agregada a la solucion libObsidean como dependencia para que se compile automaticamente

This commit is contained in:
Miguel 2024-06-16 20:28:56 +02:00
parent 6a4427f69a
commit 77a4f86953
2 changed files with 116 additions and 62 deletions

View File

@ -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<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].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;
}
}
}

View File

@ -5,7 +5,7 @@ using Newtonsoft.Json.Linq;
namespace libObsidean
{
internal class Obsidean
public class Obsidean
{
static HashSet<string> 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<string> ExtractTechnicalTerms(string[] lines)
{
var terms = new HashSet<string>();
@ -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<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++)
{
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<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;
}
}
}