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();
string afterTrim;
@ -33,14 +33,12 @@ internal abstract class MarkDown
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)
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;
@ -55,7 +53,9 @@ internal abstract class MarkDown
const string person = "person";
DateTime dateTime = new(ticks);
List<GenealogicalDataCommunicationRelation>? relations;
lines.AddRange(frontMatter);
lines.Add("---");
lines.AddRange(frontMatterLines);
lines.Add("---");
lines.Add(string.Empty);
lines.Add($"# {fullName}");
lines.Add(string.Empty);

View File

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