-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipes
executable file
·125 lines (100 loc) · 2.56 KB
/
recipes
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
#!/usr/bin/env bash
# ┏━┓┏━╸┏━╸╻┏━┓┏━╸┏━┓
# ┣┳┛┣╸ ┃ ┃┣━┛┣╸ ┗━┓
# ╹┗╸┗━╸┗━╸╹╹ ┗━╸┗━┛
#
# script to open my recipes in zathura
PROG=$(basename "$0")
ROOT="${XDG_DOCUMENTS_DIR:-}/vimwiki/cookbook/"
TMP="/tmp"
TERMINAL=${TERMINAL:-st}
DEPS=(pandoc zathura find pdflatex)
function send_notification {
local prog
local mesg="<b>$1</b>"
local icon="cookie"
prog=$(echo "$PROG" | tr '[:lower:]' '[:upper:]')
notify-send -i "$icon" "$prog" "$mesg"
}
function log_err {
printf "%s: %s\n" "$PROG" "$*" >&2
exit 1
}
if [[ ! -d "$ROOT" ]]; then
err_msg="path not found: $ROOT"
send_notification "$err_msg"
log_err "$err_msg"
fi
# load files
declare -a COOKS
readarray -t COOKS < <(find "$ROOT" -type f -name "*.md" -exec basename {} \;)
function list_recipes {
local recipe
recipe=$(printf "%s\n" "${COOKS[@]}" | dmenu -l 10 -p "cookbook>")
echo "$recipe"
}
function select_recipe {
local recipe
recipe=$(list_recipes)
echo "$recipe"
}
function convert_recipe {
local filepath filename pdf
filepath="$1"
filename=$(basename "$filepath")
pdf="${TMP}/${filename%%.md}.pdf"
# if [[ ! -f "${pdf}" ]]; then
# pandoc --from=markdown --to=pdf "${filepath}" --output="${pdf}"
# fi
pandoc --from=markdown --to=pdf "${filepath}" --output="${pdf}"
echo "$pdf"
}
function open_recipe {
local recipe
recipe=$(select_recipe)
[[ -z "${recipe}" ]] && exit 1
file=$(find "$ROOT" -type f -name "${recipe}")
pdf=$(convert_recipe "${file}")
if [[ ! -f "${pdf}" ]]; then
printf "%s: %s\n" "$PROG" "pdf file '${pdf}' not found"
exit 1
fi
zathura "$pdf"
}
function edit_recipe {
local recipe
recipe=$(select_recipe)
$TERMINAL -e nvim "${ROOT}/${recipe}"
}
function redo_recipe {
local recipe
recipe=$(select_recipe)
file=$(find "$ROOT" -type f -name "${recipe}")
convert_recipe "${file}"
}
function usage {
cat <<-_EOF
Usage: $PROG [options]
Options:
-e, --edit edit recipe
-r, --redo convert recipe
-o, --open open recipe
-h, --help display this help and exit
_EOF
}
function main {
local recipe cmd
local arg="$1"
for cmd in "${DEPS[@]}"; do
if ! command -v "$cmd" >/dev/null; then
log_err "'$cmd' not found"
fi
done
case "$arg" in
-h | *help) usage ;;
-e | *edit) edit_recipe ;;
-r | redo) redo_recipe ;;
*) open_recipe "${recipe}" ;;
esac
}
main "$@"