155 lines
7.6 KiB
C#
155 lines
7.6 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
|
|
{
|
|
|
|
// ...
|
|
|
|
private static List<string> GetFrontMatterLines(string[] parsedLines)
|
|
{
|
|
List<string> results = new();
|
|
string afterTrim;
|
|
string[] segments;
|
|
StringBuilder stringBuilder = new();
|
|
for (int i = 0; i < parsedLines.Length; i++)
|
|
{
|
|
afterTrim = parsedLines[i].Trim();
|
|
if (string.IsNullOrEmpty(afterTrim) || afterTrim.First() is '{' or '}')
|
|
continue;
|
|
segments = afterTrim.Split(": ");
|
|
if (segments.Length != 2)
|
|
{
|
|
if (results.Last()[^1] == '[')
|
|
{
|
|
_ = stringBuilder.Clear();
|
|
_ = stringBuilder.Append(results.Last());
|
|
results.RemoveAt(results.Count - 1);
|
|
for (int j = i; j < parsedLines.Length; j++)
|
|
{
|
|
i = j;
|
|
afterTrim = parsedLines[j].Trim();
|
|
if (afterTrim == "],")
|
|
_ = stringBuilder.Append(afterTrim[..^1]);
|
|
else if (afterTrim[^1] == ',')
|
|
_ = stringBuilder.Append(afterTrim).Append(' ');
|
|
else
|
|
_ = stringBuilder.Append(afterTrim);
|
|
if (afterTrim is "]" or "],")
|
|
{
|
|
results.Add(stringBuilder.ToString());
|
|
break;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
results.Clear();
|
|
break;
|
|
}
|
|
if (afterTrim[^1] != ',')
|
|
results.Add(afterTrim[1..].Replace("\": ", ": "));
|
|
else
|
|
results.Add(afterTrim[1..^1].Replace("\": ", ": "));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
internal static List<string> GetFrontMatterLines(long ticks, string fullName, string lowerHyphenFullName, Models.GenealogicalDataCommunication genealogicalDataCommunication)
|
|
{
|
|
List<string> results;
|
|
string[] parsedLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine);
|
|
results = GetFrontMatterLines(parsedLines);
|
|
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, 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 text;
|
|
string decade;
|
|
string? personFullName;
|
|
bool hasRelation = false;
|
|
string? mergeWithLineTwo;
|
|
List<string> lines = new();
|
|
StringBuilder link = new();
|
|
const string person = "person";
|
|
DateTime dateTime = new(ticks);
|
|
List<GenealogicalDataCommunicationRelation>? genealogicalDataCommunicationRelations;
|
|
lines.Add("---");
|
|
lines.AddRange(frontMatterLines);
|
|
lines.Add("---");
|
|
lines.Add(string.Empty);
|
|
lines.Add($"# {fullName}");
|
|
lines.Add(string.Empty);
|
|
foreach (KeyValuePair<int, List<GenealogicalDataCommunicationRelation>> keyValuePair in familyIndexToCollection)
|
|
{
|
|
foreach (GenealogicalDataCommunicationRelation genealogicalDataCommunicationRelation in keyValuePair.Value)
|
|
{
|
|
if (genealogicalDataCommunication?.NickName is null || genealogicalDataCommunication.NickName.Length < 4)
|
|
continue;
|
|
if (genealogicalDataCommunicationRelation.Relation != IGenealogicalDataCommunication.Child)
|
|
continue;
|
|
if (genealogicalDataCommunicationRelation.NickName != personKeyFormatted)
|
|
continue;
|
|
if (!familyIndexToCollection.TryGetValue(genealogicalDataCommunicationRelation.FamilyIndex, out genealogicalDataCommunicationRelations))
|
|
continue;
|
|
foreach (GenealogicalDataCommunicationRelation relation in genealogicalDataCommunicationRelations)
|
|
{
|
|
if (relation.FamilyIndex != genealogicalDataCommunicationRelation.FamilyIndex)
|
|
continue;
|
|
if (relation.Relation is IGenealogicalDataCommunication.Husband or IGenealogicalDataCommunication.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) ");
|
|
mergeWithLineTwo = GenealogicalDataCommunication.GetMergeWithLineTwo(genealogicalDataCommunicationRelation, relation);
|
|
lines.Add($"{link}{mergeWithLineTwo}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (hasRelation)
|
|
lines.Add(string.Empty);
|
|
// text = string.Join(Environment.NewLine, lines);
|
|
// _ = IPath.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true);
|
|
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
string markdownOnlyDirectory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString(), person, personKeyFormatted[..3]);
|
|
if (!Directory.Exists(markdownOnlyDirectory))
|
|
_ = Directory.CreateDirectory(markdownOnlyDirectory);
|
|
text = string.Join(Environment.NewLine, lines);
|
|
_ = IPath.WriteAllText(Path.Combine(markdownOnlyDirectory, $"{lowerHyphenFullName}.md"), text, updateDateWhenMatches: false, compareBeforeWrite: true);
|
|
}
|
|
|
|
} |