Use new .NET regular expression source generators
This commit is contained in:
parent
5b4a4341ad
commit
ee8c0ae27b
5 changed files with 107 additions and 92 deletions
|
|
@ -19,7 +19,7 @@ namespace LibationFileManager
|
|||
static abstract IEnumerable<TagCollection> TagCollections { get; }
|
||||
}
|
||||
|
||||
public abstract class Templates
|
||||
public abstract partial class Templates
|
||||
{
|
||||
public const string ERROR_FULL_PATH_IS_INVALID = @"No colons or full paths allowed. Eg: should not start with C:\";
|
||||
public const string WARNING_NO_CHAPTER_NUMBER_TAG = "Should include chapter number tag in template used for naming files which are split by chapter. Ie: <ch#> or <ch# 0>";
|
||||
|
|
@ -250,46 +250,31 @@ namespace LibationFileManager
|
|||
|
||||
#endregion
|
||||
|
||||
#region Tag Formatters
|
||||
#region Tag Formatters
|
||||
|
||||
//Format must have at least one of the string {T}, {F}, {M}, {L}, or {S}
|
||||
private static readonly Regex FormatRegex = new(@"[Ff]ormat\((.*?(?:{[TFMLS]})+.*?)\)", RegexOptions.Compiled);
|
||||
//Sort must have exactly one of the characters F, M, or L
|
||||
private static readonly Regex SortRegex = new(@"[Ss]ort\(\s*?([FML])\s*?\)", RegexOptions.Compiled);
|
||||
//Max must have a 1 or 2-digit number
|
||||
private static readonly Regex MaxRegex = new(@"[Mm]ax\(\s*?(\d{1,2})\s*?\)", RegexOptions.Compiled);
|
||||
//Separator can be anything
|
||||
private static readonly Regex SeparatorRegex = new(@"[Ss]eparator\((.*?)\)", RegexOptions.Compiled);
|
||||
/// <summary> Sort must have exactly one of the characters F, M, or L </summary>
|
||||
[GeneratedRegex(@"[Ss]ort\(\s*?([FML])\s*?\)")]
|
||||
private static partial Regex NamesSortRegex();
|
||||
/// <summary> Format must have at least one of the string {T}, {F}, {M}, {L}, or {S} </summary>
|
||||
[GeneratedRegex(@"[Ff]ormat\((.*?(?:{[TFMLS]})+.*?)\)")]
|
||||
private static partial Regex NamesFormatRegex();
|
||||
/// <summary> Separator can be anything </summary>
|
||||
[GeneratedRegex(@"[Ss]eparator\((.*?)\)")]
|
||||
private static partial Regex NamesSeparatorRegex();
|
||||
/// <summary> Max must have a 1 or 2-digit number </summary>
|
||||
[GeneratedRegex(@"[Mm]ax\(\s*?(\d{1,2})\s*?\)")]
|
||||
private static partial Regex NamesMaxRegex();
|
||||
|
||||
private static string NameListFormatter(ITemplateTag templateTag, IEnumerable<string> value, string formatString)
|
||||
private static string NameListFormatter(ITemplateTag templateTag, IEnumerable<string> names, string formatString)
|
||||
{
|
||||
var names = value.Select(n => new HumanName(removeSuffix(n), Prefer.FirstOverPrefix));
|
||||
|
||||
var formatMatch = FormatRegex.Match(formatString);
|
||||
string nameFormatString = formatMatch.Success ? formatMatch.Groups[1].Value : "{T} {F} {M} {L} {S}";
|
||||
var humanNames = names.Select(n => new HumanName(removeSuffix(n), Prefer.FirstOverPrefix));
|
||||
|
||||
var maxMatch = MaxRegex.Match(formatString);
|
||||
int maxNames = maxMatch.Success && int.TryParse(maxMatch.Groups[1].Value, out var max) ? int.Max(1, max) : int.MaxValue;
|
||||
var sortedNames = sort(humanNames, formatString);
|
||||
var nameFormatString = format(formatString, defaultValue: "{T} {F} {M} {L} {S}");
|
||||
var separatorString = separator(formatString, defaultValue: ", ");
|
||||
var maxNames = max(formatString, defaultValue: humanNames.Count());
|
||||
|
||||
var separatorMatch = SeparatorRegex.Match(formatString);
|
||||
var separatorString = separatorMatch.Success ? separatorMatch.Groups[1].Value : ", ";
|
||||
|
||||
var sortMatch = SortRegex.Match(formatString);
|
||||
var sortedNames
|
||||
= sortMatch.Success
|
||||
? (
|
||||
sortMatch.Groups[1].Value == "F" ? names.OrderBy(n => n.First)
|
||||
: sortMatch.Groups[1].Value == "M" ? names.OrderBy(n => n.Middle)
|
||||
: sortMatch.Groups[1].Value == "L" ? names.OrderBy(n => n.Last)
|
||||
: names
|
||||
)
|
||||
: names;
|
||||
|
||||
var formattedNames = string.Join(
|
||||
separatorString,
|
||||
sortedNames
|
||||
.Take(int.Min(sortedNames.Count(), maxNames))
|
||||
.Select(n => formatName(n, nameFormatString)));
|
||||
var formattedNames = string.Join(separatorString, sortedNames.Take(maxNames).Select(n => formatName(n, nameFormatString)));
|
||||
|
||||
while (formattedNames.Contains(" "))
|
||||
formattedNames = formattedNames.Replace(" ", " ");
|
||||
|
|
@ -299,12 +284,40 @@ namespace LibationFileManager
|
|||
static string removeSuffix(string namesString)
|
||||
{
|
||||
namesString = namesString.Replace('’', '\'').Replace(" - Ret.", ", Ret.");
|
||||
|
||||
int dashIndex = namesString.IndexOf(" - ");
|
||||
|
||||
return (dashIndex > 0 ? namesString[..dashIndex] : namesString).Trim();
|
||||
}
|
||||
|
||||
static IEnumerable<HumanName> sort(IEnumerable<HumanName> humanNames, string formatString)
|
||||
{
|
||||
var sortMatch = NamesSortRegex().Match(formatString);
|
||||
return
|
||||
sortMatch.Success
|
||||
? sortMatch.Groups[1].Value == "F" ? humanNames.OrderBy(n => n.First)
|
||||
: sortMatch.Groups[1].Value == "M" ? humanNames.OrderBy(n => n.Middle)
|
||||
: sortMatch.Groups[1].Value == "L" ? humanNames.OrderBy(n => n.Last)
|
||||
: humanNames
|
||||
: humanNames;
|
||||
}
|
||||
|
||||
static string format(string formatString, string defaultValue)
|
||||
{
|
||||
var formatMatch = NamesFormatRegex().Match(formatString);
|
||||
return formatMatch.Success ? formatMatch.Groups[1].Value : defaultValue;
|
||||
}
|
||||
|
||||
static string separator(string formatString, string defaultValue)
|
||||
{
|
||||
var separatorMatch = NamesSeparatorRegex().Match(formatString);
|
||||
return separatorMatch.Success ? separatorMatch.Groups[1].Value : defaultValue;
|
||||
}
|
||||
|
||||
static int max(string formatString, int defaultValue)
|
||||
{
|
||||
var maxMatch = NamesMaxRegex().Match(formatString);
|
||||
return maxMatch.Success && int.TryParse(maxMatch.Groups[1].Value, out var max) ? int.Max(1, max) : defaultValue;
|
||||
}
|
||||
|
||||
static string formatName(HumanName humanName, string nameFormatString)
|
||||
{
|
||||
//Single-word names parse as first names. Use it as last name.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue