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 { // ... private static List GetFrontMatterLines(string[] parsedLines) { List results = new(); string afterTrim; string[] segments; StringBuilder stringBuilder = new(); for (int i = 0; i < parsedLines.Length; i++) { afterTrim = parsedLines[i].Trim(); if (string.IsNullOrEmpty(afterTrim) || afterTrim.First() is '{' or '}') continue; segments = afterTrim.Split(": "); if (segments.Length != 2) { if (results.Last()[^1] == '[') { _ = stringBuilder.Clear(); _ = stringBuilder.Append(results.Last()); results.RemoveAt(results.Count - 1); for (int j = i; j < parsedLines.Length; j++) { i = j; afterTrim = parsedLines[j].Trim(); if (afterTrim != "],") _ = stringBuilder.Append(afterTrim); else _ = stringBuilder.Append(afterTrim[..^1]); if (afterTrim is "]" or "],") { results.Add(stringBuilder.ToString()); break; } } continue; } results.Clear(); break; } if (afterTrim[^1] != ',') results.Add(afterTrim[1..].Replace("\": ", ": ")); else results.Add(afterTrim[1..^1].Replace("\": ", ": ")); } return results; } internal static List GetFrontMatterLines(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication) { List results; string[] parsedLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine); results = GetFrontMatterLines(parsedLines); 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.Add($"{nameof(lowerHyphenFullName)}: \"{lowerHyphenFullName}\""); } 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 frontMatterLines) { string decade; 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); List? relations; lines.Add("---"); lines.AddRange(frontMatterLines); 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(), pattern, "-").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); } }