using System.Collections.ObjectModel; using System.Text.RegularExpressions; namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class MarkDown { // ... internal static void WriteFile(string personKeyFormatted, long ticks, Models.PersonName personName, List genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, ReadOnlyDictionary personKeyFormattedToPersonFullName, ReadOnlyDictionary> familyIndexToCollection, bool isDefaultName, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool first) { string decade; string jrOrSr; string? personFullName; const char father = 'F'; const char mother = 'M'; bool hasRelation = false; const string wife = "WIFE"; string lowerHyphenRelation; const string child = "CHIL"; const string husband = "HUSB"; const string person = "person"; string fullName = PersonName.GetFullName(personName); List? relations; string now = DateTime.Now.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(fullName)}: '{fullName}'", }; 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.Add(string.Empty); lines.Add($"# {lowerHyphenFullName}"); lines.Add(string.Empty); foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations) { if (genealogicalDataCommunicationRelation.Relation != child) continue; if (genealogicalDataCommunicationRelation.NickName != personKeyFormatted) continue; if (!familyIndexToCollection.TryGetValue(genealogicalDataCommunicationRelation.FamilyIndex, out relations)) continue; foreach (GenealogicalDataCommunicationRelation relation in relations) { if (relation.FamilyIndex != genealogicalDataCommunicationRelation.FamilyIndex) continue; if (relation.Relation is husband or wife) { if (!hasRelation) { lines.Add("## Relations"); lines.Add(string.Empty); hasRelation = true; } decade = relation.NickName[..3]; if (!personKeyFormattedToPersonFullName.TryGetValue(relation.NickName, out personFullName)) lowerHyphenRelation = relation.NickName; else lowerHyphenRelation = Regex.Replace(personFullName.ToLower(), "[^a-z0-9-]", "-").Replace("--", "-"); if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo)) lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]]"); else { if (genealogicalDataCommunicationRelation.LineTwo[1] == father) { if (relation.Relation == wife) lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {nameof(mother)}"); else if (relation.Relation == husband) lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(father)}"); } else if (genealogicalDataCommunicationRelation.LineTwo[1] == mother) { if (relation.Relation == husband) lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {nameof(father)}"); else if (relation.Relation == wife) lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(mother)}"); } } } } } if (hasRelation) lines.Add(string.Empty); string text = string.Join(Environment.NewLine, lines); string directory = Path.Combine(a2PeopleContentDirectory, $"content-{ticks}", person, personKeyFormatted[..3]); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); _ = IPath.WriteAllText(Path.Combine(directory, $"{lowerHyphenFullName}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true); } }