forked from ttscoff/QuickQuestion
-
Notifications
You must be signed in to change notification settings - Fork 0
/qq
Copy pathexecutable file
·92 lines (86 loc) · 3.04 KB
/qq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# notes folder, for note creation and limiting searches
NOTESDIR="$HOME/Dropbox/nvALT2.2"
# extension used for your notes
NOTESEXT="md"
# the prefix you use to separate "Question" notes
NOTESPRE="??"
# editor command to use for modifying answers
EDITOR="subl"
NOTESDIR=`echo $NOTESDIR|sed -e '/\/$/! s/$/\//'`
filename="filename"
AND="AND"
case $LANG in
ru_RU.UTF-8|ru_RU)
filename="имя"
export LC_CTYPE=C
AND="И"
;;
esac
if [[ "$1" == "-h" ]]
then
appname=`basename $0`
echo "$appname: build a knowledgebase with plain text files"
echo "Find an answer: $appname \"terms to search for\""
echo "Add a question\answer and (optional) tags: $appname -a \"Question in natural language\" \"Succinct answer\" \"tag1 tag2\""
echo "Add a question\answer\tags interactively: $appname -a"
echo "Edit a question\answer: $appname -e \"terms to search for\" # first question found is edited"
elif [[ "$1" == "-a" ]]
then
if [ $# == 3 ]; then
QUESTION=$2
ANSWER=$3
elif [ $# == 4 ]; then
QUESTION=$2
ANSWER=$3
TAGS=$4
elif [ $# == 1 ]; then
echo -n "Question: "
read QUESTION
echo -n "Answer: "
read ANSWER
echo -n "Tags: "
read TAGS
else
echo "Invalid number of arguments for -a(dd). Requires question and answer (or no arguments to input them at runtime)."
echo "example: ${0##*/} -a \"What is the meaning of life?\" \"42\""
exit 1
fi
echo -n "$ANSWER" > "${NOTESDIR}$NOTESPRE $QUESTION.$NOTESEXT" && echo "Question added and answered." || echo "Something went wrong"
[[ ! -z $TAGS ]] && openmeta -a $TAGS -p "${NOTESDIR}$NOTESPRE $QUESTION.$NOTESEXT" && echo "Tags added." || echo "Something went wrong"
elif [[ "$1" == "-e" ]]; then
shift
INPUT=$@
ANSWER=`mdfind -onlyin "$NOTESDIR" "${filename}:.$NOTESEXT ${AND} ${filename}:\"$NOTESPRE\" ${AND} ${INPUT%\?}"|head -n 1`
if [[ "$ANSWER" == "" ]]; then
echo "No results found for search."
exit 2
else
$EDITOR "$ANSWER"
fi
else
INPUT=$@
echo "`mdfind -onlyin \"$NOTESDIR\" -interpret \"${filename}:.$NOTESEXT ${AND} ${filename}:$NOTESPRE ${AND} ${INPUT%\?}\"`"|while read LINE; do
if [[ "$LINE" == "" ]]; then
echo "Sorry, I don't know the answer to that question."
exit 1;
fi
QUESTION=${LINE##*/}
echo -n "Q: "
NOTESPREESC=`echo "$NOTESPRE"|sed -E 's/([\?\!\$\`\"]) ?/\\\\\1/g'`
echo ${QUESTION%%.$NOTESEXT}|sed -E "s/$NOTESPREESC ?//g"|sed -E 's/([^\?])$/\1?/'
echo -n "A: "
cat "$LINE"|sed -E 's/@\([^\)]+\) ?//g'|sed -E 's/@copy\(([^\)]+)\)/\1/'|sed -E 's/@open\(([^\)+]*)\)/Related URL: \1/'|sed -E 's/@[^\( ]+ ?//g'|sed -E 's/^[ ]*|[ ]*$//g'
if [[ `cat "$LINE"|grep -E '@copy\('` ]]; then
cat "$LINE"|grep '@copy('|sed -E 's/.*@copy\(([^\)]+)\).*/\1/'|tr -d '\n'|pbcopy
echo "Example in clipboard"
fi
if [[ `cat "$LINE"|grep -E '@open\('` ]]; then
url=$(cat "$LINE"|grep '@open('|sed -E 's/.*@open\(([^\)]+)\).*/\1 /'|tr -d '\n')
open -g $url
echo "Opened URL"
fi
echo
done
fi
exit 0