-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.sh
executable file
·144 lines (129 loc) · 3.97 KB
/
index.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env zsh
__schoolkit_dir="$HOME/dev/schoolkit" # TODO: make sure this is right
__schoolkit_work_dir="$HOME/School"
function __schoolkit_get_real_name() {
if [ -n "$SCHOOLKIT_REAL_NAME" ]; then
echo "$SCHOOLKIT_REAL_NAME"
elif [[ "Darwin" == "$(uname)" ]]; then
dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p'
else
echo "Unable to detect real name! Please set \$SCHOOLKIT_REAL_NAME to your real-world name." >&2
fi
}
function __schoolkit_edit() {
editor="${SCHOOLKIT_EDITOR:-${EDITOR:-vim}}"
if [[ "${1: -3}" = ".md" ]] || [[ "${1: -4}" = ".txt" ]]; then
if [ "${editor: -3}" = "vim" ]; then
$editor "+Goyo" "$1"
else
$editor "$1"
fi
else
open "$1"
fi
}
function __schoolkit_select_note() {
if [ -z "$1" ]; then
echo "$(__schoolkit_notes_list | fzf)"
else
if [ "$1" == "latest" ]; then
echo "$(__schoolkit_notes_list | head -1)"
else
echo "$1"
fi
fi
}
function __schoolkit_notes_new() {
if [ $# -eq 0 ]; then
echo "Please provide a title to \`sn new\`! Run \`sn help\` for more information."
return 1
fi
filename="$(date +%Y-%m-%d) [email protected]"
touch "$filename"
__schoolkit_edit "$filename"
}
function __schoolkit_notes_list() {
ls | sort -nr | "${PAGER:-less}"
}
function __schoolkit_notes_edit() {
filename="$(__schoolkit_select_note "$1")"
# https://superuser.com/a/1002826
if [[ "${@#-p}" = "$@" ]]; then
__schoolkit_edit "$filename"
else
echo "$filename"
fi
}
function __schoolkit_notes_cornell() {
browser="${SCHOOLKIT_BROWSER:-${BROWSER:-Google Chrome}}"
filename="$(__schoolkit_select_note "$1")"
export SCHOOLKIT_REAL_NAME="$(__schoolkit_get_real_name)"
dataurl="$(
cat "$filename" |
"$__schoolkit_dir/markdown-cornell/run.js" --data-uri
)" &&
osascript -e "tell application \"$browser\" to activate" &&
osascript -e "tell application \"$browser\" to open location \"$dataurl\""
}
function __schoolkit_notes_mla() {
filename="$(__schoolkit_select_note "$1")"
bibname="$(basename "$filename" ".md").bib"
outputname="$(basename "$filename" ".md").docx"
prettydate="$(date -jf "%Y-%m-%d" "$(echo "$filename" | cut -d' ' -f1)" "+%B %d, %Y")"
pandoc_opts=(
--from=markdown
--to=docx
-M "author=$(__schoolkit_get_real_name)"
-M "date=$prettydate"
--reference-doc="$__schoolkit_dir/mla-reference.docx"
)
if [ -f "$bibname" ]; then
pandoc_opts += (
-M "bibliography=$bibname"
-M "csl=$__schoolkit_dir/mla.csl"
--filter pandoc-citeproc
)
fi
pandoc $pandoc_opts -o "$outputname" "$filename" && open "$outputname"
}
function sn() {
if [ $# -eq 0 ]; then
__schoolkit_notes_list
else
case "$1" in
list)
__schoolkit_notes_list "${@:2}"
;;
new)
__schoolkit_notes_new "${@:2}"
;;
edit)
__schoolkit_notes_edit "${@:2}"
;;
cornell)
__schoolkit_notes_cornell "${@:2}"
;;
mla)
__schoolkit_notes_mla "${@:2}"
;;
help)
echo "Usage:"
echo " $0 [class] list|new"
echo " $0 [class] edit|cornell|mla [latest|filename.md]"
echo ""
echo "[class] - change to the class's folder before executing the command"
echo "list - list notes"
echo "new ... - create a new note, using all remaining arguments as a name (spaces are OK!)"
echo "edit [latest|./path/to/note.md]- edit a note in \$EDITOR (currently $EDITOR). Defaults to prompting you to select a note. Pass 'latest' as the first argument to edit the latest note."
echo "cornell [latest|./path/to/note.md] - convert a markdown note to an HTML cornell note and open it in a browser. Defaults to prompting you to select a note. Pass 'latest' as the first argument to convert the latest note."
echo "mla [latest|./path/to/note.md] - convert a markdown note to an MLA-formatted word document. Defaults to prompting you to select a note. Pass 'latest' as the first argument to convert the latest note. Will ues a '.bib.' file with the same name if present."
;;
*)
cd "$__schoolkit_work_dir/$1"
if [[ $# -gt 1 ]]; then
sn "${@:2}"
fi
;;
esac
fi
}