2020-05-11 14:35:26 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
# shows due entries of bibtex file passed in
|
|
|
|
# HACK: brittle! will break on various bibfile abnormities (even just on due date w/o priority)
|
|
|
|
# FIXME: reimplementation with real library needed
|
|
|
|
#
|
|
|
|
|
2020-05-12 10:10:08 +00:00
|
|
|
OPTIND=1 # Reset in case getopts has been used previously in the shell.
|
2020-05-11 14:35:26 +00:00
|
|
|
fields="due|priority|\bauthor\b|\btitle\b"
|
|
|
|
filterby="due"
|
2020-05-12 10:10:08 +00:00
|
|
|
file="${BIBFILE}"
|
|
|
|
until=""
|
2020-05-11 14:35:26 +00:00
|
|
|
|
2020-05-12 10:10:08 +00:00
|
|
|
show_help() {
|
|
|
|
printf "%s\n" \
|
|
|
|
"" \
|
|
|
|
" bib-due Show due readings from bibtex file." \
|
|
|
|
"" \
|
|
|
|
" Usage: bib-due [-hv] -i input.bib -r 'due|priority|\bauthor|\btitle' -l 'due' -u '2020-05-12'" \
|
|
|
|
"" \
|
|
|
|
" Options:" \
|
|
|
|
"" \
|
|
|
|
" -i [bibtex-file] Input bibtex file to scrape and get items from." \
|
|
|
|
"" \
|
|
|
|
" -r [fields] Field values to read in file." \
|
|
|
|
"" \
|
|
|
|
" -l [filter] Field to use as filter entity." \
|
|
|
|
" This field is required for the scraper to pick entries up." \
|
|
|
|
"" \
|
|
|
|
" help | -h | --help Print out this help." \
|
|
|
|
"" \
|
|
|
|
" Invoked without arguments, bib-due will scrape the file defined in BIBFILE environment variable, " \
|
|
|
|
" filtering entries with the 'due' field, and getting the values for 'due', 'priority', 'author', " \
|
|
|
|
" and 'title' fields. It will then print the entries to stdout." \
|
|
|
|
"" \
|
|
|
|
" Example output line:" \
|
|
|
|
"" \
|
|
|
|
' 2020-06-25 (1): Sergei Gerasymchuk -- “Ze” time in Ukraine (Gerasymchuk2019) ' \
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
filter_until() {
|
|
|
|
# filter for dates, with line numbers
|
|
|
|
filtered=$(echo "$entries" | grep -noE '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}')
|
|
|
|
|
|
|
|
# redirect entries to fifo pipe
|
|
|
|
mkfifo filteredentries
|
|
|
|
finish() {
|
|
|
|
rm filteredentries
|
|
|
|
}
|
|
|
|
trap finish EXIT
|
|
|
|
echo "$filtered" >filteredentries &
|
|
|
|
|
|
|
|
# find first date past until filter
|
|
|
|
lastline=""
|
|
|
|
while IFS= read -r line; do
|
|
|
|
cond=$(printf '%s' "$line" | cut -d: -f2)
|
|
|
|
cond=$(date -d "$cond" +%s)
|
|
|
|
if [ "$cond" -gt "$until" ]; then
|
|
|
|
lastline=$(printf '%s' "$line" | cut -d: -f1)
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done <filteredentries
|
|
|
|
|
|
|
|
# special cases all in filter, or none in filter
|
|
|
|
if [ -z "$lastline" ]; then
|
|
|
|
return
|
|
|
|
elif [ "$lastline" -eq 1 ]; then
|
|
|
|
entries=""
|
|
|
|
# filter
|
2020-05-11 14:35:26 +00:00
|
|
|
else
|
2020-05-12 10:10:08 +00:00
|
|
|
# remove lines below found
|
|
|
|
lastprinted="$((lastline - 1))"
|
|
|
|
entries=$(echo "$entries" | sed -n "1,${lastprinted}p;${lastline}q")
|
|
|
|
fi
|
2020-05-16 16:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filter_priority() {
|
|
|
|
priority="$1"
|
|
|
|
# filter for dates, with line numbers
|
|
|
|
# filtered=$(echo "$entries" | grep -noE '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}')
|
|
|
|
filtered=""
|
|
|
|
while [ "$priority" -gt 0 ]; do
|
|
|
|
current=$(echo "$entries" | grep -E "\($priority\)")
|
2020-05-12 10:10:08 +00:00
|
|
|
|
2020-05-16 16:39:06 +00:00
|
|
|
# append found to filtered entries
|
|
|
|
filtered=$(printf "%s\n%s" "$filtered" "$current")
|
|
|
|
|
|
|
|
# go to next 'higher' priority
|
|
|
|
priority="$((priority - 1))"
|
|
|
|
done
|
|
|
|
# sort them chronologically again, remove empty lines
|
|
|
|
entries="$(echo "$filtered" | sed -e '/^$/d' | sort)"
|
2020-05-12 10:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
if [ -z "$file" ]; then
|
2020-05-11 14:35:26 +00:00
|
|
|
echo "Requires a bibtex file as argument."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# filter all entries for those containing filterby field (default: due)
|
|
|
|
# retain values of all fields mentioned in fields variable
|
2020-05-12 10:10:08 +00:00
|
|
|
entries=$(grep -E "^@|$fields" "$file" | awk 1 ORS=' ' | sed -E 's/@\w+\{/\n/g' | grep "$filterby" | tail -n +2 | sed -E 's/(,\s+(\w+)\s+=\s+|,\s*$)/\t/g' | awk -F'\t' '{ print $4 "\t" $5 "\t" $2 "\t" $3 "\t" $1 }')
|
2020-05-11 14:35:26 +00:00
|
|
|
|
|
|
|
# prettify and sort the entries for display (remove {}, order by date,prio,author,title)
|
|
|
|
entries=$(echo "$entries" | awk -F'\t' '{ gsub(/{/,""); gsub(/}/,""); gsub(/prio/,"",$2) } { print $1 " (" $2 "): " $3 " -- " $4 " (" $5 ")"}' | sort)
|
|
|
|
|
2020-05-12 10:10:08 +00:00
|
|
|
if [ -n "$until" ]; then
|
|
|
|
filter_until
|
|
|
|
fi
|
2020-05-16 16:39:06 +00:00
|
|
|
if [ -n "$priority" ]; then
|
|
|
|
filter_priority "$priority"
|
|
|
|
fi
|
2020-05-12 10:10:08 +00:00
|
|
|
|
2020-05-11 14:35:26 +00:00
|
|
|
echo "$entries"
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:39:06 +00:00
|
|
|
while getopts "h?i:u:r:l:p:" opt; do
|
2020-05-12 10:10:08 +00:00
|
|
|
case "$opt" in
|
|
|
|
h | \?)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
# v) verbose=1
|
|
|
|
# ;;
|
|
|
|
r)
|
|
|
|
fields="$OPTARG"
|
|
|
|
;;
|
|
|
|
l)
|
|
|
|
filterby="$OPTARG"
|
|
|
|
;;
|
|
|
|
i)
|
|
|
|
file="$OPTARG"
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
until="$(date -d "$OPTARG" +%s)"
|
|
|
|
;;
|
2020-05-16 16:39:06 +00:00
|
|
|
p)
|
|
|
|
priority="$OPTARG"
|
|
|
|
;;
|
2020-05-12 10:10:08 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
[ "${1:-}" = "--" ] && shift
|
|
|
|
|
2020-05-11 14:35:26 +00:00
|
|
|
main "$@"
|