2021-02-12 23:20:57 +01:00
|
|
|
#!/usr/bin/env sh
|
2020-05-20 18:34:34 +02:00
|
|
|
|
|
|
|
######################################################################
|
2020-05-23 15:47:21 +02:00
|
|
|
# @author : swytch
|
|
|
|
# @file : bibinput
|
2020-06-25 18:19:40 +02:00
|
|
|
# @license : MIT
|
2020-05-23 15:47:21 +02:00
|
|
|
# @created : Wednesday May 20, 2020 17:57:47 CEST
|
2020-05-20 18:34:34 +02:00
|
|
|
#
|
2020-05-23 15:47:21 +02:00
|
|
|
# @description : create a bibliography entry through dmenu
|
2020-05-25 14:44:06 +02:00
|
|
|
# @dependencies : dmenu
|
2020-05-20 18:34:34 +02:00
|
|
|
######################################################################
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
parse(){
|
|
|
|
if [ "$1" = "@" ]; then
|
2020-05-25 14:44:06 +02:00
|
|
|
grep "$1" "$file" | awk -F '{' '{print $2} ' | tr ',' '\n' | sed '/^[[:space:]]*$/d' | sort -r
|
2020-05-06 03:20:19 +02:00
|
|
|
elif [ "$1" = "keywords" ]; then
|
2020-05-25 14:44:06 +02:00
|
|
|
grep "$1" "$file" | awk -F '"' '{print $2} ' | tr ' ' '\n' | sed -e 's/,//g' -e '/^[[:space:]]*$/d' | sort -u
|
2020-05-06 03:20:19 +02:00
|
|
|
else
|
2020-05-25 14:44:06 +02:00
|
|
|
grep "$1" "$file" | sed "s/,$//g" | awk -F '"' '{print $2} ' | sort -u
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
input() {
|
|
|
|
if [ "$1" = "reference" ]; then
|
|
|
|
refinput | dmenu -i -p 'reference?' -l 10
|
|
|
|
elif [ "$1" = "doctype" ]; then
|
2020-05-25 15:04:16 +02:00
|
|
|
printf "$doctypes" | sed "s| |\n|g" | dmenu -i -p 'doctype?'
|
2020-05-06 03:20:19 +02:00
|
|
|
elif [ "$1" = "author" ]; then
|
|
|
|
parse "author" | dmenu -i -p 'author name? (surname, first name)' -l 10
|
|
|
|
elif [ "$1" = "keywords" ]; then
|
|
|
|
parse "keywords" | dmenu -i -p "keywords? {$keywords}" -l 10
|
|
|
|
else
|
|
|
|
parse "$1" | dmenu -i -p "$1?" -l 10
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
refinput(){
|
2020-05-25 15:04:16 +02:00
|
|
|
printf "ref$(cat "$file" | grep '@' | wc -l | awk '{printf $1}')\n" && parse "@"
|
2020-05-06 03:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
doctypes="article book online techreport"
|
2020-05-20 17:03:00 +02:00
|
|
|
file="$(find $HOME/documents/bibliographies/ -type f -not -path '*/\.*' | dmenu -l 20 -p "[bibinput] which bibliography?")" # the -not -path allows find to ignore hidden files
|
2020-05-06 03:20:19 +02:00
|
|
|
|
2020-05-20 17:03:00 +02:00
|
|
|
[ -z "$file" ] && exit 1
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
refs="$(cat "$file" | grep "@" | sed "s/,$//g" | awk -F '{' '{print $2}' | tr '\n' ' ')"
|
|
|
|
|
2020-05-20 17:03:00 +02:00
|
|
|
# if the file does not exist, create it
|
2020-05-06 03:20:19 +02:00
|
|
|
if [ ! -e "$file" ]; then
|
|
|
|
mkfile="$(printf "No\\nYes" | dmenu -i -p "$file does not exist. Create it?")"
|
|
|
|
[ "$mkfile" = "Yes" ] && (touch "$file")
|
|
|
|
fi
|
|
|
|
|
|
|
|
doctype="$(input "doctype")"
|
|
|
|
[ -z "$doctype" ] && exit 1
|
|
|
|
reference="$(input "reference")"
|
|
|
|
[ -z "$reference" ] && exit 1
|
|
|
|
while [ "${refs#*"$reference"}" != "$refs" ]; do
|
|
|
|
reference="$(refinput | dmenu -i -p 'ref already exist, please provide another' -l 10)"
|
|
|
|
[ -z "$refname" ] && exit 1
|
|
|
|
done
|
|
|
|
author="$(input "author")"
|
|
|
|
[ -z "$author" ] && exit 1
|
|
|
|
title="$(input "title")"
|
|
|
|
[ -z "$title" ] && exit 1
|
|
|
|
year="$(input "year")"
|
|
|
|
[ -z "$year" ] && exit 1
|
|
|
|
if [ "article" == "$doctype" ]; then
|
|
|
|
journal="$(input "journal")"
|
|
|
|
[ -z "$journal" ] && exit 1
|
|
|
|
volume="$(input "volume")"
|
|
|
|
fi
|
|
|
|
if [ "$doctype" = "book" ]; then
|
|
|
|
publisher="$(input "publisher")"
|
|
|
|
[ -z "$publisher" ] && exit 1
|
|
|
|
fi
|
|
|
|
if [ "$doctype" = "online" ]; then
|
|
|
|
url="{URL}"
|
|
|
|
fi
|
|
|
|
if [ "$doctype" = "techreport" ]; then
|
|
|
|
institution="$(input "institution")"
|
|
|
|
[ -z "$institution" ] && exit 1
|
|
|
|
fi
|
|
|
|
keyword="$(input "keywords")"
|
|
|
|
keywords=$keyword
|
|
|
|
if [ -z "$keyword" ]; then
|
|
|
|
keyword_test="no"
|
|
|
|
else
|
|
|
|
keyword_test="$(printf "yes\nno" |dmenu -p "another keyword?")"
|
2020-05-25 14:44:06 +02:00
|
|
|
keyword_test=${keyword_test:-"no"}
|
2020-05-06 03:20:19 +02:00
|
|
|
fi
|
|
|
|
while [ "$keyword_test" = "yes" ]; do
|
|
|
|
keyword="$(input "keywords")"
|
|
|
|
if [ -z "$keyword" ];then
|
|
|
|
keyword_test="no"
|
|
|
|
elif [ "${keywords#*"$keyword"}" != "$keywords" ]; then
|
|
|
|
keyword_test="$(printf "yes\nno" |dmenu -p "keyword already given. another keyword?")"
|
2020-05-25 14:44:06 +02:00
|
|
|
keyword_test=${keyword_test:-"no"}
|
2020-05-06 03:20:19 +02:00
|
|
|
else
|
|
|
|
keywords="$keywords, $keyword"
|
|
|
|
keyword_test="$(printf "yes\nno" |dmenu -p "another keyword?")"
|
|
|
|
keyword_test=${keyword_test:-no}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-05-25 15:04:16 +02:00
|
|
|
printf "\n@$doctype{"$reference"," >> "$file"
|
|
|
|
printf "\n\tauthor = \"$author\"," >> "$file"
|
|
|
|
printf "\n\ttitle = \"$title\"," >> "$file"
|
|
|
|
printf "\n\tyear = \"$year\"," >> "$file"
|
|
|
|
[ -n "$journal" ] && printf "\n\tjournal = \"$journal\"," >> "$file"
|
|
|
|
[ -n "$volume" ] && printf "\n\tvolume = \"$volume\"," >> "$file"
|
|
|
|
[ -n "$publisher" ] && printf "\n\tpublisher = \"$publisher\"," >> "$file"
|
|
|
|
[ -n "$url" ] && printf "\n\turl = \"$url\"," >> "$file"
|
|
|
|
[ -n "$institution" ] && printf "\n\tinstitution = \"$institution\"," >> "$file"
|
|
|
|
printf "\n\tkeywords = \"$keywords\"" >> "$file"
|
|
|
|
printf "\n}" >> "$file"
|