Formatting change.

This commit is contained in:
Emre AKYÜZ 2024-01-07 19:20:47 +03:00 committed by GitHub
parent 0465be083b
commit 7752ed9ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,25 +1,28 @@
#!/bin/dash
BIB_FILE="$HOME/latex/uni.bib"
BIB_FILE="${HOME}/latex/uni.bib"
correction_method() {
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/\6/p; T; q'
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/\6/p; T; q'
}
get_doi_from_pdf() {
pdf="$1"
doi=$(pdfinfo "$pdf" 2>/dev/null | correction_method)
[ -z "$doi" ] && doi=$(pdftotext -q -l 2 "$pdf" - 2>/dev/null | correction_method)
[ -z "$doi" ] && echo "No DOI found for PDF: $pdf" >&2 && return 1
echo "$doi"
pdf="${1}"
doi="$(pdfinfo "${pdf}" 2> "/dev/null" | correction_method)"
[ -z "${doi}" ] && doi="$(pdftotext -q -l "2" "${pdf}" - 2> "/dev/null" | correction_method)"
[ -z "${doi}" ] && echo "No DOI found for PDF: ${pdf}" >&2 && return "1"
echo "${doi}"
}
correct_names() {
sed 's/\}, /\},\n /g
sed 's/\}, /\},\n /g
s/, /,\n /
s/ }/\n}/
s/,\s*pages=/,\n\tpages=/' |
sed '1s/^ *//
sed '1s/^ *//
1s/[0-9]*\([0-9]\{2\}\)/\1/
1s/_//
1s/.*/\L&/
@ -28,36 +31,51 @@ correct_names() {
}
process_doi() {
doi="$1"
bibtex_entry=$(curl -s "https://api.crossref.org/works/"$doi"/transform/application/x-bibtex" | correct_names)
red_color='\033[0;31m'
reset_color='\033[0m'
doi="${1}"
bibtex_entry="$(curl -s "https://api.crossref.org/works/${doi}/transform/application/x-bibtex" | correct_names)"
red_color='\033[0;31m'
reset_color='\033[0m'
printf "${red_color}%s${reset_color}\n" "$bibtex_entry"
[ -z "$bibtex_entry" ] && [ "$(echo "$bibtex_entry" | cut -c2)" != "@" ] && echo "Failed to fetch bibtex entry for DOI: $doi" && return 1
printf "${red_color}%s${reset_color}\n" "${bibtex_entry}"
grep -iFq "doi = {${doi}}" "$BIB_FILE" || {
[ -s "$BIB_FILE" ] && echo "" >> "$BIB_FILE"
echo "$bibtex_entry" >> "$BIB_FILE"
echo "Added bibtex entry for DOI: $doi"
return 0
}
echo "Bibtex entry for DOI: $doi already exists in the file."
[ -z "${bibtex_entry}" ] && [ "$(echo "${bibtex_entry}" | cut -c2)" != "@" ] && {
echo "Failed to fetch bibtex entry for DOI: ${doi}"
return "1"
}
grep -iFq "doi = {${doi}}" "${BIB_FILE}" || {
[ -s "${BIB_FILE}" ] && echo "" >> "${BIB_FILE}"
echo "${bibtex_entry}" >> "${BIB_FILE}"
echo "Added bibtex entry for DOI: ${doi}"
return "0"
}
echo "Bibtex entry for DOI: ${doi} already exists in the file."
}
[ -z "$1" ] && echo "Give either a pdf file or a DOI or a directory path that has PDFs as an argument." && exit 0
[ -d "$1" ] && {
for pdf in "$1"/*.pdf; do
doi=$(get_doi_from_pdf "$pdf")
[ -n "$doi" ] && process_doi "$doi"; done
exit 0
[ -z "${1}" ] && {
echo "Give either a pdf file or a DOI or a directory path that has PDFs as an argument."
exit "0"
}
[ -f "$1" ] && [ "$(echo "$1" | grep -c "\.pdf$")" -ne 0 ] && {
doi=$(get_doi_from_pdf "$1")
[ -n "$doi" ] && { process_doi "$doi"; exit 0; }
[ -d "${1}" ] && {
for pdf in "${1}"/*.pdf; do
doi="$(get_doi_from_pdf "${pdf}")"
[ -n "${doi}" ] && process_doi "${doi}"
done
exit "0"
}
doi=$(echo "$1" | correction_method)
[ -n "$doi" ] && process_doi "$doi"
[ -f "${1}" ] && [ "$(echo "${1}" | grep -c "\.pdf$")" -ne "0" ] && {
doi="$(get_doi_from_pdf "${1}")"
[ -n "${doi}" ] && {
process_doi "${doi}"
exit "0"
}
}
doi="$(echo "${1}" | correction_method)"
[ -n "${doi}" ] && process_doi "${doi}"