allFrontMatterLines

This commit is contained in:
Mike Phares 2023-07-21 12:08:04 -07:00
parent c8da883a88
commit 6c711945b1
2 changed files with 40 additions and 16 deletions

View File

@ -10,7 +10,7 @@ internal abstract class MarkDown
// ... // ...
internal static List<string> GetFrontMatter(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication) internal static List<string> GetFrontMatterLines(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication)
{ {
List<string> results = new(); List<string> results = new();
string afterTrim; string afterTrim;
@ -33,14 +33,12 @@ internal abstract class MarkDown
results.Insert(0, $"title: \"{fullName}\""); results.Insert(0, $"title: \"{fullName}\"");
results.Insert(0, "type: \"person\""); results.Insert(0, "type: \"person\"");
results.Add("draft: false"); results.Add("draft: false");
results.Insert(0, "---");
results.Add($"{nameof(lowerHyphenFullName)}: \"{lowerHyphenFullName}\""); results.Add($"{nameof(lowerHyphenFullName)}: \"{lowerHyphenFullName}\"");
results.Add("---");
} }
return results; 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) 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 decade;
string? personFullName; string? personFullName;
@ -55,7 +53,9 @@ internal abstract class MarkDown
const string person = "person"; const string person = "person";
DateTime dateTime = new(ticks); DateTime dateTime = new(ticks);
List<GenealogicalDataCommunicationRelation>? relations; List<GenealogicalDataCommunicationRelation>? relations;
lines.AddRange(frontMatter); lines.Add("---");
lines.AddRange(frontMatterLines);
lines.Add("---");
lines.Add(string.Empty); lines.Add(string.Empty);
lines.Add($"# {fullName}"); lines.Add($"# {fullName}");
lines.Add(string.Empty); lines.Add(string.Empty);

View File

@ -1,5 +1,7 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization; using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace View_by_Distance.Shared.Models.Stateless.Methods; namespace View_by_Distance.Shared.Models.Stateless.Methods;
@ -396,17 +398,20 @@ internal abstract class PersonContainer
{ {
bool? male; bool? male;
bool? first; bool? first;
string json;
string fullName; string fullName;
string[] matches; string[] matches;
string[] segments;
string? directory; string? directory;
bool isDefaultName; bool isDefaultName;
const int zero = 0; const int zero = 0;
List<string> frontMatter;
string personKeyFormatted; string personKeyFormatted;
string frontMatterLastLine;
string lowerHyphenFullName; string lowerHyphenFullName;
List<string> frontMatterLines;
string pattern = "[^a-z0-9-]"; string pattern = "[^a-z0-9-]";
DateTime dateTime = new(ticks); DateTime dateTime = new(ticks);
List<string> allFrontMatter = new(); List<string> allFrontMatterLines = new();
Calendar calendar = new CultureInfo("en-US").Calendar; Calendar calendar = new CultureInfo("en-US").Calendar;
Models.GenealogicalDataCommunication genealogicalDataCommunication; Models.GenealogicalDataCommunication genealogicalDataCommunication;
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines; GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
@ -441,17 +446,36 @@ internal abstract class PersonContainer
continue; continue;
fullName = PersonName.GetFullName(personContainer.Person.Name); fullName = PersonName.GetFullName(personContainer.Person.Name);
lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), pattern, "-")}"; lowerHyphenFullName = $"{Regex.Replace(fullName.ToLower(), pattern, "-")}";
frontMatter = MarkDown.GetFrontMatter(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication); frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication);
if (!frontMatter.Any()) if (!frontMatterLines.Any())
continue; continue;
allFrontMatter.AddRange(frontMatter); allFrontMatterLines.Add("{");
MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatter); frontMatterLastLine = frontMatterLines.Last();
foreach (string frontMatterLine in frontMatterLines)
{
segments = frontMatterLine.Split(": ");
if (segments.Length != 2)
continue;
if (frontMatterLine == frontMatterLastLine)
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)}");
else
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)},");
} }
allFrontMatterLines.Add("},");
MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines);
}
if (allFrontMatterLines.Any())
{
directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString()); directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString());
if (!Directory.Exists(directory)) if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory); _ = Directory.CreateDirectory(directory);
string text = string.Join(Environment.NewLine, allFrontMatter); json = $"[{string.Join(Environment.NewLine, allFrontMatterLines.ToArray(), 0, allFrontMatterLines.Count - 1)}{allFrontMatterLines.Last()[..^1]}]";
_ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true); Models.GenealogicalDataCommunication[]? genealogicalDataCommunications = JsonSerializer.Deserialize<Models.GenealogicalDataCommunication[]>(json);
if (genealogicalDataCommunications is null)
throw new NullReferenceException(nameof(genealogicalDataCommunications));
json = JsonSerializer.Serialize(genealogicalDataCommunications, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
_ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true);
}
} }
} }