From 6c711945b159cc0c61975b3164cf998a05f1a372 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Fri, 21 Jul 2023 12:08:04 -0700 Subject: [PATCH] allFrontMatterLines --- Shared/Models/Stateless/Methods/MarkDown.cs | 10 ++-- .../Stateless/Methods/PersonContainer.cs | 46 ++++++++++++++----- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/Shared/Models/Stateless/Methods/MarkDown.cs b/Shared/Models/Stateless/Methods/MarkDown.cs index 0f6f537..e99aea3 100644 --- a/Shared/Models/Stateless/Methods/MarkDown.cs +++ b/Shared/Models/Stateless/Methods/MarkDown.cs @@ -10,7 +10,7 @@ internal abstract class MarkDown // ... - internal static List GetFrontMatter(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication) + internal static List GetFrontMatterLines(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication) { List results = new(); string afterTrim; @@ -33,14 +33,12 @@ internal abstract class MarkDown results.Insert(0, $"title: \"{fullName}\""); results.Insert(0, "type: \"person\""); results.Add("draft: false"); - results.Insert(0, "---"); results.Add($"{nameof(lowerHyphenFullName)}: \"{lowerHyphenFullName}\""); - results.Add("---"); } return results; } - internal static void WriteFile(string personKeyFormatted, long ticks, List genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, Calendar calendar, string pattern, ReadOnlyDictionary personKeyFormattedToPersonFullName, ReadOnlyDictionary> familyIndexToCollection, Models.GenealogicalDataCommunication genealogicalDataCommunication, string fullName, string lowerHyphenFullName, List frontMatter) + internal static void WriteFile(string personKeyFormatted, long ticks, List genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, Calendar calendar, string pattern, ReadOnlyDictionary personKeyFormattedToPersonFullName, ReadOnlyDictionary> familyIndexToCollection, Models.GenealogicalDataCommunication genealogicalDataCommunication, string fullName, string lowerHyphenFullName, List frontMatterLines) { string decade; string? personFullName; @@ -55,7 +53,9 @@ internal abstract class MarkDown const string person = "person"; DateTime dateTime = new(ticks); List? relations; - lines.AddRange(frontMatter); + lines.Add("---"); + lines.AddRange(frontMatterLines); + lines.Add("---"); lines.Add(string.Empty); lines.Add($"# {fullName}"); lines.Add(string.Empty); diff --git a/Shared/Models/Stateless/Methods/PersonContainer.cs b/Shared/Models/Stateless/Methods/PersonContainer.cs index f4d3749..a0e06bd 100644 --- a/Shared/Models/Stateless/Methods/PersonContainer.cs +++ b/Shared/Models/Stateless/Methods/PersonContainer.cs @@ -1,5 +1,7 @@ using System.Collections.ObjectModel; using System.Globalization; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace View_by_Distance.Shared.Models.Stateless.Methods; @@ -396,17 +398,20 @@ internal abstract class PersonContainer { bool? male; bool? first; + string json; string fullName; string[] matches; + string[] segments; string? directory; bool isDefaultName; const int zero = 0; - List frontMatter; string personKeyFormatted; + string frontMatterLastLine; string lowerHyphenFullName; + List frontMatterLines; string pattern = "[^a-z0-9-]"; DateTime dateTime = new(ticks); - List allFrontMatter = new(); + List allFrontMatterLines = new(); Calendar calendar = new CultureInfo("en-US").Calendar; Models.GenealogicalDataCommunication genealogicalDataCommunication; GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines; @@ -441,17 +446,36 @@ internal abstract class PersonContainer continue; fullName = PersonName.GetFullName(personContainer.Person.Name); lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), pattern, "-")}"; - frontMatter = MarkDown.GetFrontMatter(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication); - if (!frontMatter.Any()) + frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication); + if (!frontMatterLines.Any()) continue; - allFrontMatter.AddRange(frontMatter); - MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatter); + allFrontMatterLines.Add("{"); + frontMatterLastLine = frontMatterLines.Last(); + foreach (string frontMatterLine in frontMatterLines) + { + segments = frontMatterLine.Split(": "); + if (segments.Length != 2) + continue; + if (frontMatterLine == frontMatterLastLine) + allFrontMatterLines.Add($"\"{string.Join("\": ", segments)}"); + else + allFrontMatterLines.Add($"\"{string.Join("\": ", segments)},"); + } + allFrontMatterLines.Add("},"); + MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines); + } + if (allFrontMatterLines.Any()) + { + directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString()); + if (!Directory.Exists(directory)) + _ = Directory.CreateDirectory(directory); + json = $"[{string.Join(Environment.NewLine, allFrontMatterLines.ToArray(), 0, allFrontMatterLines.Count - 1)}{allFrontMatterLines.Last()[..^1]}]"; + Models.GenealogicalDataCommunication[]? genealogicalDataCommunications = JsonSerializer.Deserialize(json); + if (genealogicalDataCommunications is null) + throw new NullReferenceException(nameof(genealogicalDataCommunications)); + json = JsonSerializer.Serialize(genealogicalDataCommunications, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); + _ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true); } - directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString()); - if (!Directory.Exists(directory)) - _ = Directory.CreateDirectory(directory); - string text = string.Join(Environment.NewLine, allFrontMatter); - _ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true); } } \ No newline at end of file