diff --git a/Instance/DlibDotNet.cs b/Instance/DlibDotNet.cs index 4f5b6e0..784e8bc 100644 --- a/Instance/DlibDotNet.cs +++ b/Instance/DlibDotNet.cs @@ -131,7 +131,7 @@ public partial class DlibDotNet TimeSpan a2LastWriteTimeTimeSpan = new(ticks - new DirectoryInfo(a2PeopleContentDirectory).LastWriteTime.Ticks); if (a2LastWriteTimeTimeSpan.TotalDays > 1) { - IPersonContainer.MaybeWriteFiles(configuration.MappingDefaultName, configuration.PersonBirthdayFormat, ticks, genealogicalDataCommunicationRelations, _PersonContainers, a2PeopleContentDirectory); + IPersonContainer.MaybeWriteMarkDownFiles(configuration.MappingDefaultName, configuration.PersonBirthdayFormat, ticks, genealogicalDataCommunicationRelations, _PersonContainers, a2PeopleContentDirectory); if (IGenealogicalDataCommunication.CleanDisplayDirectoryAllFilesAndWriteTicksGed(_Configuration.MappingDefaultName, _Configuration.PersonBirthdayFormat, _PersonContainers, headerLines, familyGroupLines, footerLines, ticks, a2PeopleContentDirectory)) _PersonContainers = IPersonContainer.GetPersonContainers(storage, configuration.MappingDefaultName, configuration.PersonBirthdayFormat, configuration.PersonCharacters.ToArray(), _Faces.FileNameExtension, individuals); } diff --git a/Map/Models/Stateless/MapLogic.cs b/Map/Models/Stateless/MapLogic.cs index 21f8768..3c974b7 100644 --- a/Map/Models/Stateless/MapLogic.cs +++ b/Map/Models/Stateless/MapLogic.cs @@ -251,11 +251,6 @@ internal abstract class MapLogic personDisplayDirectoryNames = IPath.GetDirectoryNames(personNameDirectory); if (!personDisplayDirectoryNames.Any()) continue; - if (newestPersonKeyFormatted is null) - { - if (!IPerson.IsDefaultName(configuration.MappingDefaultName, personDisplayDirectoryNames[^1])) - throw new NotImplementedException("Should this happen?"); - } files = Directory.GetFiles(personNameDirectory, "*", SearchOption.TopDirectoryOnly); if (isReservedDirectoryName && files.Any()) throw new Exception($"Move personKey directories up one from {nameof(Shared.Models.Stateless.IMapLogic.Sorting)} and delete {nameof(Shared.Models.Stateless.IMapLogic.Sorting)} directory!"); @@ -263,6 +258,11 @@ internal abstract class MapLogic throw new Exception($"Move personKey directories up one from {manualCopyHumanized} and delete {manualCopyHumanized} directory!"); if (personKeyFormatted == forceSingleImageHumanized && files.Any()) throw new Exception($"Move personKey directories up one from {forceSingleImageHumanized} and delete {forceSingleImageHumanized} directory!"); + if (newestPersonKeyFormatted is null) + { + if (!IPerson.IsDefaultName(configuration.MappingDefaultName, personDisplayDirectoryNames[^1])) + throw new NotImplementedException("Should this happen?"); + } if (personKeyFormatted.Length != configuration.PersonBirthdayFormat.Length) continue; if (personDisplayDirectoryNames[^1].Length == 1 || IPerson.IsDefaultName(configuration.MappingDefaultName, personDisplayDirectoryNames[^1]) || !personKeyFormattedCollection.Contains(personKeyFormatted)) diff --git a/Shared/Models/GenealogicalDataCommunication.cs b/Shared/Models/GenealogicalDataCommunication.cs index e4190b9..24f62af 100644 --- a/Shared/Models/GenealogicalDataCommunication.cs +++ b/Shared/Models/GenealogicalDataCommunication.cs @@ -2,22 +2,22 @@ using System.Text.Json; namespace View_by_Distance.Shared.Models; -public record GenealogicalDataCommunication(string? Id, - string? UId, - string? Name, - string? GivenName, - string? SurName, - string? Suffix, - string? NickName, - char Sex, - DateTime? Birth, +public record GenealogicalDataCommunication(DateTime? Birth, + DateTime? Changed, DateTime? Death, - DateTime? Changed) + string? GivenName, + string? Id, + string? Name, + string NickName, + char Sex, + string? Suffix, + string? SurName, + string? UId) { public override string ToString() { - string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }); return result; } diff --git a/Shared/Models/Stateless/Methods/GenealogicalDataCommunication.cs b/Shared/Models/Stateless/Methods/GenealogicalDataCommunication.cs index e9fd2e0..722e675 100644 --- a/Shared/Models/Stateless/Methods/GenealogicalDataCommunication.cs +++ b/Shared/Models/Stateless/Methods/GenealogicalDataCommunication.cs @@ -276,18 +276,18 @@ internal abstract class GenealogicalDataCommunication return result; } - internal static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) + internal static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(bool first, GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) { Models.GenealogicalDataCommunication result; DateTime? birth; DateTime? death; DateTime? changed; - char sex = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Sex) || !genealogicalDataCommunicationLines.Sex.Contains("1 SEX ") ? 'U' : genealogicalDataCommunicationLines.Sex.Split("1 SEX ")[1][0]; string? uId = string.IsNullOrEmpty(genealogicalDataCommunicationLines.UId) || !genealogicalDataCommunicationLines.UId.Contains("1 _UID ") ? null : genealogicalDataCommunicationLines.UId.Split("1 _UID ")[1]; + char sex = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Sex) || !genealogicalDataCommunicationLines.Sex.Contains("1 SEX ") ? 'U' : genealogicalDataCommunicationLines.Sex.Split("1 SEX ")[1].First(); string? name = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Name) || !genealogicalDataCommunicationLines.Name.Contains("1 NAME ") ? null : genealogicalDataCommunicationLines.Name.Split("1 NAME ")[1]; string? suffix = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Suffix) || !genealogicalDataCommunicationLines.Suffix.Contains("2 NSFX ") ? null : genealogicalDataCommunicationLines.Suffix.Split("2 NSFX ")[1]; string? surName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.SurName) || !genealogicalDataCommunicationLines.SurName.Contains("2 SURN ") ? null : genealogicalDataCommunicationLines.SurName.Split("2 SURN ")[1]; - string? nickName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.NickName) || !genealogicalDataCommunicationLines.NickName.Contains("2 NICK ") ? null : genealogicalDataCommunicationLines.NickName.Split("2 NICK ")[1]; + string nickName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.NickName) || !genealogicalDataCommunicationLines.NickName.Contains("2 NICK ") ? throw new NotSupportedException() : genealogicalDataCommunicationLines.NickName.Split("2 NICK ")[1]; string? givenName = string.IsNullOrEmpty(genealogicalDataCommunicationLines.GivenName) || !genealogicalDataCommunicationLines.GivenName.Contains("2 GIVN ") ? null : genealogicalDataCommunicationLines.GivenName.Split("2 GIVN ")[1]; string? id = string.IsNullOrEmpty(genealogicalDataCommunicationLines.Id) || !genealogicalDataCommunicationLines.Id.Contains("0 @I") || !genealogicalDataCommunicationLines.Id.Contains("@ INDI") ? null : genealogicalDataCommunicationLines.Id.Split('@')[1][1..]; string[] birthLines = (from l in genealogicalDataCommunicationLines.Birth where l.Contains("2 DATE ") select l.Split("2 DATE ")[1]).ToArray(); @@ -310,6 +310,8 @@ internal abstract class GenealogicalDataCommunication bool alive = death is null && !genealogicalDataCommunicationLines.Death.Any(l => l == "1 DEAT Y"); (int age, _) = IAge.GetAge(DateTime.Now.Ticks, birth.Value.Ticks); int hours = IPersonBirthday.GetHour(alive, sex); + if (!first) + hours += 2; if (death == birth) death = death.Value.AddHours(hours); birth = birth.Value.AddHours(hours); @@ -319,83 +321,20 @@ internal abstract class GenealogicalDataCommunication death = birth; } death ??= !genealogicalDataCommunicationLines.Death.Any(l => l == "1 DEAT Y") ? null : birth; - result = new(id, uId, name, givenName, surName, suffix, nickName, sex, birth, death, changed); + result = new(birth, + changed, + death, + givenName, + id, + name, + nickName, + sex, + suffix, + surName, + uId); return result; } - internal static void WriteFile(string personKeyFormatted, Models.PersonName personName, string[]? individualsLines, bool isDefaultName, string directory, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool verify, bool first) - { - if (verify) - { - if (genealogicalDataCommunication.SurName != personName.Last.Value) - throw new Exception($"{genealogicalDataCommunication.Name} - {personKeyFormatted}"); - } - string jrOrSr; - if (string.IsNullOrEmpty(personName.Alias.Value)) - jrOrSr = string.Empty; - else - { - if (personName.Alias.Value.Contains(" Jr")) - jrOrSr = " Jr"; - else if (personName.Alias.Value.Contains(" Sr")) - jrOrSr = " Sr"; - else - jrOrSr = string.Empty; - } - string code = IPersonBirthday.GetHour(genealogicalDataCommunication.Death is null, genealogicalDataCommunication.Sex).ToString("00"); - if (directory.EndsWith("00")) - directory = string.Concat(directory[..^2], code); - else if (directory.EndsWith("01")) - directory = string.Concat(directory[..^2], code); - if (!Directory.Exists(directory)) - _ = Directory.CreateDirectory(directory); - if (first && !personKeyFormatted.EndsWith(code)) - personKeyFormatted = $"{personKeyFormatted[..^2]}{code}"; - List lines = new(); - if (individualsLines is null || !individualsLines.Any()) - lines.Add($"0 @I{personKeyFormatted}@ INDI"); - else - { - if (!individualsLines[0].StartsWith("0 @I")) - throw new NotSupportedException(); - lines.Add(individualsLines[0]); - } - lines.Add($"1 NAME {personName.First.Value} /{personName.Last.Value}/{jrOrSr}"); - if (!string.IsNullOrEmpty(personName.First.Value)) - lines.Add($"2 GIVN {personName.First.Value}"); - if (!string.IsNullOrEmpty(personName.Last.Value)) - lines.Add($"2 SURN {personName.Last.Value}"); - if (!string.IsNullOrEmpty(jrOrSr)) - lines.Add($"2 NSFX {jrOrSr.Trim()}"); - lines.Add($"2 NICK {personKeyFormatted}"); - lines.Add($"1 SEX {genealogicalDataCommunication.Sex}"); - if (genealogicalDataCommunication.Birth is not null) - { - Models.PersonBirthday personBirthday = new(genealogicalDataCommunication.Birth.Value); - if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday)) - { - lines.Add("1 BIRT"); - lines.Add($"2 DATE {genealogicalDataCommunication.Birth.Value:dd MMM yyyy}"); - } - } - if (genealogicalDataCommunication.Death is not null) - { - if (genealogicalDataCommunication?.Death is null || genealogicalDataCommunication.Death == genealogicalDataCommunication.Birth || IPersonBirthday.IsCounterPersonBirthday(new(genealogicalDataCommunication.Death.Value))) - lines.Add("1 DEAT Y"); - else - { - lines.Add("1 DEAT"); - lines.Add($"2 DATE {genealogicalDataCommunication.Death.Value:dd MMM yyyy}"); - } - } - if (isDefaultName) - lines.Add("9 NOTE"); - if (individualsLines is not null) - lines.AddRange(GetFilteredOutMapped(individualsLines)); - string text = string.Join(Environment.NewLine, lines); - _ = IPath.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), text, updateDateWhenMatches: false, compareBeforeWrite: true); - } - internal static bool CleanDisplayDirectoryAllFilesAndWriteTicksGed(string mappingDefaultName, string personBirthdayFormat, List personContainers, string[] headerLines, List familyGroupLines, string[] footerLines, long ticks, string a2PeopleContentDirectory) { bool result = false; diff --git a/Shared/Models/Stateless/Methods/IGenealogicalDataCommunication.cs b/Shared/Models/Stateless/Methods/IGenealogicalDataCommunication.cs index 2dd7396..fb87c2f 100644 --- a/Shared/Models/Stateless/Methods/IGenealogicalDataCommunication.cs +++ b/Shared/Models/Stateless/Methods/IGenealogicalDataCommunication.cs @@ -7,11 +7,6 @@ public interface IGenealogicalDataCommunication // ... - void TestStatic_WriteFile(string personKeyFormatted, Models.PersonName personName, string[]? individualsLines, bool isDefaultName, string directory, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool verify, bool first) => - WriteFile(personKeyFormatted, personName, individualsLines, isDefaultName, directory, genealogicalDataCommunication, verify, first); - static void WriteFile(string personKeyFormatted, Models.PersonName personName, string[]? individualsLines, bool isDefaultName, string directory, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool verify, bool first) => - GenealogicalDataCommunication.WriteFile(personKeyFormatted, personName, individualsLines, isDefaultName, directory, genealogicalDataCommunication, verify, first); - List TestStatic_GetMappedLines(string genealogicalDataCommunicationFile, bool requireNickName) => GetMappedLines(genealogicalDataCommunicationFile, requireNickName); static List GetMappedLines(string genealogicalDataCommunicationFile, bool requireNickName) => @@ -22,10 +17,10 @@ public interface IGenealogicalDataCommunication static GenealogicalDataCommunicationLines GetGenealogicalDataCommunicationLines(string[] individualsLines) => GenealogicalDataCommunication.GetGenealogicalDataCommunicationLines(new(DateTime.Now), individualsLines); - Models.GenealogicalDataCommunication TestStatic_GetGenealogicalDataCommunication(GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) => - GetGenealogicalDataCommunication(genealogicalDataCommunicationLines); - static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) => - GenealogicalDataCommunication.GetGenealogicalDataCommunication(genealogicalDataCommunicationLines); + Models.GenealogicalDataCommunication TestStatic_GetGenealogicalDataCommunication(bool first, GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) => + GetGenealogicalDataCommunication(first, genealogicalDataCommunicationLines); + static Models.GenealogicalDataCommunication GetGenealogicalDataCommunication(bool first, GenealogicalDataCommunicationLines genealogicalDataCommunicationLines) => + GenealogicalDataCommunication.GetGenealogicalDataCommunication(first, genealogicalDataCommunicationLines); (string[], ReadOnlyDictionary, List, string[], List genealogicalDataCommunicationRelations) TestStatic_GetIndividuals(string genealogicalDataCommunicationFile, bool requireNickName) => GetIndividuals(genealogicalDataCommunicationFile, requireNickName); diff --git a/Shared/Models/Stateless/Methods/IPersonContainer.cs b/Shared/Models/Stateless/Methods/IPersonContainer.cs index fa28e8a..66970d2 100644 --- a/Shared/Models/Stateless/Methods/IPersonContainer.cs +++ b/Shared/Models/Stateless/Methods/IPersonContainer.cs @@ -22,10 +22,10 @@ public interface IPersonContainer static List GetPersonContainers(Properties.IStorage storage, string mappingDefaultName, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, ReadOnlyDictionary individuals) => PersonContainer.GetPersonContainers(storage, mappingDefaultName, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals); - void TestStatic_MaybeWriteFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) => - MaybeWriteFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory); - static void MaybeWriteFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) => - PersonContainer.MaybeWriteFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory); + void TestStatic_MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) => + MaybeWriteMarkDownFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory); + static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) => + PersonContainer.MaybeWriteMarkDownFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory); List<(long?, string)> TestStatic_GetDisplay(string personBirthdayFormat, Models.PersonContainer personContainer) => GetDisplay(personBirthdayFormat, personContainer); diff --git a/Shared/Models/Stateless/Methods/MarkDown.cs b/Shared/Models/Stateless/Methods/MarkDown.cs index 873e27e..0f6f537 100644 --- a/Shared/Models/Stateless/Methods/MarkDown.cs +++ b/Shared/Models/Stateless/Methods/MarkDown.cs @@ -10,74 +10,52 @@ internal abstract class MarkDown // ... - internal static void WriteFile(string personKeyFormatted, long ticks, Models.PersonName personName, List genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, Calendar calendar, ReadOnlyDictionary personKeyFormattedToPersonFullName, ReadOnlyDictionary> familyIndexToCollection, bool isDefaultName, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool first) + internal static List GetFrontMatter(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication) + { + List results = new(); + string afterTrim; + string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine); + foreach (string jsonLine in jsonLines) + { + afterTrim = jsonLine.Trim(); + if (afterTrim.Length < 3) + continue; + if (afterTrim[^1] != ',') + results.Add(afterTrim[1..].Replace("\": \"", ": \"")); + else + results.Add(afterTrim[1..^1].Replace("\": \"", ": \"")); + } + if (results.Any()) + { + DateTime dateTime = new DateTime(ticks).ToUniversalTime(); + results.Insert(0, $"updated: \"{dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}\""); + results.Insert(0, $"created: \"{dateTime:yyyy-MM-ddTHH:mm:ss.fffZ}\""); + 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) { string decade; - string jrOrSr; string? personFullName; const char father = 'F'; const char mother = 'M'; bool hasRelation = false; const string wife = "WIFE"; + List lines = new(); StringBuilder link = new(); const string child = "CHIL"; const string husband = "HUSB"; const string person = "person"; DateTime dateTime = new(ticks); - string fullName = PersonName.GetFullName(personName); List? relations; - string now = dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); - string lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), "[^a-z0-9-]", "-")}"; - string code = IPersonBirthday.GetHour(genealogicalDataCommunication.Death is null, genealogicalDataCommunication.Sex).ToString("00"); - List lines = new() - { - "---", - $"type: '{person}'", - $"created: {now}", - $"updated: {now}", - $"draft: false", - $"title: '{fullName}'", - $"{nameof(lowerHyphenFullName)}: '{lowerHyphenFullName}'", - }; - if (string.IsNullOrEmpty(personName.Alias.Value)) - jrOrSr = string.Empty; - else - { - if (personName.Alias.Value.Contains(" Jr")) - jrOrSr = " Jr"; - else if (personName.Alias.Value.Contains(" Sr")) - jrOrSr = " Sr"; - else - jrOrSr = string.Empty; - } - lines.Add($"name: '{personName.First.Value} /{personName.Last.Value}/{jrOrSr}'"); - if (first && !personKeyFormatted.EndsWith(code)) - personKeyFormatted = $"{personKeyFormatted[..^2]}{code}"; - if (!string.IsNullOrEmpty(personName.First.Value)) - lines.Add($"given: '{personName.First.Value}'"); - if (!string.IsNullOrEmpty(personName.Last.Value)) - lines.Add($"surname: '{personName.Last.Value}'"); - if (!string.IsNullOrEmpty(jrOrSr)) - lines.Add($"suffix: '{jrOrSr.Trim()}'"); - lines.Add($"sex: '{genealogicalDataCommunication.Sex}'"); - if (genealogicalDataCommunication.Birth is not null) - { - lines.Add($"date: {genealogicalDataCommunication.Birth.Value:yyyy-MM-ddT00:00:00.000Z}"); - Models.PersonBirthday personBirthday = new(genealogicalDataCommunication.Birth.Value); - if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday)) - lines.Add($"birthDate: '{genealogicalDataCommunication.Birth.Value:dd MMM yyyy}'"); - } - if (genealogicalDataCommunication.Death is not null) - { - if (genealogicalDataCommunication?.Death is null || genealogicalDataCommunication.Death == genealogicalDataCommunication.Birth || IPersonBirthday.IsCounterPersonBirthday(new(genealogicalDataCommunication.Death.Value))) - lines.Add("isDead: true"); - else - lines.Add($"deathDate: '{genealogicalDataCommunication.Death.Value:dd MMM yyyy}'"); - } - if (isDefaultName) - lines.Add($"{nameof(isDefaultName)}: {isDefaultName}"); - lines.Add($"{nameof(personKeyFormatted)}: '{personKeyFormatted}'"); - lines.Add("---"); + lines.AddRange(frontMatter); lines.Add(string.Empty); lines.Add($"# {fullName}"); lines.Add(string.Empty); @@ -117,7 +95,7 @@ internal abstract class MarkDown if (personFullName is null) _ = link.Append(relation.NickName); else - _ = link.Append(Regex.Replace(personFullName.ToLower(), "[^a-z0-9-]", "-").Replace("--", "-")); + _ = link.Append(Regex.Replace(personFullName.ToLower(), pattern, "-").Replace("--", "-")); _ = link.Append(".md) "); if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo)) lines.Add(link.ToString()); diff --git a/Shared/Models/Stateless/Methods/PersonContainer.cs b/Shared/Models/Stateless/Methods/PersonContainer.cs index 2a33b43..c797f6c 100644 --- a/Shared/Models/Stateless/Methods/PersonContainer.cs +++ b/Shared/Models/Stateless/Methods/PersonContainer.cs @@ -1,5 +1,6 @@ using System.Collections.ObjectModel; using System.Globalization; +using System.Text.RegularExpressions; namespace View_by_Distance.Shared.Models.Stateless.Methods; @@ -391,16 +392,19 @@ internal abstract class PersonContainer return new(results); } - internal static void MaybeWriteFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) + internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List genealogicalDataCommunicationRelations, List personContainers, string a2PeopleContentDirectory) { bool? male; bool? first; + string fullName; string[] matches; string? directory; bool isDefaultName; - bool verify = true; const int zero = 0; + List frontMatter; string personKeyFormatted; + string lowerHyphenFullName; + string pattern = "[^a-z0-9-]"; Calendar calendar = new CultureInfo("en-US").Calendar; Models.GenealogicalDataCommunication genealogicalDataCommunication; GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines; @@ -425,13 +429,19 @@ internal abstract class PersonContainer genealogicalDataCommunicationLines = personContainer.GenealogicalDataCommunicationRelationIndividualsLines is null ? null : GenealogicalDataCommunication.GetGenealogicalDataCommunicationLines(personContainer.Birthdays[zero], personContainer.GenealogicalDataCommunicationRelationIndividualsLines); if (genealogicalDataCommunicationLines is null) continue; - genealogicalDataCommunication = GenealogicalDataCommunication.GetGenealogicalDataCommunication(genealogicalDataCommunicationLines); + genealogicalDataCommunication = GenealogicalDataCommunication.GetGenealogicalDataCommunication(first.Value, genealogicalDataCommunicationLines); if (genealogicalDataCommunication.Sex != personContainer.PersonDirectory.Sex) continue; + if (genealogicalDataCommunication.Birth is not null && !directory.EndsWith(genealogicalDataCommunication.Birth.Value.Hour.ToString())) + continue; if (genealogicalDataCommunication.Death is null && personContainer.PersonDirectory.Status == 'D' || genealogicalDataCommunication.Death is not null && personContainer.PersonDirectory.Status == 'A') continue; - GenealogicalDataCommunication.WriteFile(personKeyFormatted, personContainer.Person.Name, personContainer.GenealogicalDataCommunicationRelationIndividualsLines, isDefaultName, directory, genealogicalDataCommunication, verify, first.Value); - MarkDown.WriteFile(personKeyFormatted, ticks, personContainer.Person.Name, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, personKeyFormattedToPersonFullName, familyIndexToCollection, isDefaultName, genealogicalDataCommunication, first.Value); + fullName = PersonName.GetFullName(personContainer.Person.Name); + lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), pattern, "-")}"; + frontMatter = MarkDown.GetFrontMatter(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication); + if (!frontMatter.Any()) + continue; + MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatter); } } diff --git a/Tests/UnitTestHardCoded.cs b/Tests/UnitTestHardCoded.cs index 71c9b99..da38134 100644 --- a/Tests/UnitTestHardCoded.cs +++ b/Tests/UnitTestHardCoded.cs @@ -265,6 +265,7 @@ public partial class UnitTestHardCoded string directory = "D:/1-Images-A/Images-dd514b88-Results/A2) People/dd514b88/([])"; if (Directory.Exists(Path.GetPathRoot(directory)) && Directory.Exists(directory)) { + bool first = true; List mappedLines; ReadOnlyDictionary individuals; GenealogicalDataCommunication genealogicalDataCommunication; @@ -291,7 +292,7 @@ public partial class UnitTestHardCoded { genealogicalDataCommunicationLines = IGenealogicalDataCommunication.GetGenealogicalDataCommunicationLines(keyValuePair.Value); Assert.IsNotNull(genealogicalDataCommunicationLines.Name); - genealogicalDataCommunication = IGenealogicalDataCommunication.GetGenealogicalDataCommunication(genealogicalDataCommunicationLines); + genealogicalDataCommunication = IGenealogicalDataCommunication.GetGenealogicalDataCommunication(first, genealogicalDataCommunicationLines); Assert.IsNotNull(genealogicalDataCommunication.Name); } mappedLines = IGenealogicalDataCommunication.GetMappedLines(genealogicalDataCommunicationFile, requireNickName); @@ -313,11 +314,11 @@ public partial class UnitTestHardCoded long personKey; string ageGroup; string fileName; - string? directory; bool first = true; + string? directory; PersonName? personName; string personKeyFormatted; - bool isDefaultName = false; + // bool isDefaultName = false; ReadOnlyDictionary individuals; GenealogicalDataCommunication genealogicalDataCommunication; GenealogicalDataCommunicationLines genealogicalDataCommunicationLines; @@ -344,7 +345,7 @@ public partial class UnitTestHardCoded { genealogicalDataCommunicationLines = IGenealogicalDataCommunication.GetGenealogicalDataCommunicationLines(keyValuePair.Value); Assert.IsNotNull(genealogicalDataCommunicationLines.Name); - genealogicalDataCommunication = IGenealogicalDataCommunication.GetGenealogicalDataCommunication(genealogicalDataCommunicationLines); + genealogicalDataCommunication = IGenealogicalDataCommunication.GetGenealogicalDataCommunication(first, genealogicalDataCommunicationLines); Assert.IsNotNull(genealogicalDataCommunication.Name); if (genealogicalDataCommunication.Birth is null) continue; @@ -367,7 +368,7 @@ public partial class UnitTestHardCoded directory = Path.Combine(saveDirectory, fileName, ageGroup, $"{personName.First.Value} {personName.Last.Value}^{age}", personKeyFormatted); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); - IGenealogicalDataCommunication.WriteFile(personKeyFormatted, personName, keyValuePair.Value, isDefaultName, directory, genealogicalDataCommunication, verify, first); + // IGenealogicalDataCommunication.WriteFile(personKeyFormatted, personName, keyValuePair.Value, isDefaultName, directory, genealogicalDataCommunication, verify, first); } } } @@ -676,15 +677,15 @@ public partial class UnitTestHardCoded long personKey; DateTime? death; string ageGroup; - bool first = true; string? directory; - bool verify = false; + // bool first = true; string[] dateSegments; + // bool verify = false; DateTime parseDateTime; PersonName? personName; string[] spaceSegments; string personKeyFormatted; - bool isDefaultName = false; + // bool isDefaultName = false; PersonBirthday personBirthday; string[] family = GetFamily(); GenealogicalDataCommunication genealogicalDataCommunication; @@ -723,8 +724,18 @@ public partial class UnitTestHardCoded directory = Path.Combine(saveDirectory, "Norman", ageGroup, $"{personName.First.Value} {personName.Last.Value}^{age}", personKeyFormatted); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); - genealogicalDataCommunication = new(null, null, null, null, null, null, null, 'U', personBirthday.Value, death, null); - IGenealogicalDataCommunication.WriteFile(personKeyFormatted, personName, null, isDefaultName, directory, genealogicalDataCommunication, verify, first); + genealogicalDataCommunication = new(personBirthday.Value, + null, + death, + null, + null, + null, + "", + 'U', + null, + null, + null); + // IGenealogicalDataCommunication.WriteFile(personKeyFormatted, personName, null, isDefaultName, directory, genealogicalDataCommunication, verify, first); } } NonThrowTryCatch();