-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvw
executable file
·198 lines (173 loc) · 4.23 KB
/
pvw
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
# preview a pdf
# depends on: pandoc, wget, fzf, poppler (for pdfunite)
# Copyright (c) 2021 Julia
version="1.3.2"
preview() {
# make a temporary file, view it and remove it once viewed
local outputFile
[[ -s "$1" ]] || printError "'$1' does not exist."
if [[ "${1: -3}" == pdf ]]; then
$pdfViewer "$1" &
return
fi
# set the output file based on if it'll be saved or not
if [[ "${preserve}" ]]; then
outputFile="${1%.*}.pdf"
else
outputFile="$(mktemp --suffix='.pdf')"
fi
trap 'rm -f ${outputFile}' EXIT
# convert and open the file
pandoc --quiet "$1" -o "${outputFile}" 2>/dev/null &&
$pdfViewer "${outputFile}" &
if [[ ! "$preserve" ]]; then
rm -f -- "${outputFile}"
fi
}
detectUrl() {
# detect if input is a url, return a filename
regex="https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
if grep -Ei "$regex" <<<"$1" >/dev/null; then
getFile "$1"
else
echo "$1"
fi
}
getFile() {
# download a file
local downloadedFile
if [[ "$preserve" ]]; then
# trim the url name to the specific file, and replace any extension with html
# this exists to remove html anchors from the filename
local tmp="${1##*/}"
downloadedFile="${tmp%.*}.html"
unset tmp
else
downloadedFile="$(mktemp --suffix='.html')"
fi
wget -q "$1" -O "${downloadedFile}"
trap 'rm -f ${downloadedFile}' EXIT
echo "${downloadedFile}"
}
merge() {
# merge pdfs based on the order they're given as args
# TODO: (jam) use pdfunite
:
}
main() {
checkDeps
# check for arguments and run preview() on every valid file given
parseArgs "$@"
# TODO: (jam) option to combine files into one pdf (pandoc -s)
# declare arrays for args and opts, sort provided arguments accordingly
declare -a args opts
for arg in "$@"; do
# remove pdfViewer from args
if [[ "${pdfViewer}" && "${arg}" == "${pdfViewer}" ]]; then
continue
fi
if [[ $arg == [-]* ]]; then
opts+=("$arg")
else
args+=("$arg")
fi
done
if [[ ! "${pdfViewer}" ]]; then
pdfViewer="xdg-open"
fi
# if edit flag is present, edit input files
if [[ "$edit" ]]; then
# TODO: (jam) implement detectUrl here
for f in "${args[@]}"; do
"$EDITOR" -- "$f"
done
fi
# if the user provided files to operate on, iterate through them
if (("${#args[@]}" > 0)); then
for f in "${args[@]}"; do
preview "$(detectUrl "$f")" || printError "Cannot access file '${f}'"
shift
done
# otherwise, select files interactively
else
local preview_file
preview_file="$(find "." -type f | fzf +m)" || exit 0
preview "${preview_file}"
fi
}
checkDeps() {
# check dependencies
for com in fzf pandoc wget; do
hash "$com" 2>/dev/null || printError "$com needs to be installed to run pvw."
done
}
printError() {
# print errors to stderr
tput setaf 1
printf -- '[ERR] %s\n' "$1" >&2
tput sgr0
exit 1
}
usage() {
# usage
cat <<EOF
Usage: preview [<args>] [file|url]
Arguments are:
-e, --edit Edit input files before generating pdfs
-h, --help Display this help
-k, --keep Save the generated pdf
-r, --readme Preview the README of the current directory
-v, --viewer Choose the pdf viewer to use (default: xdg-open)
--version Display version information
Note:
If providing a URL, be sure to prefix it with 'http[s]'.
EOF
exit 0
}
parseArgs() {
# parse arguments
TEMP="$(getopt -o 'ehkrv:' -l 'edit,help,keep,readme,version,viewer:' -n "pvw" -- "$@")"
eval set -- "$TEMP"
unset TEMP
while true; do
case "$1" in
'-e' | '--edit')
edit=true
shift
continue
;;
'-h' | '--help')
usage
;;
'-k' | '--keep')
# TODO: (jam) change the location the pdf is stored to (opt arg?)
preserve=true
shift
continue
;;
'-r' | '--readme')
preview "./README.md"
exit 0
;;
'--version')
printf -- 'pvw v%s\n' "$version"
exit 0
;;
'-v' | '--viewer')
pdfViewer="$2"
shift 2
continue
;;
'--')
shift
break
;;
*)
printError "$2 is not a recognized argument"
;;
esac
done
}
main "$@"
exit 0