-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
31 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 f19675b
Merge remote-tracking branch 'origin/main' into add-doi-to-bibtex-exa…
koppor d2aca73
Continue on wrong DOI
koppor 6ebc93c
Merge remote-tracking branch 'origin/main' into add-doi-to-bibtex-exa…
koppor b68c6e0
Apply suggestions from code review
koppor 05c4a8b
Fix checkstyle
koppor 7ebb00b
Merge branch 'add-doi-to-bibtex-example' of github.com:JabRef/jabref …
koppor 1f88fb3
Fix space
koppor 19e0ded
Disable JUL output at JabKit
koppor 8b14408
--porcelain does not output any logs to the console any more
koppor fd0b7ef
Fix logger config
koppor 067af4f
Have --porcelain working
koppor 43b4b87
Improve strings
koppor b439fdf
Update CHANGELOG.md
subhramit bea7fc5
Update jabkit/src/main/java/org/jabref/cli/DoiToBibtex.java
subhramit 8faed65
Merge branch 'main' into add-doi-to-bibtex-example
koppor d4b287d
Merge branch 'main' into add-doi-to-bibtex-example
subhramit fd63a0d
Merge branch 'main' into add-doi-to-bibtex-example
koppor 06b5c48
Workaround for tests
koppor 1644f8b
Compilefix
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
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,81 @@ | ||
| 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.logic.l10n.Localization; | ||
| 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; | ||
|
|
||
| @CommandLine.Mixin | ||
| private ArgumentProcessor.SharedOptions sharedOptions = new ArgumentProcessor.SharedOptions(); | ||
|
|
||
| @Parameters(paramLabel = "DOI", description = "one or more DOIs to fetch", arity = "1..*") | ||
| private String[] dois; | ||
|
|
||
| @Override | ||
| public Integer call() { | ||
| CrossRef fetcher = new CrossRef(); | ||
| List<BibEntry> entries = new ArrayList<>(dois.length); | ||
|
|
||
| for (String doiString : dois) { | ||
| Optional<DOI> doiParsed = DOI.parse(doiString); | ||
| if (doiParsed.isEmpty()) { | ||
| LOGGER.warn("Skipped DOI {}, because it is not a valid DOI string", doiString); | ||
| System.out.println(Localization.lang("DOI %0 is invalid", doiString)); | ||
| continue; | ||
| } | ||
| Optional<BibEntry> entry; | ||
| try { | ||
| entry = fetcher.performSearchById(doiParsed.get().asString()); | ||
| } catch (FetcherException e) { | ||
| LOGGER.error("Could not fetch BibTeX based on DOI", e); | ||
| System.err.println(Localization.lang("Error")); | ||
| continue; | ||
| } | ||
|
|
||
| if (entry.isEmpty()) { | ||
| LOGGER.error("Could not fetch BibTeX based on DOI"); | ||
subhramit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| System.err.println(Localization.lang("Error")); | ||
| continue; | ||
| } | ||
|
|
||
| entries.add(entry.get()); | ||
| } | ||
|
|
||
| try (OutputStreamWriter writer = new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) { | ||
| BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(entries)); | ||
| BibDatabaseWriter bibWriter = new BibDatabaseWriter(writer, context, argumentProcessor.cliPreferences); | ||
| bibWriter.writeDatabase(context); | ||
| } catch (IOException e) { | ||
| LOGGER.error("Could not write BibTeX", e); | ||
| System.err.println(Localization.lang("Unable to write to %0.", "stdout")); | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.