Make suggested changes
This commit is contained in:
parent
3a4ab80892
commit
1f4ada604a
@ -72,6 +72,7 @@ namespace LibationFileManager
|
|||||||
(_, e) => _chapterTitle = GetTemplate<ChapterTitleTemplate>((string)e.NewValue);
|
(_, e) => _chapterTitle = GetTemplate<ChapterTitleTemplate>((string)e.NewValue);
|
||||||
|
|
||||||
HumanName.Suffixes.Add("ret");
|
HumanName.Suffixes.Add("ret");
|
||||||
|
HumanName.Titles.Add("professor");
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -202,9 +203,9 @@ namespace LibationFileManager
|
|||||||
{ TemplateTags.Id, lb => lb.AudibleProductId, v => v },
|
{ TemplateTags.Id, lb => lb.AudibleProductId, v => v },
|
||||||
{ TemplateTags.Title, lb => lb.Title },
|
{ TemplateTags.Title, lb => lb.Title },
|
||||||
{ TemplateTags.TitleShort, lb => getTitleShort(lb.Title) },
|
{ TemplateTags.TitleShort, lb => getTitleShort(lb.Title) },
|
||||||
{ TemplateTags.Author, lb => lb.Authors, NameFormatter },
|
{ TemplateTags.Author, lb => lb.Authors, NameListFormatter },
|
||||||
{ TemplateTags.FirstAuthor, lb => lb.FirstAuthor },
|
{ TemplateTags.FirstAuthor, lb => lb.FirstAuthor },
|
||||||
{ TemplateTags.Narrator, lb => lb.Narrators, NameFormatter },
|
{ TemplateTags.Narrator, lb => lb.Narrators, NameListFormatter },
|
||||||
{ TemplateTags.FirstNarrator, lb => lb.FirstNarrator },
|
{ TemplateTags.FirstNarrator, lb => lb.FirstNarrator },
|
||||||
{ TemplateTags.Series, lb => lb.SeriesName },
|
{ TemplateTags.Series, lb => lb.SeriesName },
|
||||||
{ TemplateTags.SeriesNumber, lb => lb.SeriesNumber },
|
{ TemplateTags.SeriesNumber, lb => lb.SeriesNumber },
|
||||||
@ -251,16 +252,6 @@ namespace LibationFileManager
|
|||||||
|
|
||||||
#region Tag Formatters
|
#region Tag Formatters
|
||||||
|
|
||||||
private static readonly string[] suffixes = { "introductions", "introduction", "adaptation", "translator", "contributor", "illustrator", "director", "foreword", "editor", "preface", "adaptor", "afterword", "interviewer", "introductions", "essay", "editor/introduction" };
|
|
||||||
|
|
||||||
private static string removeSuffix(string namesString)
|
|
||||||
{
|
|
||||||
foreach (var suffix in suffixes)
|
|
||||||
namesString = namesString.Replace($" - {suffix}", "");
|
|
||||||
|
|
||||||
return namesString.Replace('’', '\'').Replace(" - Ret.", ", Ret.").Trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Format must have at least one of the string {T}, {F}, {M}, {L}, or {S}
|
//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);
|
private static readonly Regex FormatRegex = new(@"[Ff]ormat\((.*?(?:{[TFMLS]})+.*?)\)", RegexOptions.Compiled);
|
||||||
//Sort must have exactly one of the characters F, M, or L
|
//Sort must have exactly one of the characters F, M, or L
|
||||||
@ -270,20 +261,12 @@ namespace LibationFileManager
|
|||||||
//Separator can be anything
|
//Separator can be anything
|
||||||
private static readonly Regex SeparatorRegex = new(@"[Ss]eparator\((.*?)\)", RegexOptions.Compiled);
|
private static readonly Regex SeparatorRegex = new(@"[Ss]eparator\((.*?)\)", RegexOptions.Compiled);
|
||||||
|
|
||||||
private static string NameFormatter(ITemplateTag templateTag, IEnumerable<string> value, string formatString)
|
private static string NameListFormatter(ITemplateTag templateTag, IEnumerable<string> value, string formatString)
|
||||||
{
|
{
|
||||||
var names = value.Select(n => new HumanName(removeSuffix(n), Prefer.FirstOverPrefix));
|
var names = value.Select(n => new HumanName(removeSuffix(n), Prefer.FirstOverPrefix));
|
||||||
|
|
||||||
var formatMatch = FormatRegex.Match(formatString);
|
var formatMatch = FormatRegex.Match(formatString);
|
||||||
string nameFormatString
|
string nameFormatString = formatMatch.Success ? formatMatch.Groups[1].Value : "{T} {F} {M} {L} {S}";
|
||||||
= formatMatch.Success
|
|
||||||
? formatMatch.Groups[1].Value
|
|
||||||
.Replace("{T}", "{0}")
|
|
||||||
.Replace("{F}", "{1}")
|
|
||||||
.Replace("{M}", "{2}")
|
|
||||||
.Replace("{L}", "{3}")
|
|
||||||
.Replace("{S}", "{4}")
|
|
||||||
: "{0} {1} {2} {3} {4}"; // T F M L S
|
|
||||||
|
|
||||||
var maxMatch = MaxRegex.Match(formatString);
|
var maxMatch = MaxRegex.Match(formatString);
|
||||||
int maxNames = maxMatch.Success && int.TryParse(maxMatch.Groups[1].Value, out var max) ? int.Max(1, max) : int.MaxValue;
|
int maxNames = maxMatch.Success && int.TryParse(maxMatch.Groups[1].Value, out var max) ? int.Max(1, max) : int.MaxValue;
|
||||||
@ -306,13 +289,37 @@ namespace LibationFileManager
|
|||||||
separatorString,
|
separatorString,
|
||||||
sortedNames
|
sortedNames
|
||||||
.Take(int.Min(sortedNames.Count(), maxNames))
|
.Take(int.Min(sortedNames.Count(), maxNames))
|
||||||
.Select(n => string.Format(nameFormatString, n.Title, n.First, n.Middle, n.Last, n.Suffix).Trim())
|
.Select(n => formatName(n, nameFormatString)));
|
||||||
);
|
|
||||||
|
|
||||||
while (formattedNames.Contains(" "))
|
while (formattedNames.Contains(" "))
|
||||||
formattedNames = formattedNames.Replace(" ", " ");
|
formattedNames = formattedNames.Replace(" ", " ");
|
||||||
|
|
||||||
return formattedNames;
|
return formattedNames;
|
||||||
|
|
||||||
|
static string removeSuffix(string namesString)
|
||||||
|
{
|
||||||
|
namesString = namesString.Replace('’', '\'').Replace(" - Ret.", ", Ret.");
|
||||||
|
|
||||||
|
int dashIndex = namesString.IndexOf(" - ");
|
||||||
|
|
||||||
|
return (dashIndex > 0 ? namesString[..dashIndex] : namesString).Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
static string formatName(HumanName humanName, string nameFormatString)
|
||||||
|
{
|
||||||
|
//Single-word names parse as first names. Use it as last name.
|
||||||
|
var lastName = string.IsNullOrWhiteSpace(humanName.Last) ? humanName.First : humanName.Last;
|
||||||
|
|
||||||
|
nameFormatString
|
||||||
|
= nameFormatString
|
||||||
|
.Replace("{T}", "{0}")
|
||||||
|
.Replace("{F}", "{1}")
|
||||||
|
.Replace("{M}", "{2}")
|
||||||
|
.Replace("{L}", "{3}")
|
||||||
|
.Replace("{S}", "{4}");
|
||||||
|
|
||||||
|
return string.Format(nameFormatString, humanName.Title, humanName.First, humanName.Middle, lastName, humanName.Suffix).Trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string getTitleShort(string title)
|
private static string getTitleShort(string title)
|
||||||
|
|||||||
@ -239,28 +239,88 @@ namespace TemplatesTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[DataRow("<author>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Bruce Bueno de Mesquita", "Title=, First=Bruce, Middle=Bueno Last=de Mesquita, Suffix=")]
|
||||||
[DataRow("<author[]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Ramon de Ocampo", "Title=, First=Ramon, Middle= Last=de Ocampo, Suffix=")]
|
||||||
[DataRow("<author[sort(F)]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Ramon De Ocampo", "Title=, First=Ramon, Middle= Last=De Ocampo, Suffix=")]
|
||||||
[DataRow("<author[sort(L)]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Jennifer Van Dyck", "Title=, First=Jennifer, Middle= Last=Van Dyck, Suffix=")]
|
||||||
[DataRow("<author[sort(M)]>", "Stephen Fry, Arthur Conan Doyle")]
|
[DataRow("Carla Naumburg PhD", "Title=, First=Carla, Middle= Last=Naumburg, Suffix=PhD")]
|
||||||
[DataRow("<author[sort(m)]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Doug Stanhope and Friends", "Title=, First=Doug, Middle= Last=Stanhope and Friends, Suffix=")]
|
||||||
[DataRow("<author [ max( 1 ) ]>", "Arthur Conan Doyle")]
|
[DataRow("Tamara Lovatt-Smith", "Title=, First=Tamara, Middle= Last=Lovatt-Smith, Suffix=")]
|
||||||
[DataRow("<author[max(2)]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Common", "Title=, First=Common, Middle= Last=Common, Suffix=")]
|
||||||
[DataRow("<author[max(3)]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Doug Tisdale Jr.", "Title=, First=Doug, Middle= Last=Tisdale, Suffix=Jr")]
|
||||||
[DataRow("<author[format({L}, {F})]>", "Doyle, Arthur, Fry, Stephen")]
|
[DataRow("Robert S. Mueller III", "Title=, First=Robert, Middle=S. Last=Mueller, Suffix=III")]
|
||||||
[DataRow("<author[format({f}, {l})]>", "Arthur Conan Doyle, Stephen Fry")]
|
[DataRow("Frank T Vertosick Jr. MD", "Title=, First=Frank, Middle=T Last=Vertosick, Suffix=Jr. MD")]
|
||||||
[DataRow("<author[format(First={F}, Last={L})]>", "First=Arthur, Last=Doyle, First=Stephen, Last=Fry")]
|
[DataRow("The Arabian Nights", "Title=, First=The Arabian, Middle= Last=Nights, Suffix=")]
|
||||||
[DataRow("<author[format({L}, {F}) separator( - )]>", "Doyle, Arthur - Fry, Stephen")]
|
[DataRow("The Great Courses", "Title=, First=The Great, Middle= Last=Courses, Suffix=")]
|
||||||
public void NameFormat(string template, string expected)
|
[DataRow("The Laurie Berkner Band", "Title=, First=The Laurie, Middle=Berkner Last=Band, Suffix=")]
|
||||||
|
[DataRow("Committee on Foreign Affairs", "Title=, First=Committee, Middle=on Last=Foreign Affairs, Suffix=")]
|
||||||
|
[DataRow("House Permanent Select Committee on Intelligence", "Title=, First=House, Middle=Permanent Select Committee on Last=Intelligence, Suffix=")]
|
||||||
|
[DataRow("Professor David K. Johnson PhD University of Oklahoma", "Title=Professor, First=David, Middle=K. Johnson PhD Last=University of Oklahoma, Suffix=")]
|
||||||
|
[DataRow("Festival of the Spoken Nerd", "Title=, First=Festival of the Spoken, Middle= Last=Nerd, Suffix=")]
|
||||||
|
[DataRow("Audible Original", "Title=, First=Audible, Middle= Last=Original, Suffix=")]
|
||||||
|
[DataRow("Audible Originals", "Title=, First=Audible, Middle= Last=Originals, Suffix=")]
|
||||||
|
[DataRow("Patrick O'Brian", "Title=, First=Patrick, Middle= Last=O'Brian, Suffix=")]
|
||||||
|
[DataRow("Patrick O’Connell", "Title=, First=Patrick, Middle= Last=O'Connell, Suffix=")]
|
||||||
|
[DataRow("L.E. Modesitt", "Title=, First=L.E., Middle= Last=Modesitt, Suffix=")]
|
||||||
|
[DataRow("L. E. Modesitt Jr.", "Title=, First=L., Middle=E. Last=Modesitt, Suffix=Jr")]
|
||||||
|
[DataRow("LE Modesitt, Jr.", "Title=, First=LE, Middle= Last=Modesitt, Suffix=Jr")]
|
||||||
|
[DataRow("Marine Le Pen", "Title=, First=Marine, Middle= Last=Le Pen, Suffix=")]
|
||||||
|
[DataRow("L. Sprague de Camp", "Title=, First=L., Middle=Sprague Last=de Camp, Suffix=")]
|
||||||
|
[DataRow("Lt. Col. - Ret. Douglas L. Bland", "Title=, First=Ret., Middle=Douglas L. Bland Last=Lt. Col., Suffix=")]
|
||||||
|
[DataRow("Col. Lee Ellis - Ret. - foreword", "Title=Col., First=Lee, Middle= Last=Ellis, Suffix=Ret")]
|
||||||
|
public void NameFormat_unusual(string author, string expected)
|
||||||
{
|
{
|
||||||
Templates.TryGetTemplate<Templates.FileTemplate>(template, out var fileTemplate).Should().BeTrue();
|
var bookDto = GetLibraryBook();
|
||||||
|
bookDto.Authors = new List<string> { author };
|
||||||
|
Templates.TryGetTemplate<Templates.FileTemplate>("<author[format(Title={T}, First={F}, Middle={M} Last={L}, Suffix={S})]>", out var fileTemplate).Should().BeTrue();
|
||||||
fileTemplate
|
fileTemplate
|
||||||
.GetFilename(GetLibraryBook(), "", "", Replacements)
|
.GetFilename(bookDto, "", "", Replacements)
|
||||||
.PathWithoutPrefix
|
.PathWithoutPrefix
|
||||||
.Should().Be(expected);
|
.Should().Be(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[DataRow("<author>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author[]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author[sort(F)]>", "Charles E. Gannon, Christopher John Fetherolf, Jill Conner Browne, Jon Bon Jovi, Lucy Maud Montgomery, Paul Van Doren")]
|
||||||
|
[DataRow("<author[sort(L)]>", "Jon Bon Jovi, Jill Conner Browne, Christopher John Fetherolf, Charles E. Gannon, Lucy Maud Montgomery, Paul Van Doren")]
|
||||||
|
[DataRow("<author[sort(M)]>", "Jon Bon Jovi, Paul Van Doren, Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery")]
|
||||||
|
[DataRow("<author[sort(f)]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author[sort(m)]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author[sort(l)]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author [ max( 1 ) ]>", "Jill Conner Browne")]
|
||||||
|
[DataRow("<author[max(2)]>", "Jill Conner Browne, Charles E. Gannon")]
|
||||||
|
[DataRow("<author[max(3)]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf")]
|
||||||
|
[DataRow("<author[format({L}, {F})]>", "Browne, Jill, Gannon, Charles, Fetherolf, Christopher, Montgomery, Lucy, Bon Jovi, Jon, Van Doren, Paul")]
|
||||||
|
[DataRow("<author[format({f}, {l})]>", "Jill Conner Browne, Charles E. Gannon, Christopher John Fetherolf, Lucy Maud Montgomery, Jon Bon Jovi, Paul Van Doren")]
|
||||||
|
[DataRow("<author[format(First={F}, Last={L})]>", "First=Jill, Last=Browne, First=Charles, Last=Gannon, First=Christopher, Last=Fetherolf, First=Lucy, Last=Montgomery, First=Jon, Last=Bon Jovi, First=Paul, Last=Van Doren")]
|
||||||
|
[DataRow("<author[format({L}, {F}) separator( - ) max(3)]>", "Browne, Jill - Gannon, Charles - Fetherolf, Christopher")]
|
||||||
|
[DataRow("<author[sort(F) max(2) separator(; ) format({F})]>", "Charles; Christopher")]
|
||||||
|
[DataRow("<author[sort(L) max(2) separator(; ) format({L})]>", "Bon Jovi; Browne")]
|
||||||
|
//Jon Bon Jovi and Paul Van Doren don't have middle names, so they are sorted to the top.
|
||||||
|
//Since only the middle names of the first 2 names are to be displayed, the name string is empty.
|
||||||
|
[DataRow("<author[sort(M) max(2) separator(; ) format({M})]>", ";")]
|
||||||
|
public void NameFormat_formatters(string template, string expected)
|
||||||
|
{
|
||||||
|
var bookDto = GetLibraryBook();
|
||||||
|
bookDto.Authors = new List<string>
|
||||||
|
{
|
||||||
|
"Jill Conner Browne",
|
||||||
|
"Charles E. Gannon",
|
||||||
|
"Christopher John Fetherolf",
|
||||||
|
"Lucy Maud Montgomery",
|
||||||
|
"Jon Bon Jovi",
|
||||||
|
"Paul Van Doren"
|
||||||
|
};
|
||||||
|
|
||||||
|
Templates.TryGetTemplate<Templates.FileTemplate>(template, out var fileTemplate).Should().BeTrue();
|
||||||
|
fileTemplate
|
||||||
|
.GetFilename(bookDto, "", "", Replacements)
|
||||||
|
.PathWithoutPrefix
|
||||||
|
.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[DataRow(@"C:\a\b", @"C:\a\b\foobar.ext", PlatformID.Win32NT)]
|
[DataRow(@"C:\a\b", @"C:\a\b\foobar.ext", PlatformID.Win32NT)]
|
||||||
[DataRow(@"/a/b", @"/a/b/foobar.ext", PlatformID.Unix)]
|
[DataRow(@"/a/b", @"/a/b/foobar.ext", PlatformID.Unix)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user