1 Commits

Author SHA1 Message Date
2e4aa313fa Update Markdown Helper to write json files for tables and yaml 2025-06-24 08:35:55 -07:00
2 changed files with 42 additions and 12 deletions

4
.vscode/launch.json vendored
View File

@ -13,9 +13,9 @@
"args": [ "args": [
"s", "s",
"M", "M",
"D:/5-Other-Small/Notes/Infineon", "D:/5-Other-Small/Notes/EC-Documentation",
"-d", "-d",
"D:/5-Other-Small/Notes/Infineon/.vscode/helper", "D:/5-Other-Small/Notes/EC-Documentation/.vscode/helper",
"s", "s",
"X", "X",
"D:/5-Other-Small/Proxmox/DiskInfo", "D:/5-Other-Small/Proxmox/DiskInfo",

View File

@ -1271,12 +1271,17 @@ internal static partial class HelperMarkdown
{ {
if (string.IsNullOrEmpty(input.Destination)) if (string.IsNullOrEmpty(input.Destination))
throw new NotSupportedException(); throw new NotSupportedException();
string old;
string json;
string[] lines; string[] lines;
string fileName; string fileName;
List<Block> blocks; List<Block> blocks;
string directoryName;
List<string>? jsonLines;
MarkdownFile markdownFile; MarkdownFile markdownFile;
List<string> fileNames = []; List<string> fileNames = [];
ReadOnlyCollection<Table> tables; ReadOnlyCollection<Table> tables;
Dictionary<string, List<string>> results = [];
ReadOnlyCollection<Block> javaScriptObjectNotationBlocks; ReadOnlyCollection<Block> javaScriptObjectNotationBlocks;
ReadOnlyCollection<Block> yetAnotherMarkupLanguageBlocks; ReadOnlyCollection<Block> yetAnotherMarkupLanguageBlocks;
ReadOnlyDictionary<string, MarkdownFileAndLines> relativeToCollection = GetRelativeToCollection(appSettings, input, gitOthersModifiedAndDeletedExcludingStandardFiles: null); ReadOnlyDictionary<string, MarkdownFileAndLines> relativeToCollection = GetRelativeToCollection(appSettings, input, gitOthersModifiedAndDeletedExcludingStandardFiles: null);
@ -1309,7 +1314,26 @@ internal static partial class HelperMarkdown
blocks.AddRange(GetBlocks(logger, markdownFile, yetAnotherMarkupLanguageBlocks)); blocks.AddRange(GetBlocks(logger, markdownFile, yetAnotherMarkupLanguageBlocks));
if (blocks.Count == 0) if (blocks.Count == 0)
continue; continue;
WriteBlocks(logger, input, markdownFile, fileName, blocks.AsReadOnly()); directoryName = Path.GetFileName(Path.Combine(input.Destination, markdownFile.Directory));
if (!results.TryGetValue(directoryName, out jsonLines))
{
results.Add(directoryName, []);
if (!results.TryGetValue(directoryName, out jsonLines))
throw new Exception();
}
json = GetJson(input, markdownFile, blocks.AsReadOnly());
jsonLines.Add(json);
}
foreach (KeyValuePair<string, List<string>> keyValuePair in results)
{
json = string.Concat('[', Environment.NewLine, string.Join($",{Environment.NewLine}", keyValuePair.Value), Environment.NewLine, ']');
fileName = Path.Combine(input.Destination, $"{keyValuePair.Key}.json");
old = !File.Exists(fileName) ? string.Empty : File.ReadAllText(fileName);
if (json != old)
{
File.WriteAllText(fileName, json);
logger.LogInformation("Updated json file for <{fileName}>", fileName);
}
} }
} }
@ -1558,24 +1582,30 @@ internal static partial class HelperMarkdown
return results.AsReadOnly(); return results.AsReadOnly();
} }
private static void WriteBlocks(ILogger<Worker> logger, Input input, MarkdownFile markdownFile, string fileName, ReadOnlyCollection<Block> blocks) private static string GetJson(Input input, MarkdownFile markdownFile, ReadOnlyCollection<Block> blocks)
{ {
string result;
string paramCase; string paramCase;
List<string> lines = []; List<string> lines = [];
string fileNameParamCase = GetParamCase(markdownFile.FileNameWithoutExtension);
foreach (Block block in blocks.OrderBy(l => l.Line)) foreach (Block block in blocks.OrderBy(l => l.Line))
{ {
paramCase = block.Title is null ? $"Line-{block.Line}" : GetParamCase(block.Title); paramCase = block.Title is null ? $"Line-{block.Line}" : GetParamCase(block.Title);
if (!string.IsNullOrEmpty(input.ReplaceWithTitle) && paramCase == input.ReplaceWithTitle) if (!string.IsNullOrEmpty(input.ReplaceWithTitle) && paramCase == input.ReplaceWithTitle)
paramCase = GetParamCase(markdownFile.FileNameWithoutExtension); paramCase = $"{fileNameParamCase}-{block.Line}";
lines.Add($"\"{paramCase}\": {block.Json}"); lines.Add($"\"{paramCase}\": {block.Json}");
} }
string old = !File.Exists(fileName) ? string.Empty : File.ReadAllText(fileName); result = string.Concat('{',
string json = string.Concat('{', Environment.NewLine, string.Join($",{Environment.NewLine}", lines), Environment.NewLine, '}'); Environment.NewLine,
if (json != old) $"\"{fileNameParamCase}\": ",
{ '{',
File.WriteAllText(fileName, json); Environment.NewLine,
logger.LogInformation("Updated json file for <{fileName}>", fileName); string.Join($",{Environment.NewLine}", lines),
} Environment.NewLine,
'}',
Environment.NewLine,
'}');
return result;
} }
} }