Adding {subtitle} to the file naming patterns variables (#1195)
This commit is contained in:
@@ -36,6 +36,8 @@ public class PathPatternResolver {
|
||||
? metadata.getTitle()
|
||||
: "Untitled");
|
||||
|
||||
String subtitle = sanitize(metadata != null ? metadata.getSubtitle() : "");
|
||||
|
||||
String authors = sanitize(
|
||||
metadata != null
|
||||
? String.join(", ", metadata.getAuthors())
|
||||
@@ -70,6 +72,7 @@ public class PathPatternResolver {
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
values.put("authors", authors);
|
||||
values.put("title", title);
|
||||
values.put("subtitle", subtitle);
|
||||
values.put("year", year);
|
||||
values.put("series", series);
|
||||
values.put("seriesIndex", seriesIndex);
|
||||
@@ -169,6 +172,8 @@ public class PathPatternResolver {
|
||||
private interface MetadataProvider {
|
||||
String getTitle();
|
||||
|
||||
String getSubtitle();
|
||||
|
||||
List<String> getAuthors();
|
||||
|
||||
Integer getYear();
|
||||
@@ -211,6 +216,11 @@ public class PathPatternResolver {
|
||||
return metadata.getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSubtitle() {
|
||||
return metadata.getSubtitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAuthors() {
|
||||
return metadata.getAuthors() != null ? metadata.getAuthors().stream().toList() : Collections.emptyList();
|
||||
@@ -264,6 +274,11 @@ public class PathPatternResolver {
|
||||
return metadata.getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSubtitle() {
|
||||
return metadata.getSubtitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAuthors() {
|
||||
return metadata.getAuthors() != null
|
||||
|
||||
@@ -17,7 +17,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
class PathPatternResolverTest {
|
||||
|
||||
private BookEntity createBook(String title, List<String> authors, LocalDate date,
|
||||
private BookEntity createBook(String title, String subtitle, List<String> authors, LocalDate date,
|
||||
String series, Float seriesNum, String lang,
|
||||
String publisher, String isbn13, String isbn10,
|
||||
String fileName) {
|
||||
@@ -28,6 +28,7 @@ class PathPatternResolverTest {
|
||||
when(book.getFileName()).thenReturn(fileName);
|
||||
|
||||
when(metadata.getTitle()).thenReturn(title);
|
||||
when(metadata.getSubtitle()).thenReturn(subtitle);
|
||||
|
||||
if (authors == null) {
|
||||
when(metadata.getAuthors()).thenReturn(null);
|
||||
@@ -51,6 +52,14 @@ class PathPatternResolverTest {
|
||||
return book;
|
||||
}
|
||||
|
||||
// Helper method for backward compatibility
|
||||
private BookEntity createBook(String title, List<String> authors, LocalDate date,
|
||||
String series, Float seriesNum, String lang,
|
||||
String publisher, String isbn13, String isbn10,
|
||||
String fileName) {
|
||||
return createBook(title, null, authors, date, series, seriesNum, lang, publisher, isbn13, isbn10, fileName);
|
||||
}
|
||||
|
||||
@Test void emptyPattern_returnsOnlyExtension() {
|
||||
var book = createBook("Title", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.pdf");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "")).isEqualTo("file.pdf");
|
||||
@@ -210,4 +219,58 @@ class PathPatternResolverTest {
|
||||
String pattern = "{title}.{extension}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("X.mobi");
|
||||
}
|
||||
|
||||
@Test void subtitleInPattern_replacedCorrectly() {
|
||||
var book = createBook("Main Title", "The Subtitle", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title} - {subtitle}")).isEqualTo("Main Title - The Subtitle.epub");
|
||||
}
|
||||
|
||||
@Test void subtitleEmpty_replacedWithEmpty() {
|
||||
var book = createBook("Title", "", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title} - {subtitle}")).isEqualTo("Title - .epub");
|
||||
}
|
||||
|
||||
@Test void subtitleNull_replacedWithEmpty() {
|
||||
var book = createBook("Title", null, List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title} - {subtitle}")).isEqualTo("Title - .epub");
|
||||
}
|
||||
|
||||
@Test void subtitleInOptionalBlock_withValue_blockIncluded() {
|
||||
var book = createBook("Title", "Subtitle", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title}< - {subtitle}>")).isEqualTo("Title - Subtitle.epub");
|
||||
}
|
||||
|
||||
@Test void subtitleInOptionalBlock_withoutValue_blockRemoved() {
|
||||
var book = createBook("Title", null, List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title}< - {subtitle}>")).isEqualTo("Title.epub");
|
||||
}
|
||||
|
||||
@Test void subtitleWithIllegalChars_sanitized() {
|
||||
var book = createBook("Title", "Sub:title<>|*?", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
String result = PathPatternResolver.resolvePattern(book, "{title} - {subtitle}");
|
||||
assertThat(result).doesNotContain(":", "<", ">", "|", "*", "?")
|
||||
.contains("Title").contains("Subtitle");
|
||||
}
|
||||
|
||||
@Test void subtitleWithWhitespace_trimmedAndSanitized() {
|
||||
var book = createBook("Title", " Sub title ", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title} - {subtitle}")).isEqualTo("Title - Sub title.epub");
|
||||
}
|
||||
|
||||
@Test void complexPatternWithSubtitle_allPlaceholdersPresent() {
|
||||
var book = createBook("Main Title", "The Great Subtitle", List.of("Author One"), LocalDate.of(2010, 5, 5),
|
||||
"Series", 1f, "English", "Publisher", "ISBN13", "ISBN10", "complex.epub");
|
||||
String pattern = "<{series}/>{title}< - {subtitle}> - {authors} - {year}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern))
|
||||
.isEqualTo("Series/Main Title - The Great Subtitle - Author One - 2010.epub");
|
||||
}
|
||||
|
||||
@Test void optionalBlockWithTitleAndSubtitle_partialValues() {
|
||||
var book1 = createBook("Title", "Subtitle", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
var book2 = createBook("Title", null, List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.epub");
|
||||
String pattern = "<{title} - {subtitle}>";
|
||||
|
||||
assertThat(PathPatternResolver.resolvePattern(book1, pattern)).isEqualTo("Title - Subtitle.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book2, pattern)).isEqualTo("file.epub");
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,7 @@
|
||||
<h4 class="subsection-title">Available Placeholders</h4>
|
||||
<ul>
|
||||
<li><code class="placeholder-code">{{ '{title}' }}</code> – Book title</li>
|
||||
<li><code class="placeholder-code">{{ '{subtitle}' }}</code> – Book subtitle</li>
|
||||
<li><code class="placeholder-code">{{ '{authors}' }}</code> – Author(s)</li>
|
||||
<li><code class="placeholder-code">{{ '{year}' }}</code> – Full year (e.g. 2025)</li>
|
||||
<li><code class="placeholder-code">{{ '{series}' }}</code> – Series name</li>
|
||||
@@ -180,6 +181,7 @@
|
||||
<h4 class="subsection-title">Examples with Full Metadata</h4>
|
||||
<div class="metadata-sample">
|
||||
<span>title: <code>Harry Potter and the Sorcerer's Stone</code></span>
|
||||
<span>subtitle: <code>The Boy Who Lived</code></span>
|
||||
<span>authors: <code>J.K. Rowling</code></span>
|
||||
<span>series: <code>Harry Potter</code></span>
|
||||
<span>seriesIndex: <code>01</code></span>
|
||||
@@ -217,6 +219,11 @@
|
||||
<p class="example-pattern"><strong>Reuse original filename in path:</strong> <code>{{ '{authors}/{series}/{currentFilename}' }}</code></p>
|
||||
<p class="example-output"><strong>Output:</strong> <code>J.K. Rowling/Harry Potter/harry1_original.epub</code></p>
|
||||
</div>
|
||||
|
||||
<div class="example-item">
|
||||
<p class="example-pattern"><strong>Title + Subtitle:</strong> <code>{{ '{title}: {subtitle}' }}</code></p>
|
||||
<p class="example-output"><strong>Output:</strong> <code>Harry Potter and the Sorcerer's Stone: The Boy Who Lived.epub</code></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -226,6 +233,7 @@
|
||||
<h4 class="subsection-title">Examples with Missing Optional Fields</h4>
|
||||
<div class="metadata-sample">
|
||||
<span>title: <code>Project Hail Mary</code></span>
|
||||
<span>subtitle: <code>(not provided)</code></span>
|
||||
<span>authors: <code>Andy Weir</code></span>
|
||||
<span>year: <code>2021</code></span>
|
||||
<span>series: <code>(not provided)</code></span>
|
||||
@@ -258,6 +266,11 @@
|
||||
<p class="example-pattern"><strong>Use original filename with year suffix:</strong> <code>{{ '{authors}/{year}__{currentFilename}' }}</code></p>
|
||||
<p class="example-output"><strong>Output:</strong> <code>Andy Weir/2021__project_hail_mary_final.epub</code></p>
|
||||
</div>
|
||||
|
||||
<div class="example-item">
|
||||
<p class="example-pattern"><strong>Title + Subtitle fallback:</strong> <code>{{ '{title}<: {subtitle}>' }}</code></p>
|
||||
<p class="example-output"><strong>Output:</strong> <code>Project Hail Mary.epub</code></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -20,14 +20,15 @@ import {Divider} from 'primeng/divider';
|
||||
})
|
||||
export class FileNamingPatternComponent implements OnInit {
|
||||
readonly exampleMetadata: Record<string, string> = {
|
||||
title: 'The Fellowship of the Ring',
|
||||
authors: 'J.R.R. Tolkien',
|
||||
year: '1954',
|
||||
series: 'The Lord of the Rings',
|
||||
title: "Harry Potter and the Sorcerer's Stone",
|
||||
subtitle: 'The Boy Who Lived',
|
||||
authors: 'J.K. Rowling',
|
||||
year: '1997',
|
||||
series: 'Harry Potter',
|
||||
seriesIndex: '01',
|
||||
language: 'en',
|
||||
publisher: 'Allen & Unwin',
|
||||
isbn: '9780618574940',
|
||||
publisher: 'Bloomsbury',
|
||||
isbn: '9780747532699',
|
||||
};
|
||||
|
||||
defaultPattern = '';
|
||||
|
||||
Reference in New Issue
Block a user