From 57112f2238841da40821246f21bf488a8d669013 Mon Sep 17 00:00:00 2001 From: Wanling Fu Date: Sun, 23 Mar 2025 01:40:53 +0000 Subject: [PATCH 1/5] Fix #12188: Remove LaTeX commands from generated filenames This fixes an issue where LaTeX commands (e.g., \mkbibquote{...}) were retained in generated filenames using the [fulltitle] pattern. - Added RemoveLatexCommandsFormatter to createFileNameFromPattern - Added test case to FileUtilTest to validate filename sanitization --- src/main/java/org/jabref/logic/util/io/FileUtil.java | 6 +++++- .../java/org/jabref/logic/util/io/FileUtilTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index 5ddb734d8ce..7e9c9911014 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -24,6 +24,7 @@ import org.jabref.logic.FilePreferences; import org.jabref.logic.citationkeypattern.BracketedPattern; +import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter; import org.jabref.logic.util.StandardFileType; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; @@ -329,9 +330,12 @@ public static String createFileNameFromPattern(BibDatabase database, BibEntry en if (targetName.isEmpty()) { targetName = entry.getCitationKey().orElse("default"); } - + // Remove LaTeX commands (e.g., \mkbibquote{}) from expanded fields before cleaning filename + // See: https://github.com/JabRef/jabref/issues/12188 + targetName = new RemoveLatexCommandsFormatter().format(targetName); // Removes illegal characters from filename targetName = FileNameCleaner.cleanFileName(targetName); + return targetName; } diff --git a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java index e09b5e89b8e..2c3914f1c40 100644 --- a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java @@ -152,6 +152,17 @@ void getLinkedFileNameByYearAuthorFirstpage() { assertEquals("1868_Kitsune_567", FileUtil.createFileNameFromPattern(null, entry, fileNamePattern)); } + @Test + void getLinkedFileNameRemovesLatexCommands() { + String pattern = "[citationkey] - [fulltitle]"; + BibEntry entry = new BibEntry(); + entry.setCitationKey("BrayBuildingCommunity"); + entry.setField(StandardField.TITLE, "Building \\mkbibquote{Community}"); + + String result = FileUtil.createFileNameFromPattern(null, entry, pattern); + assertEquals("BrayBuildingCommunity - Building Community", result); + } + @Test void getFileExtensionSimpleFile() { assertEquals("pdf", FileUtil.getFileExtension(Path.of("test.pdf")).get()); From d3e99a590c3374acc4c9f201608a9e23f7e1f93b Mon Sep 17 00:00:00 2001 From: Wanling Fu Date: Sun, 23 Mar 2025 02:06:49 +0000 Subject: [PATCH 2/5] Change in described --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34a89efc32a..19e077d7b89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue where an exception would occur when running abbreviate journals for multiple entries. [#12634](https://github.com/JabRef/jabref/issues/12634) - We fixed an issue where JabRef displayed dropdown triangle in wrong place in "Search for unlinked local files" dialog [#12713](https://github.com/JabRef/jabref/issues/12713) - We fixed an issue where JabRef would not open if an invalid external journal abbreviation path was encountered. [#12776](https://github.com/JabRef/jabref/issues/12776) +- We fixed a bug where LaTeX commands (e.g., \mkbibquote{}) were not removed from filenames generated using the `[fulltitle]` pattern. [#12188](https://github.com/JabRef/jabref/issues/12188) + ### Removed From 7330a1e9b2f4ccb749aed16046addda73cb630bb Mon Sep 17 00:00:00 2001 From: Wanling Fu Date: Sun, 23 Mar 2025 02:12:51 +0000 Subject: [PATCH 3/5] Change in CHANGELOG.md: describe LaTeX filename cleanup (Closes #12188) --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19e077d7b89..c48c2c23051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,7 +85,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue where an exception would occur when running abbreviate journals for multiple entries. [#12634](https://github.com/JabRef/jabref/issues/12634) - We fixed an issue where JabRef displayed dropdown triangle in wrong place in "Search for unlinked local files" dialog [#12713](https://github.com/JabRef/jabref/issues/12713) - We fixed an issue where JabRef would not open if an invalid external journal abbreviation path was encountered. [#12776](https://github.com/JabRef/jabref/issues/12776) -- We fixed a bug where LaTeX commands (e.g., \mkbibquote{}) were not removed from filenames generated using the `[fulltitle]` pattern. [#12188](https://github.com/JabRef/jabref/issues/12188) +- We fixed a bug where LaTeX commands were not removed from filenames generated using the `[bibtexkey] - [fulltitle]` pattern. [#12188](https://github.com/JabRef/jabref/issues/12188) ### Removed From 7536793d53900b60f05e1d1d52868dabcb8ecfa6 Mon Sep 17 00:00:00 2001 From: Wanling Fu Date: Sun, 23 Mar 2025 13:12:33 +0000 Subject: [PATCH 4/5] Fix test case style and markdown format in CHANGELOG --- CHANGELOG.md | 1 - src/test/java/org/jabref/logic/util/io/FileUtilTest.java | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c48c2c23051..76b8f300a88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,7 +87,6 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue where JabRef would not open if an invalid external journal abbreviation path was encountered. [#12776](https://github.com/JabRef/jabref/issues/12776) - We fixed a bug where LaTeX commands were not removed from filenames generated using the `[bibtexkey] - [fulltitle]` pattern. [#12188](https://github.com/JabRef/jabref/issues/12188) - ### Removed - "Web of Science" [journal abbreviation list](https://docs.jabref.org/advanced/journalabbreviations) was removed. [abbrv.jabref.org#176](https://github.com/JabRef/abbrv.jabref.org/issues/176) diff --git a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java index 2c3914f1c40..50048bb3166 100644 --- a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java @@ -158,9 +158,9 @@ void getLinkedFileNameRemovesLatexCommands() { BibEntry entry = new BibEntry(); entry.setCitationKey("BrayBuildingCommunity"); entry.setField(StandardField.TITLE, "Building \\mkbibquote{Community}"); - + String expected = "BrayBuildingCommunity - Building Community"; String result = FileUtil.createFileNameFromPattern(null, entry, pattern); - assertEquals("BrayBuildingCommunity - Building Community", result); + assertEquals(expected, result); } @Test From 17a2fe3df39f87f21ec749fcd463bf90cdcf22a2 Mon Sep 17 00:00:00 2001 From: Wanling Fu Date: Mon, 24 Mar 2025 15:17:06 +0000 Subject: [PATCH 5/5] Refactor: use class constant and method chaining --- src/main/java/org/jabref/logic/util/io/FileUtil.java | 4 +++- src/test/java/org/jabref/logic/util/io/FileUtilTest.java | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index 7e9c9911014..1e68c3ff2f9 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -48,6 +48,7 @@ public class FileUtil { private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class); private static final String ELLIPSIS = "..."; private static final int ELLIPSIS_LENGTH = ELLIPSIS.length(); + private static final RemoveLatexCommandsFormatter REMOVE_LATEX_COMMANDS_FORMATTER = new RemoveLatexCommandsFormatter(); /** * MUST ALWAYS BE A SORTED ARRAY because it is used in a binary search @@ -330,9 +331,10 @@ public static String createFileNameFromPattern(BibDatabase database, BibEntry en if (targetName.isEmpty()) { targetName = entry.getCitationKey().orElse("default"); } + // Remove LaTeX commands (e.g., \mkbibquote{}) from expanded fields before cleaning filename // See: https://github.com/JabRef/jabref/issues/12188 - targetName = new RemoveLatexCommandsFormatter().format(targetName); + targetName = REMOVE_LATEX_COMMANDS_FORMATTER.format(targetName); // Removes illegal characters from filename targetName = FileNameCleaner.cleanFileName(targetName); diff --git a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java index 50048bb3166..124587c2837 100644 --- a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java @@ -155,9 +155,9 @@ void getLinkedFileNameByYearAuthorFirstpage() { @Test void getLinkedFileNameRemovesLatexCommands() { String pattern = "[citationkey] - [fulltitle]"; - BibEntry entry = new BibEntry(); - entry.setCitationKey("BrayBuildingCommunity"); - entry.setField(StandardField.TITLE, "Building \\mkbibquote{Community}"); + BibEntry entry = new BibEntry() + .withCitationKey("BrayBuildingCommunity") + .withField(StandardField.TITLE, "Building \\mkbibquote{Community}"); String expected = "BrayBuildingCommunity - Building Community"; String result = FileUtil.createFileNameFromPattern(null, entry, pattern); assertEquals(expected, result);