44import java .nio .file .Files ;
55import java .nio .file .Path ;
66import java .util .List ;
7+ import java .util .regex .Pattern ;
78
89import org .jabref .logic .cleanup .ConvertToBibtexCleanup ;
910import org .jabref .logic .formatter .bibtexfields .RemoveNewlinesFormatter ;
@@ -23,6 +24,11 @@ public final class BstPreviewLayout implements PreviewLayout {
2324
2425 private static final Logger LOGGER = LoggerFactory .getLogger (BstPreviewLayout .class );
2526
27+ private static final Pattern COMMENT_PATTERN = Pattern .compile ("%.*" );
28+ private static final Pattern BIBITEM_PATTERN = Pattern .compile ("\\ \\ bibitem[{].*[}]" );
29+ private static final Pattern LATEX_COMMAND_PATTERN = Pattern .compile ("(?m)^\\ \\ .*$" );
30+ private static final Pattern MULTIPLE_SPACES_PATTERN = Pattern .compile (" +" );
31+
2632 private final String name ;
2733 private String source ;
2834 private BstVM bstVM ;
@@ -55,23 +61,23 @@ public String generatePreview(BibEntry originalEntry, BibDatabaseContext databas
5561 if (error != null ) {
5662 return error ;
5763 }
58- // ensure that the entry is of BibTeX format (and do not modify the original entry)
64+ // Ensure that the entry is of BibTeX format (and do not modify the original entry)
5965 BibEntry entry = new BibEntry (originalEntry );
6066 new ConvertToBibtexCleanup ().cleanup (entry );
6167 String result = bstVM .render (List .of (entry ));
6268 // Remove all comments
63- result = result .replaceAll ("%.*" , "" );
69+ result = COMMENT_PATTERN . matcher ( result ) .replaceAll ("" );
6470 // Remove all LaTeX comments
6571 // The RemoveLatexCommandsFormatter keeps the words inside latex environments. Therefore, we remove them manually
6672 result = result .replace ("\\ begin{thebibliography}{1}" , "" );
6773 result = result .replace ("\\ end{thebibliography}" , "" );
6874 // The RemoveLatexCommandsFormatter keeps the word inside the latex command, but we want to remove that completely
69- result = result .replaceAll (" \\ \\ bibitem[{].*[}]" , "" );
75+ result = BIBITEM_PATTERN . matcher ( result ) .replaceAll ("" );
7076 // We want to replace \newblock by a space instead of completely removing it
7177 result = result .replace ("\\ newblock" , " " );
72- // remove all latex commands statements - assumption: command in a separate line
73- result = result . replaceAll ( "(?m)^ \\ \\ .*$" , "" );
74- // remove some IEEEtran.bst output (resulting from a multiline \providecommand)
78+ // Remove all latex commands statements - assumption: command in a separate line
79+ result = LATEX_COMMAND_PATTERN . matcher ( result ). replaceAll ( "" );
80+ // Remove some IEEEtran.bst output (resulting from a multiline \providecommand)
7581 result = result .replace ("#2}}" , "" );
7682 // Have quotes right - and more
7783 result = new LatexToUnicodeFormatter ().format (result );
@@ -81,7 +87,7 @@ public String generatePreview(BibEntry originalEntry, BibDatabaseContext databas
8187 result = new RemoveNewlinesFormatter ().format (result );
8288 result = new RemoveLatexCommandsFormatter ().format (result );
8389 result = new RemoveTilde ().format (result );
84- result = result .trim ().replaceAll (" +" , " " );
90+ result = MULTIPLE_SPACES_PATTERN . matcher ( result .trim ()) .replaceAll (" " );
8591 return result ;
8692 }
8793
0 commit comments