#!/bin/bash NOTES_FOLDER="$HOME/Documents/Notes" JSON_DB="$NOTES_FOLDER/books.yml" function exists_or_create() { if [[ -f "$2" ]]; then echo "Not Creating the note: File is already there" else echo -e "$1 \n$2" echo -e "# Book: $1\n" > "$2" cat <> $JSON_DB - book: "$1" hash: "$(basename $2)" date: "$(date '+%d/%m/%Y %H:%M:%S')" EOF fi } function add_annotation() { selection=$(xclip -out -selection clipboard | sed ':a;N;$!ba;s/\n/ /g') text="" ccount=0 for word in $selection; do ccount=$(( $ccount + $(echo $word | wc -c))) if [ $ccount -gt 75 ]; then ccount=0 echo "$(echo $text | xargs)" >> $1 text="" fi text="$text$word " done echo -e "> \n" >> $1 } hashed_filename="$NOTES_FOLDER/$(md5sum "$1" | cut -f1 -d' ').md" filename=$(basename "$@") exists_or_create "$filename" "$hashed_filename" add_annotation "$hashed_filename"