forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a rule to use when renaming pages in a comic file [comixed#516]
* Added a migration to insert a default page renaming rule. * Added a input for configuring the page renaming rule. * Added an adaptor for generating page names based on renaming rules. Also removed the explicit page renaming flag when converting comics. Instead, this flag is implicitly set if a page renaming rule is defined.
- Loading branch information
Showing
24 changed files
with
408 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
comixed-adaptors/src/main/java/org/comixedproject/adaptors/comicbooks/ComicPageAdaptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2021, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.adaptors.comicbooks; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.codehaus.plexus.util.FileUtils; | ||
import org.comixedproject.model.comicpages.Page; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* <code>ComicPageAdaptor</code> provides a set of utility methods for naming comic pages. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
@Component | ||
@Log4j2 | ||
public class ComicPageAdaptor { | ||
private static final String FORBIDDEN_RULE_CHARACTERS = "[\"':\\\\*?|<>]"; | ||
|
||
/** | ||
* Generates a new filename for the given page. | ||
* | ||
* @param page the page | ||
* @param renamingRule the renaming rule | ||
* @param pageIndex the page index in the comic | ||
* @return the page name | ||
*/ | ||
public String createFilenameFromRule( | ||
final Page page, final String renamingRule, final int pageIndex) { | ||
log.debug("Scrubbing renaming rule: {}", renamingRule); | ||
final String rule = this.scrub(renamingRule, FORBIDDEN_RULE_CHARACTERS); | ||
|
||
log.debug("Generating relative filename based on renaming rule: {}", rule); | ||
final String index = String.valueOf(pageIndex + 1); | ||
|
||
String result = rule.replace("$INDEX", index); | ||
|
||
log.debug("Relative page name: {}", result); | ||
|
||
return String.format("%s.%s", result, FileUtils.getExtension(page.getFilename())); | ||
} | ||
|
||
private String scrub(final String text, final String forbidden) { | ||
log.trace("Pre-sanitized text: {}", text); | ||
return text.replaceAll(forbidden, "_"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...d-adaptors/src/test/java/org/comixedproject/adaptors/comicbooks/ComicPageAdaptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2021, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.adaptors.comicbooks; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import org.comixedproject.model.comicpages.Page; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ComicPageAdaptorTest { | ||
private static final String TEST_RENAMING_RULE = "page-$INDEX"; | ||
private static final int TEST_PAGE_INDEX = 3; | ||
private static final String TEST_FILENAME = "oldname3.png"; | ||
private static final String TEST_EXPECTED_PAGE_NAME = "page-4.png"; | ||
|
||
@InjectMocks private ComicPageAdaptor adaptor; | ||
@Mock private Page page; | ||
|
||
@Before | ||
public void setUp() { | ||
Mockito.when(page.getFilename()).thenReturn(TEST_FILENAME); | ||
} | ||
|
||
@Test | ||
public void testCreateFilenameFromRule() { | ||
final String result = adaptor.createFilenameFromRule(page, TEST_RENAMING_RULE, TEST_PAGE_INDEX); | ||
|
||
assertNotNull(result); | ||
assertEquals(TEST_EXPECTED_PAGE_NAME, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.