Removed GenealogicalDataCommunication.WriteFile

This commit is contained in:
2023-07-21 03:48:06 -07:00
parent 33a142549a
commit 61f5fa20de
9 changed files with 111 additions and 178 deletions

View File

@ -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<string> 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<Models.PersonContainer> personContainers, string[] headerLines, List<string[]> familyGroupLines, string[] footerLines, long ticks, string a2PeopleContentDirectory)
{
bool result = false;

View File

@ -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<string> TestStatic_GetMappedLines(string genealogicalDataCommunicationFile, bool requireNickName) =>
GetMappedLines(genealogicalDataCommunicationFile, requireNickName);
static List<string> 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<string, string[]>, List<string[]>, string[], List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations) TestStatic_GetIndividuals(string genealogicalDataCommunicationFile, bool requireNickName) =>
GetIndividuals(genealogicalDataCommunicationFile, requireNickName);

View File

@ -22,10 +22,10 @@ public interface IPersonContainer
static List<Models.PersonContainer> GetPersonContainers(Properties.IStorage storage, string mappingDefaultName, string personBirthdayFormat, char[] personCharacters, string facesFileNameExtension, ReadOnlyDictionary<string, string[]> individuals) =>
PersonContainer.GetPersonContainers(storage, mappingDefaultName, personBirthdayFormat, personCharacters, facesFileNameExtension, individuals);
void TestStatic_MaybeWriteFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory) =>
MaybeWriteFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory);
static void MaybeWriteFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory) =>
PersonContainer.MaybeWriteFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory);
void TestStatic_MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory) =>
MaybeWriteMarkDownFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory);
static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory) =>
PersonContainer.MaybeWriteMarkDownFiles(mappingDefaultName, personBirthdayFormat, ticks, genealogicalDataCommunicationRelations, personContainers, a2PeopleContentDirectory);
List<(long?, string)> TestStatic_GetDisplay(string personBirthdayFormat, Models.PersonContainer personContainer) =>
GetDisplay(personBirthdayFormat, personContainer);

View File

@ -10,74 +10,52 @@ internal abstract class MarkDown
// ...
internal static void WriteFile(string personKeyFormatted, long ticks, Models.PersonName personName, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, Calendar calendar, ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection, bool isDefaultName, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool first)
internal static List<string> GetFrontMatter(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 (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<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> frontMatter)
{
string decade;
string jrOrSr;
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);
string fullName = PersonName.GetFullName(personName);
List<GenealogicalDataCommunicationRelation>? 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<string> 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());

View File

@ -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<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory)
internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory)
{
bool? male;
bool? first;
string fullName;
string[] matches;
string? directory;
bool isDefaultName;
bool verify = true;
const int zero = 0;
List<string> 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);
}
}