-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdv
executable file
·142 lines (117 loc) · 2.79 KB
/
mdv
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
#!/usr/bin/env bash
# TODO: Evaluate alternatives
# TODO: Consider flags for pandoc and lynx
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}():} '
set -e
set -u
set -o pipefail
readonly version=1
: "${LINES:=$(tput lines)}"
export LINES
: "${COLUMNS:=$(tput cols)}"
export COLUMNS
: "${BROWSER:=lynx -stdin}"
export BROWSER
command -v pandoc >/dev/null || {
echo "$0: Install pandoc" >&2
exit 1
}
fmt=fmt
readonly fmt_width=$((COLUMNS - 5))
function setup-terminal() {
if [[ ! -t 1 ]]; then
readonly fmt=cat
return 0
fi
if ((fmt_width < 10)); then
echo "$0: Your terminal is too narrow." >&2
readonly fmt=cat
return 0
fi
fmt="fmt -w $fmt_width"
readonly fmt
}
function print-usage() {
cat <<EOU | $fmt
Usage: $0 [-h|--help][-m|--man][--version][-w|--web] MARKDOWN-FILE [VIEWER-OPTIONS]
EOU
}
function print-help() {
echo "$0, version $version"
print-usage
cat <<EOH
Renders a single GitHub MARKDOWN-FILE to your BROWSER or PAGER.
Options:
-h, --help Print help and exit normally
-f, --from Pick a specific markdown variant (default: gfm)
-m, --man View as man page
--version Print version and exit normally
-w, --web View as HTML (default)
Examples:
mdv README.md
Use the BROWSER environment variable to change your HTML renderer.
Use the PAGER environment variable to change your viewer for the "man" option.
EOH
}
function print-version() {
cat <<EOV
${0##*/} $version
This is free and unencumbered software released into the public domain.
For more information, please refer to <http://unlicense.org/>.
Written by B. K. Oxley (binkley).
EOV
}
function is-single-file() {
local file="$1"
[[ ! -e "$file" ]] && {
echo "$0: $file: No such file or directory" >&2
exit 1
}
[[ -d "$file" ]] && {
echo "$0: $file: Is a directory" >&2
exit 1
}
[[ -r "$file" ]] || {
echo "$0: $file: Permission denied" >&2
exit 1
}
}
function view-as-man() {
local file="$1"
shift
is-single-file "$file"
pandoc -s -f $from -t man "$file" | groff -T utf8 -man | ${PAGER-less} "$@"
}
function view-as-web() {
local file="$1"
shift
is-single-file "$file"
pandoc -f $from -t html5 "$file" | $BROWSER "$@"
}
setup-terminal
from=gfm
view=web
while getopts :hf:mvw-: opt; do
[[ $opt == - ]] && opt=${OPTARG%%=*} OPTARG=${OPTARG#*=}
case $opt in
h | help)
print-help
exit 0
;;
f | from) from="$OPTARG" ;;
m | man) view=man ;;
version)
print-version
exit 0
;;
w | web) view=web ;;
*)
print-usage >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
file="$1"
shift 2 || true # Drop the --
view-as-$view "$file" "$@"