mirror of
https://github.com/LukeSmithxyz/voidrice.git
synced 2026-01-30 09:48:11 +01:00
Compare commits
6 Commits
e2bdb1b7dd
...
ced199b646
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ced199b646 | ||
|
|
0795202675 | ||
|
|
156747aa77 | ||
|
|
8ec5ec569d | ||
|
|
8b4e8f59bb | ||
|
|
62e2856882 |
@ -25,6 +25,11 @@ case "$(file --dereference --brief --mime-type -- "$1")" in
|
||||
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
|
||||
[ ! -f "$CACHE" ] && djvused "$1" -e 'select 1; save-page-with /dev/stdout' | convert -density 200 - "$CACHE.jpg" > /dev/null 2>&1
|
||||
image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" ;;
|
||||
image/svg+xml)
|
||||
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
|
||||
[ ! -f "$CACHE" ] && inkscape --convert-dpi-method=none -o "$CACHE.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$1"
|
||||
image "$CACHE.png" "$2" "$3" "$4" "$5" "$1"
|
||||
;;
|
||||
image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;;
|
||||
text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;;
|
||||
text/troff) man ./ "$1" | col -b ;;
|
||||
|
||||
@ -1,14 +1,76 @@
|
||||
#!/bin/sh
|
||||
[ -z "$1" ] && echo "Give either a pdf file or a DOI as an argument." && exit
|
||||
#!/bin/dash
|
||||
|
||||
if [ -f "$1" ]; then
|
||||
# Try to get DOI from pdfinfo or pdftotext output.
|
||||
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}') ||
|
||||
exit 1
|
||||
else
|
||||
doi="$1"
|
||||
fi
|
||||
BIB_FILE="$HOME/latex/uni.bib"
|
||||
|
||||
# Check crossref.org for the bib citation.
|
||||
curl -s "https://api.crossref.org/works/$doi/transform/application/x-bibtex" -w "\\n"
|
||||
correction_method() {
|
||||
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/doi:\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"
|
||||
}
|
||||
|
||||
correct_names() {
|
||||
sed '/^@[a-z]\+{[^[:space:]]\+[0-9]\{4\},/{
|
||||
s/\([A-Z]\)/\L\1/g
|
||||
s/_//g
|
||||
s/[0-9]*\([0-9]\{2\}\)/\1/g
|
||||
}'
|
||||
}
|
||||
|
||||
normalize_doi() {
|
||||
doi="$1"
|
||||
doi=$(echo "$doi" | sed 's@%@\\x@g' | xargs -I {} printf "%b" "{}")
|
||||
printf "%s" "$doi" | tr 'A-Z' 'a-z'
|
||||
}
|
||||
|
||||
process_doi() {
|
||||
doi="$1"
|
||||
bibtex_entry=$(curl -s "https://api.crossref.org/works/$doi/transform/application/x-bibtex" -w "\\n" | 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
|
||||
|
||||
normalized_doi="${doi#doi:}"
|
||||
grep -q -E "doi\s*=\s*\{$(echo "$normalized_doi" | sed 's/(/\\(/g')\}" "$BIB_FILE" || {
|
||||
[ -s "$BIB_FILE" ] && echo "" >> "$BIB_FILE"
|
||||
echo "$bibtex_entry" >> "$BIB_FILE"
|
||||
echo "Added bibtex entry for DOI: $doi"
|
||||
return 1
|
||||
}
|
||||
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 1
|
||||
|
||||
[ -d "$1" ] && {
|
||||
for pdf in "$1"/*.pdf; do
|
||||
doi=$(get_doi_from_pdf "$pdf")
|
||||
[ -n "$doi" ] && {
|
||||
doi=$(normalize_doi "$doi")
|
||||
process_doi "$doi"
|
||||
}
|
||||
done
|
||||
exit 1
|
||||
}
|
||||
|
||||
[ -f "$1" ] && [ "$(echo "$1" | grep -c "\.pdf$")" -ne 0 ] && {
|
||||
doi=$(get_doi_from_pdf "$1")
|
||||
[ -n "$doi" ] && {
|
||||
doi=$(normalize_doi "$doi")
|
||||
process_doi "$doi"
|
||||
}
|
||||
exit 1
|
||||
}
|
||||
|
||||
doi=$(echo "$1" | correction_method)
|
||||
[ -n "$doi" ] && {
|
||||
doi=$(normalize_doi "$doi")
|
||||
process_doi "$doi"
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user