diff --git a/Shared/Models/GenealogicalDataCommunication.cs b/Shared/Models/GenealogicalDataCommunication.cs index 24f62af..1e39a84 100644 --- a/Shared/Models/GenealogicalDataCommunication.cs +++ b/Shared/Models/GenealogicalDataCommunication.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; namespace View_by_Distance.Shared.Models; @@ -17,8 +18,20 @@ public record GenealogicalDataCommunication(DateTime? Birth, public override string ToString() { - string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }); + string result = JsonSerializer.Serialize(this, GenealogicalDataCommunicationSourceGenerationContext.Default.GenealogicalDataCommunication); return result; } +} + +[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] +[JsonSerializable(typeof(GenealogicalDataCommunication))] +internal partial class GenealogicalDataCommunicationSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] +[JsonSerializable(typeof(GenealogicalDataCommunication[]))] +internal partial class GenealogicalDataCommunicationSourceCollectionGenerationContext : JsonSerializerContext +{ } \ No newline at end of file diff --git a/Shared/Models/Stateless/Methods/MarkDown.cs b/Shared/Models/Stateless/Methods/MarkDown.cs index 6d41678..1e2eb9b 100644 --- a/Shared/Models/Stateless/Methods/MarkDown.cs +++ b/Shared/Models/Stateless/Methods/MarkDown.cs @@ -14,6 +14,7 @@ internal abstract class MarkDown { List results = new(); string afterTrim; + string[] segments; string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine); foreach (string jsonLine in jsonLines) { diff --git a/Shared/Models/Stateless/Methods/PersonContainer.cs b/Shared/Models/Stateless/Methods/PersonContainer.cs index a0e06bd..8a2d5b5 100644 --- a/Shared/Models/Stateless/Methods/PersonContainer.cs +++ b/Shared/Models/Stateless/Methods/PersonContainer.cs @@ -1,7 +1,6 @@ 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; @@ -394,25 +393,57 @@ internal abstract class PersonContainer return new(results); } + private static void WriteAll(long ticks, string a2PeopleContentDirectory, List frontMatterLinesCollections, string weekOfYear) + { + string json; + string[] segments; + string? directory; + string frontMatterLastLine; + DateTime dateTime = new(ticks); + List allFrontMatterLines = new(); + foreach (string[] frontMatterLines in frontMatterLinesCollections) + { + 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("},"); + } + 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, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray); + if (genealogicalDataCommunications is null) + throw new NullReferenceException(nameof(genealogicalDataCommunications)); + json = JsonSerializer.Serialize(genealogicalDataCommunications, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray); + _ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true); + } + internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) { bool? male; bool? first; - string json; string fullName; string[] matches; - string[] segments; string? directory; bool isDefaultName; const int zero = 0; string personKeyFormatted; - string frontMatterLastLine; string lowerHyphenFullName; List frontMatterLines; string pattern = "[^a-z0-9-]"; DateTime dateTime = new(ticks); - List allFrontMatterLines = new(); Calendar calendar = new CultureInfo("en-US").Calendar; + List<(string? Id, string[] FrontMatterLines)> collection = new(); Models.GenealogicalDataCommunication genealogicalDataCommunication; GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines; string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); @@ -449,32 +480,13 @@ internal abstract class PersonContainer frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication); if (!frontMatterLines.Any()) continue; - 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("},"); + collection.Add((genealogicalDataCommunication.Id, frontMatterLines.ToArray())); MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines); } - if (allFrontMatterLines.Any()) + if (collection.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); + List frontMatterLinesCollections = (from l in collection orderby l.Id.Length, l.Id where l.Id is not null select l.FrontMatterLines).ToList(); + WriteAll(ticks, a2PeopleContentDirectory, frontMatterLinesCollections, weekOfYear); } }