Merge 8b4e8f59bb11941e70c1d04f067067eefd8fd11e into d8a8970715070f73bcb6333e3fe851b6802ac702

This commit is contained in:
Emre AKYÜZ 2023-08-08 01:14:39 +03:00 committed by GitHub
commit a4aaa1f9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,82 @@
#!/bin/sh #!/bin/bash
[ -z "$1" ] && echo "Give either a pdf file or a DOI as an argument." && exit
if [ -f "$1" ]; then BIB_FILE="$HOME/latex/uni.bib"
# Try to get DOI from pdfinfo or pdftotext output. CORRECTION_METHOD="sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/doi:\6/p; T; q'"
doi=$(pdfinfo "$1" | grep -io "doi:.*") ||
doi=$(pdftotext "$1" 2>/dev/null - | sed -n '/[dD][oO][iI]:/{s/.*[dD][oO][iI]:\s*\(\S\+[[:alnum:]]\).*/\1/p;q}') || function get_doi_from_pdf {
exit 1 local pdf="$1"
else local doi
doi="$1" doi=$(pdfinfo "$pdf" 2>/dev/null | eval "$CORRECTION_METHOD")
if [ -z "$doi" ]; then
doi=$(pdftotext -q -l 1 "$pdf" - 2>/dev/null | eval "$CORRECTION_METHOD")
fi
echo "$doi"
}
function normalize_doi {
local doi="$1"
echo "${doi,,}"
}
function process_doi {
local doi="$1"
local bibtex_entry
bibtex_entry=$(curl -s "https://api.crossref.org/works/$doi/transform/application/x-bibtex" -w "\\n" | sed '/^@[a-z]\+{[^[:space:]]\+[0-9]\{4\},/{
s/\([A-Z]\)/\L\1/g
s/_//g
s/[0-9]*\([0-9]\{2\}\)/\1/g
}')
red_color='\033[0;31m'
reset_color='\033[0m'
echo -e "${red_color}$bibtex_entry${reset_color}"
if [[ -z "$bibtex_entry" || ! "$bibtex_entry" =~ ^@ ]]; then
echo "Failed to fetch bibtex entry for DOI: $doi"
return 1
fi
local normalized_doi="${doi#doi:}"
if ! grep -q -E "doi\s*=\s*\{${normalized_doi//(/\\(}\}" "$BIB_FILE"; then
if [ -s "$BIB_FILE" ]; then
echo "" >> "$BIB_FILE"
fi
echo "$bibtex_entry" >> "$BIB_FILE"
echo "Added bibtex entry for DOI: $doi"
else
echo "Bibtex entry for DOI: $doi already exists in the file."
fi
}
if [ -z "$1" ]; then
echo "Give either a pdf file or a DOI or a directory path including PDFs as an argument."
exit 1
fi fi
# Check crossref.org for the bib citation. if [ -d "$1" ]; then
curl -s "https://api.crossref.org/works/$doi/transform/application/x-bibtex" -w "\\n" for pdf in "$1"/*.pdf; do
doi=$(get_doi_from_pdf "$pdf")
if [ -n "$doi" ]; then
doi=$(normalize_doi "$doi")
process_doi "$doi"
else
echo "Could not find DOI in PDF file: $pdf"
fi
done
elif [ -f "$1" ] && [[ "$1" =~ \.pdf$ ]]; then
doi=$(get_doi_from_pdf "$1")
if [ -n "$doi" ]; then
doi=$(normalize_doi "$doi")
process_doi "$doi"
else
echo "Could not find DOI in PDF file: $1"
fi
else
doi=$(echo "$1" | eval "$CORRECTION_METHOD")
if [ -n "$doi" ]; then
doi=$(normalize_doi "$doi")
process_doi "$doi"
else
echo "Invalid DOI provided: $1"
fi
fi