139 lines
6.8 KiB
C#

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 List<string> GetFrontMatterLines(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication)
{
List<string> results = new();
string afterTrim;
string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine);
foreach (string jsonLine in jsonLines)
{
afterTrim = jsonLine.Trim();
if (string.IsNullOrEmpty(afterTrim) || afterTrim.First() is '{' or '}')
continue;
segments = afterTrim.Split(": ");
if (segments.Length != 2)
{
results.Clear();
break;
}
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.Add($"{nameof(lowerHyphenFullName)}: \"{lowerHyphenFullName}\"");
}
return results;
}
internal static void WriteFile(string personKeyFormatted, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, Calendar calendar, string pattern, ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection, Models.GenealogicalDataCommunication genealogicalDataCommunication, string fullName, string lowerHyphenFullName, List<string> frontMatterLines)
{
string decade;
string? personFullName;
const char father = 'F';
const char mother = 'M';
bool hasRelation = false;
const string wife = "WIFE";
List<string> lines = new();
StringBuilder link = new();
const string child = "CHIL";
const string husband = "HUSB";
const string person = "person";
DateTime dateTime = new(ticks);
List<GenealogicalDataCommunicationRelation>? 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);
}
}