-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add doi-to-bibtex to examples and JabKit #14244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koppor
wants to merge
12
commits into
main
Choose a base branch
from
add-doi-to-bibtex-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−6
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dafaeba
Simplyfy code
koppor 5e85c5d
Fix step ignore
koppor 5b1ffaf
Add doi_to_bibtex.java
koppor 3f66895
Add doi-to-bibtex to JabKit
koppor 242436d
Add CHANGELOG.md entry
koppor 5ed9435
Add some debug code (again)
koppor f765241
Fix formatting
koppor c803beb
Fix logger
koppor df625ab
Also deal with jabkit
koppor d3dcc6b
Remove debug
koppor 3455a03
Fix casing
koppor 7ea1a65
Fix condition
koppor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||||||||||||||
| package org.jabref.cli; | ||||||||||||||
|
|
||||||||||||||
| import java.io.IOException; | ||||||||||||||
| import java.io.OutputStreamWriter; | ||||||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||||||
| import java.util.ArrayList; | ||||||||||||||
| import java.util.List; | ||||||||||||||
| import java.util.Optional; | ||||||||||||||
| import java.util.concurrent.Callable; | ||||||||||||||
|
|
||||||||||||||
| import org.jabref.logic.exporter.BibDatabaseWriter; | ||||||||||||||
| import org.jabref.logic.importer.FetcherException; | ||||||||||||||
| import org.jabref.logic.importer.fetcher.CrossRef; | ||||||||||||||
| import org.jabref.model.database.BibDatabase; | ||||||||||||||
| import org.jabref.model.database.BibDatabaseContext; | ||||||||||||||
| import org.jabref.model.entry.BibEntry; | ||||||||||||||
| import org.jabref.model.entry.identifier.DOI; | ||||||||||||||
|
|
||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||
| import picocli.CommandLine; | ||||||||||||||
| import picocli.CommandLine.Command; | ||||||||||||||
| import picocli.CommandLine.Parameters; | ||||||||||||||
|
|
||||||||||||||
| @Command(name = "doi-to-bibtex", description = "Converts a DOI to BibTeX") | ||||||||||||||
| public class DoiToBibtex implements Callable<Integer> { | ||||||||||||||
|
|
||||||||||||||
| private static final Logger LOGGER = LoggerFactory.getLogger(DoiToBibtex.class); | ||||||||||||||
|
|
||||||||||||||
| @CommandLine.ParentCommand | ||||||||||||||
| private ArgumentProcessor argumentProcessor; | ||||||||||||||
|
|
||||||||||||||
| @Parameters(paramLabel = "DOI", description = "one or more DOIs to fetch", arity = "1..*") | ||||||||||||||
| private DOI[] dois; | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| public Integer call() { | ||||||||||||||
| var fetcher = new CrossRef(); | ||||||||||||||
| List<BibEntry> entries = new ArrayList<>(dois.length); | ||||||||||||||
|
|
||||||||||||||
| for (DOI doi : dois) { | ||||||||||||||
| Optional<BibEntry> entry; | ||||||||||||||
| try { | ||||||||||||||
| entry = fetcher.performSearchById(doi.asString()); | ||||||||||||||
| } catch (FetcherException e) { | ||||||||||||||
| LOGGER.error("Could not fetch DOI from BibTeX", e); | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (entry.isEmpty()) { | ||||||||||||||
| LOGGER.error("Could not fetch DOI from BibTeX"); | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| entries.add(entry.get()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| try (var writer = new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) { | ||||||||||||||
| var context = new BibDatabaseContext(new BibDatabase(entries)); | ||||||||||||||
| var bibWriter = new BibDatabaseWriter(writer, context, argumentProcessor.cliPreferences); | ||||||||||||||
|
Comment on lines
+58
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| bibWriter.writeDatabase(context); | ||||||||||||||
| } catch (IOException e) { | ||||||||||||||
| LOGGER.error("Could not write BibTeX", e); | ||||||||||||||
| return 1; | ||||||||||||||
| } | ||||||||||||||
| return 0; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
This file contains hidden or 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,34 @@ | ||
| ///usr/bin/env jbang "$0" "$@" ; exit $? | ||
|
|
||
| import org.jabref.logic.exporter.BibWriter; | ||
| import org.jabref.logic.exporter.BibDatabaseWriter; | ||
| import org.jabref.logic.importer.fetcher.CrossRef; | ||
| import org.jabref.logic.preferences.JabRefCliPreferences; | ||
| import org.jabref.model.database.BibDatabase; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
|
|
||
| import org.tinylog.Logger; | ||
|
|
||
| //DESCRIPTION Converts a DOI to BibTeX | ||
|
|
||
| //JAVA 25+ | ||
| //RUNTIME_OPTIONS --enable-native-access=ALL-UNNAMED | ||
| //FILES tinylog.properties=tinylog.properties | ||
|
|
||
| //DEPS org.jabref:jablib:6.0-SNAPSHOT | ||
| //REPOS mavencentral,mavencentralsnapshots=https://central.sonatype.com/repository/maven-snapshots/,s01oss=https://s01.oss.sonatype.org/content/repositories/snapshots/,oss=https://oss.sonatype.org/content/repositories,jitpack=https://jitpack.io,oss2=https://oss.sonatype.org/content/groups/public,ossrh=https://oss.sonatype.org/content/repositories/snapshots,raw=https://raw.githubusercontent.com/JabRef/jabref/refs/heads/main/jablib/lib/ | ||
|
|
||
| void main() throws Exception { | ||
| var preferences = JabRefCliPreferences.getInstance(); | ||
|
|
||
| // All `IdParserFetcher<DOI>` can do. In JabRef, there is currently only one implemented | ||
|
|
||
| var fetcher = new CrossRef(); | ||
| var entry = fetcher.performSearchById("10.47397/tb/44-3/tb138kopp-jabref").get(); // will throw an exception if not found | ||
|
|
||
| try (var writer = new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) { | ||
| var context = new BibDatabaseContext(new BibDatabase(List.of(entry))); | ||
| var bibWriter = new BibDatabaseWriter(writer, context, preferences); | ||
| bibWriter.writeDatabase(context); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.