using System.Collections.ObjectModel; using System.Globalization; using System.Text; 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, Calendar calendar, 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"; 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.Add(string.Empty); lines.Add($"# {fullName}"); lines.Add(string.Empty); foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations) { if (genealogicalDataCommunication?.NickName is null || genealogicalDataCommunication.NickName.Length < 4) continue; 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]; _ = link.Clear(); _ = link.Append("- ["); if (!personKeyFormattedToPersonFullName.TryGetValue(relation.NickName, out personFullName)) _ = link.Append(relation.NickName); else _ = link.Append(personFullName); if (genealogicalDataCommunication.NickName[..3] == decade) _ = link.Append("]("); else _ = link.Append("](../").Append(decade).Append('/'); if (personFullName is null) _ = link.Append(relation.NickName); else _ = link.Append(Regex.Replace(personFullName.ToLower(), "[^a-z0-9-]", "-").Replace("--", "-")); _ = link.Append(".md) "); if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo)) lines.Add(link.ToString()); else { if (genealogicalDataCommunicationRelation.LineTwo[1] == father) { if (relation.Relation == wife) _ = link.Append(nameof(mother)); else if (relation.Relation == husband) _ = link.Append(genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()).Append(nameof(father)); } else if (genealogicalDataCommunicationRelation.LineTwo[1] == mother) { if (relation.Relation == husband) _ = link.Append(nameof(father)); else if (relation.Relation == wife) _ = link.Append(genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()).Append(nameof(mother)); } lines.Add(link.ToString()); } } } } if (hasRelation) lines.Add(string.Empty); string text = string.Join(Environment.NewLine, lines); string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); string directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString(), person, personKeyFormatted[..3]); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); _ = IPath.WriteAllText(Path.Combine(directory, $"{lowerHyphenFullName}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true); } }