-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add "Convert to BibTeX format" cleanup (#3541)
* Add "Convert to BibTeX format" cleanup * Add round-trip tests * Fix tests * Make conversations modal
- Loading branch information
1 parent
731a743
commit beb6ee0
Showing
17 changed files
with
263 additions
and
71 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
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
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
52 changes: 52 additions & 0 deletions
52
src/main/java/org/jabref/logic/cleanup/ConvertToBibtexCleanup.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,52 @@ | ||
package org.jabref.logic.cleanup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jabref.model.FieldChange; | ||
import org.jabref.model.cleanup.CleanupJob; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.EntryConverter; | ||
import org.jabref.model.entry.FieldName; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
/** | ||
* Converts the entry to biblatex format. | ||
*/ | ||
public class ConvertToBibtexCleanup implements CleanupJob { | ||
|
||
@Override | ||
public List<FieldChange> cleanup(BibEntry entry) { | ||
List<FieldChange> changes = new ArrayList<>(); | ||
|
||
// Dates: get date and fill year and month | ||
// If there already exists a non blank/empty value for the field, then it is not overwritten | ||
entry.getPublicationDate().ifPresent(date -> { | ||
if (StringUtil.isBlank(entry.getField(FieldName.YEAR))) { | ||
date.getYear().flatMap(year -> entry.setField(FieldName.YEAR, year.toString())).ifPresent(changes::add); | ||
} | ||
|
||
if (StringUtil.isBlank(entry.getField(FieldName.MONTH))) { | ||
date.getMonth().flatMap(month -> entry.setField(FieldName.MONTH, month.getJabRefFormat())).ifPresent(changes::add); | ||
} | ||
|
||
if (changes.size() > 0) { | ||
entry.clearField(FieldName.DATE).ifPresent(changes::add); | ||
} | ||
}); | ||
|
||
for (Map.Entry<String, String> alias : EntryConverter.FIELD_ALIASES_TEX_TO_LTX.entrySet()) { | ||
String oldFieldName = alias.getValue(); | ||
String newFieldName = alias.getKey(); | ||
entry.getField(oldFieldName).ifPresent(oldValue -> { | ||
if (!oldValue.isEmpty() && (!entry.getField(newFieldName).isPresent())) { | ||
// There is content in the old field and no value in the new, so just copy | ||
entry.setField(newFieldName, oldValue).ifPresent(changes::add); | ||
entry.clearField(oldFieldName).ifPresent(changes::add); | ||
} | ||
}); | ||
} | ||
return changes; | ||
} | ||
} |
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
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
62 changes: 62 additions & 0 deletions
62
src/test/java/org/jabref/logic/cleanup/BibtexBiblatexRoundtripTest.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,62 @@ | ||
package org.jabref.logic.cleanup; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class BibtexBiblatexRoundtripTest { | ||
private BibEntry bibtex; | ||
private BibEntry biblatex; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
bibtex = new BibEntry("article"); | ||
bibtex.setField("author", "Frame, J. S. and Robinson, G. de B. and Thrall, R. M."); | ||
bibtex.setField("title", "The hook graphs of the symmetric groups"); | ||
bibtex.setField("journal", "Canadian J. Math."); | ||
bibtex.setField("fjournal", "Canadian Journal of Mathematics. Journal Canadien de Math\\'ematiques"); | ||
bibtex.setField("volume", "6"); | ||
bibtex.setField("year", "1954"); | ||
bibtex.setField("pages", "316--324"); | ||
bibtex.setField("issn", "0008-414X"); | ||
bibtex.setField("mrclass", "20.0X"); | ||
bibtex.setField("mrnumber", "0062127"); | ||
bibtex.setField("mrreviewer", "D. E. Littlewood"); | ||
|
||
biblatex = new BibEntry("article"); | ||
biblatex.setField("author", "Frame, J. S. and Robinson, G. de B. and Thrall, R. M."); | ||
biblatex.setField("title", "The hook graphs of the symmetric groups"); | ||
biblatex.setField("journaltitle", "Canadian J. Math."); | ||
biblatex.setField("fjournal", "Canadian Journal of Mathematics. Journal Canadien de Math\\'ematiques"); | ||
biblatex.setField("volume", "6"); | ||
biblatex.setField("date", "1954"); | ||
biblatex.setField("pages", "316--324"); | ||
biblatex.setField("issn", "0008-414X"); | ||
biblatex.setField("mrclass", "20.0X"); | ||
biblatex.setField("mrnumber", "0062127"); | ||
biblatex.setField("mrreviewer", "D. E. Littlewood"); | ||
} | ||
|
||
@Test | ||
void roundTripBibtexToBiblatexIsIdentity() { | ||
BibEntry clone = (BibEntry) bibtex.clone(); | ||
new ConvertToBiblatexCleanup().cleanup(clone); | ||
assertEquals(biblatex, clone); | ||
new ConvertToBibtexCleanup().cleanup(clone); | ||
|
||
assertEquals(bibtex, clone); | ||
} | ||
|
||
@Test | ||
void roundTripBiblatexToBibtexIsIdentity() { | ||
BibEntry clone = (BibEntry) biblatex.clone(); | ||
new ConvertToBibtexCleanup().cleanup(clone); | ||
assertEquals(bibtex, clone); | ||
new ConvertToBiblatexCleanup().cleanup(clone); | ||
|
||
assertEquals(biblatex, clone); | ||
} | ||
} |
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.