Reformat | Add custom .bib

This commit is contained in:
Emre AKYÜZ 2024-05-22 18:27:24 +03:00 committed by GitHub
parent 7752ed9ef1
commit 5c346d90ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,81 +1,71 @@
#!/bin/dash #!/bin/sh
BIB_FILE="${HOME}/latex/uni.bib" BIB_FILE="${HOME}/latex/uni.bib"
[ -f "${BIB_FILE}" ] || BIB_FILE="${2:-$(find "${HOME}" -path "${HOME}/.*" \
-prune -o -type "f" -name "*.bib" -print -quit)}"
correction_method() { { [ -f "${BIB_FILE}" ] || [ "${2}" ]; } || {
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/\6/p; T; q' printf "%s\n" "Create a .bib file or provide as \$2." && exit "1"
} }
get_doi_from_pdf() { filter() {
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/\6/p; T; q'
}
fpdf() {
pdf="${1}" pdf="${1}"
doi="$(pdfinfo "${pdf}" 2> "/dev/null" | correction_method)" doi="$(pdfinfo "${pdf}" 2> "/dev/null" | filter)"
[ -z "${doi}" ] && doi="$(pdftotext -q -l "2" "${pdf}" - 2> "/dev/null" | correction_method)" [ "${doi}" ] || doi="$(pdftotext -q -l "2" "${pdf}" - 2> "/dev/null" | filter)"
[ -z "${doi}" ] && echo "No DOI found for PDF: ${pdf}" >&2 && return "1" [ "${doi}" ] || printf "%s\n" "No DOI found for PDF: ${pdf}" >&2
echo "${doi}" printf "%s\n" "${doi}"
} }
correct_names() { arrange() {
sed 's/\}, /\},\n /g sed 's/\}, /\},\n /g
s/, /,\n / s/, /,\n /
s/ }/\n}/ s/ }/\n}/
s/,\s*pages=/,\n\tpages=/' | s/,\s*pages=/,\n\tpages=/' |
sed '1s/^ *// sed '1s/^ *//
1s/[0-9]*\([0-9]\{2\}\)/\1/ 1s/[0-9]*\([0-9]\{2\}\)/\1/
1s/_// 1s/_//
1s/.*/\L&/ 1s/.*/\L&/
s/.*=/\L&/ s/.*=/\L&/
s/=/ = /' s/=/ = /'
} }
process_doi() { doi2bib() {
doi="${1}" doi="${1#doi:}"
bibtex_entry="$(curl -s "https://api.crossref.org/works/${doi}/transform/application/x-bibtex" | correct_names)" url="https://api.crossref.org/works/${doi}/transform/application/x-bibtex"
red_color='\033[0;31m' entry="$(curl -kLsS --no-fail "${url}" | arrange)"
reset_color='\033[0m' red='\033[0;31m'
reset='\033[0m'
printf "${red_color}%s${reset_color}\n" "${bibtex_entry}" printf "${red}%s${reset}\n" "${entry}"
[ -z "${bibtex_entry}" ] && [ "$(echo "${bibtex_entry}" | cut -c2)" != "@" ] && { [ "${entry%"${entry#?}"}" != "@" ] && {
echo "Failed to fetch bibtex entry for DOI: ${doi}" printf "%s\n" "Failed to fetch bibtex entry for DOI: ${doi}"
return "1" return "1"
} }
grep -iFq "doi = {${doi}}" "${BIB_FILE}" || { grep -iFq "doi = {${doi}}" "${BIB_FILE}" 2> "/dev/null" && {
[ -s "${BIB_FILE}" ] && echo "" >> "${BIB_FILE}" printf "%s\n" "Bibtex entry for DOI: ${doi} already exists in the file."
echo "${bibtex_entry}" >> "${BIB_FILE}" } || {
echo "Added bibtex entry for DOI: ${doi}" [ -s "${BIB_FILE}" ] && printf "\n" >> "${BIB_FILE}"
return "0" printf "%s\n" "${entry}" >> "${BIB_FILE}"
} printf "%s\n" "Added bibtex entry for DOI: ${doi}"
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"
}
[ -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)" [ "${1}" ] || {
printf "%s\n" "Give either a pdf file or a DOI or a directory path that has PDFs as an argument."
exit "1"
}
[ -n "${doi}" ] && process_doi "${doi}" [ -f "${1}" ] && doi="$(fpdf "${1}")" && doi2bib "${doi}" && exit "0"
[ -d "${1}" ] && for i in "${1}"/*.pdf; do doi="$(fpdf "${i}")" && doi2bib "${doi}"; done && exit "0"
doi="$(printf "%s\n" "${1}" | filter)" && doi2bib "${doi}"