Family Tree as Markdown Files - Hugo - Decade

This commit is contained in:
Mike Phares 2023-07-17 08:50:24 -07:00
parent a154662b9a
commit 5524a9eca2
2 changed files with 34 additions and 38 deletions

View File

@ -8,14 +8,35 @@ internal abstract class MarkDown
// ... // ...
internal static void WriteFile(string personKeyFormatted, long ticks, Models.PersonName personName, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection, bool isDefaultName, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool first, string fullName, string lowerHyphenFullName) internal static void WriteFile(string personKeyFormatted, long ticks, Models.PersonName personName, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, string a2PeopleContentDirectory, ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName, ReadOnlyDictionary<int, List<GenealogicalDataCommunicationRelation>> familyIndexToCollection, bool isDefaultName, Models.GenealogicalDataCommunication genealogicalDataCommunication, bool first)
{ {
string decade;
string jrOrSr;
string? personFullName; string? personFullName;
const char father = 'F';
const char mother = 'M';
bool hasRelation = false; bool hasRelation = false;
const string wife = "WIFE";
string lowerHyphenRelation; string lowerHyphenRelation;
const string child = "CHIL";
const string husband = "HUSB";
const string person = "person";
string fullName = PersonName.GetFullName(personName);
List<GenealogicalDataCommunicationRelation>? relations; List<GenealogicalDataCommunicationRelation>? relations;
string now = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); string now = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
string jrOrSr; 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(fullName)}: '{fullName}'",
};
if (string.IsNullOrEmpty(personName.Alias.Value)) if (string.IsNullOrEmpty(personName.Alias.Value))
jrOrSr = string.Empty; jrOrSr = string.Empty;
else else
@ -27,20 +48,9 @@ internal abstract class MarkDown
else else
jrOrSr = string.Empty; jrOrSr = string.Empty;
} }
string code = IPersonBirthday.GetHour(genealogicalDataCommunication.Death is null, genealogicalDataCommunication.Sex).ToString("00"); lines.Add($"name: '{personName.First.Value} /{personName.Last.Value}/{jrOrSr}'");
if (first && !personKeyFormatted.EndsWith(code)) if (first && !personKeyFormatted.EndsWith(code))
personKeyFormatted = $"{personKeyFormatted[..^2]}{code}"; personKeyFormatted = $"{personKeyFormatted[..^2]}{code}";
List<string> lines = new()
{
"---",
"type: person",
$"created: {now}",
$"updated: {now}",
$"draft: false",
$"title: '{fullName}'",
$"{nameof(fullName)}: '{fullName}'",
$"name: '{personName.First.Value} /{personName.Last.Value}/{jrOrSr}'",
};
if (!string.IsNullOrEmpty(personName.First.Value)) if (!string.IsNullOrEmpty(personName.First.Value))
lines.Add($"given: '{personName.First.Value}'"); lines.Add($"given: '{personName.First.Value}'");
if (!string.IsNullOrEmpty(personName.Last.Value)) if (!string.IsNullOrEmpty(personName.Last.Value))
@ -64,17 +74,11 @@ internal abstract class MarkDown
} }
if (isDefaultName) if (isDefaultName)
lines.Add($"{nameof(isDefaultName)}: {isDefaultName}"); lines.Add($"{nameof(isDefaultName)}: {isDefaultName}");
lines.Add($"{nameof(personKeyFormatted)}: {personKeyFormatted}"); lines.Add($"{nameof(personKeyFormatted)}: '{personKeyFormatted}'");
lines.Add("---"); lines.Add("---");
lines.Add(string.Empty); lines.Add(string.Empty);
lines.Add($"# {lowerHyphenFullName}"); lines.Add($"# {lowerHyphenFullName}");
lines.Add(string.Empty); lines.Add(string.Empty);
const char father = 'F';
const char mother = 'M';
const string wife = "WIFE";
const string child = "CHIL";
const string husband = "HUSB";
const string person = "person";
foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations) foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in genealogicalDataCommunicationRelations)
{ {
if (genealogicalDataCommunicationRelation.Relation != child) if (genealogicalDataCommunicationRelation.Relation != child)
@ -95,27 +99,28 @@ internal abstract class MarkDown
lines.Add(string.Empty); lines.Add(string.Empty);
hasRelation = true; hasRelation = true;
} }
decade = relation.NickName[..3];
if (!personKeyFormattedToPersonFullName.TryGetValue(relation.NickName, out personFullName)) if (!personKeyFormattedToPersonFullName.TryGetValue(relation.NickName, out personFullName))
lowerHyphenRelation = relation.NickName; lowerHyphenRelation = relation.NickName;
else else
lowerHyphenRelation = Regex.Replace(personFullName.ToLower(), "[^a-z0-9-]", "-"); lowerHyphenRelation = Regex.Replace(personFullName.ToLower(), "[^a-z0-9-]", "-").Replace("--", "-");
if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo)) if (string.IsNullOrEmpty(genealogicalDataCommunicationRelation.LineTwo))
lines.Add($"- [[{person}/{lowerHyphenRelation}]]"); lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]]");
else else
{ {
if (genealogicalDataCommunicationRelation.LineTwo[1] == father) if (genealogicalDataCommunicationRelation.LineTwo[1] == father)
{ {
if (relation.Relation == wife) if (relation.Relation == wife)
lines.Add($"- [[{person}/{lowerHyphenRelation}]] {nameof(mother)}"); lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {nameof(mother)}");
else if (relation.Relation == husband) else if (relation.Relation == husband)
lines.Add($"- [[{person}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(father)}"); lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(father)}");
} }
else if (genealogicalDataCommunicationRelation.LineTwo[1] == mother) else if (genealogicalDataCommunicationRelation.LineTwo[1] == mother)
{ {
if (relation.Relation == husband) if (relation.Relation == husband)
lines.Add($"- [[{person}/{lowerHyphenRelation}]] {nameof(father)}"); lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {nameof(father)}");
else if (relation.Relation == wife) else if (relation.Relation == wife)
lines.Add($"- [[{person}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(mother)}"); lines.Add($"- [[{person}/{decade}/{lowerHyphenRelation}]] {genealogicalDataCommunicationRelation.LineTwo.Split(' ').Last()} {nameof(mother)}");
} }
} }
} }
@ -124,7 +129,7 @@ internal abstract class MarkDown
if (hasRelation) if (hasRelation)
lines.Add(string.Empty); lines.Add(string.Empty);
string text = string.Join(Environment.NewLine, lines); string text = string.Join(Environment.NewLine, lines);
string directory = Path.Combine(a2PeopleContentDirectory, $"content-{ticks}", person); string directory = Path.Combine(a2PeopleContentDirectory, $"content-{ticks}", person, personKeyFormatted[..3]);
if (!Directory.Exists(directory)) if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory); _ = Directory.CreateDirectory(directory);
_ = IPath.WriteAllText(Path.Combine(directory, $"{lowerHyphenFullName}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true); _ = IPath.WriteAllText(Path.Combine(directory, $"{lowerHyphenFullName}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true);

View File

@ -1,5 +1,4 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace View_by_Distance.Shared.Models.Stateless.Methods; namespace View_by_Distance.Shared.Models.Stateless.Methods;
@ -395,15 +394,12 @@ internal abstract class PersonContainer
{ {
bool? male; bool? male;
bool? first; bool? first;
string fullName;
string[] matches; string[] matches;
string? directory; string? directory;
bool isDefaultName; bool isDefaultName;
bool verify = true; bool verify = true;
const int zero = 0; const int zero = 0;
string personKeyFormatted; string personKeyFormatted;
string lowerHyphenFullName;
List<string> distinct = new();
Models.GenealogicalDataCommunication genealogicalDataCommunication; Models.GenealogicalDataCommunication genealogicalDataCommunication;
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines; GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName = GetPersonKeyFormattedToPersonFullName(personBirthdayFormat, personContainers); ReadOnlyDictionary<string, string> personKeyFormattedToPersonFullName = GetPersonKeyFormattedToPersonFullName(personBirthdayFormat, personContainers);
@ -433,12 +429,7 @@ internal abstract class PersonContainer
if (genealogicalDataCommunication.Death is null && personContainer.PersonDirectory.Status == 'D' || genealogicalDataCommunication.Death is not null && personContainer.PersonDirectory.Status == 'A') if (genealogicalDataCommunication.Death is null && personContainer.PersonDirectory.Status == 'D' || genealogicalDataCommunication.Death is not null && personContainer.PersonDirectory.Status == 'A')
continue; continue;
GenealogicalDataCommunication.WriteFile(personKeyFormatted, personContainer.Person.Name, personContainer.GenealogicalDataCommunicationRelationIndividualsLines, isDefaultName, directory, genealogicalDataCommunication, verify, first.Value); GenealogicalDataCommunication.WriteFile(personKeyFormatted, personContainer.Person.Name, personContainer.GenealogicalDataCommunicationRelationIndividualsLines, isDefaultName, directory, genealogicalDataCommunication, verify, first.Value);
fullName = PersonName.GetFullName(personContainer.Person.Name); MarkDown.WriteFile(personKeyFormatted, ticks, personContainer.Person.Name, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, personKeyFormattedToPersonFullName, familyIndexToCollection, isDefaultName, genealogicalDataCommunication, first.Value);
lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), "[^a-z0-9-]", "-")}";
if (distinct.Contains(lowerHyphenFullName))
continue;
MarkDown.WriteFile(personKeyFormatted, ticks, personContainer.Person.Name, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, personKeyFormattedToPersonFullName, familyIndexToCollection, isDefaultName, genealogicalDataCommunication, first.Value, fullName, lowerHyphenFullName);
distinct.Add(lowerHyphenFullName);
} }
} }