-
Notifications
You must be signed in to change notification settings - Fork 82
/
mdroff.sh
executable file
·382 lines (309 loc) · 7.59 KB
/
mdroff.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2020 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
# shellcheck disable=SC2021
# shellcheck disable=SC2155
printf_msg() {
# shellcheck disable=SC2059
printf "$@" 1>&2
}
printf_err() {
# shellcheck disable=SC2059
printf "$@" 1>&2
}
# -----------------------------------------------------------------------------
# MDroff:
# -----------------------------------------------------------------------------
mdroff:emit:h1() {
printf '.TH "%s" 1\n' "$1"
}
mdroff:emit:h2() {
printf '.SH "%s"\n' "$1"
}
mdroff:emit:h3() {
printf '.SS "%s"\n' "$1"
}
mdroff:emit:h4() {
printf '.SS "%s"\n' "$1"
}
mdroff:emit:h5() {
printf '.SS "%s"\n' "$1"
}
mdroff:emit:line() {
if "$MDROFF_PARAGRAPH"; then
MDROFF_PARAGRAPH=false
printf ".P\n%s\n" "$1"
else
printf ".br\n%s\n" "$1"
fi
}
mdroff:emit:attr() {
printf '\\fR'
if "$MDROFF_ATTR_STRONG"; then
printf '\\fB'
fi
if "$MDROFF_ATTR_EMPHASIS"; then
printf '\\fI'
fi
if "$MDROFF_ATTR_CODE"; then
printf '\\fI'
fi
}
mdroff:emit:link() {
printf "%s" "$1"
}
# shellcheck disable=SC2034
mdroff:emit:table_start() {
printf ".TS\n"
printf "tab(|) box;\n"
# Print Header Alignment
local temp
for temp in "$@"; do printf "| cB "; done
printf "|\n"
# Print Separator
for temp in "$@"; do printf "| _ "; done
printf "|\n"
# Print Column Alignment
local cols=("$@")
printf "| "
printf "%s0 |1 " "${cols:0:$((${#@}-1))}"
printf "%s " "${cols[$((${#@}-1))]}"
printf "|.\n"
}
mdroff:emit:table_end() {
printf ".TE\n\n"
}
mdroff:emit:table_heading() {
local heading="$(printf "| %s " "$@")"
printf "%s\n" "${heading:1}"
# Prevent tbl warning.
for temp in "${@:2}"; do printf "|"; done
printf "\n"
# Start table data.
printf ".SP\n"
}
mdroff:emit:table_row() {
local row="$(printf "| %s " "$@")"
printf "%s\n" "${row:1}"
}
mdroff:emit:text() {
local text="$1"
text="${text//<br>/ }"
text="${text//<br\/>/ }"
text="${text//<br \/>/ }"
printf "%s" "$text"
}
mdroff:emit() {
local type="$1"
local data="$2"
if type "mdroff:rewrite:${type}" &>/dev/null; then
data="$("mdroff:rewrite:${type}" "${@:2}")"
fi
if type "mdroff:emit_hook:${type}" &>/dev/null; then
"mdroff:emit_hook:${type}" "$data" "${@:3}"
return
fi
"mdroff:emit:${type}" "$data" "${@:3}"
}
mdroff:trim_right() {
# shellcheck disable=SC2001
sed 's/[[:space:]]*$//' <<< "$1"
}
mdroff:trim() {
sed 's/^[[:space:]]*//; s/[[:space:]]*$//' <<< "$1"
}
mdroff:parseln() {
MDROFF_ATTR_STRONG=false
MDROFF_ATTR_EMPHASIS=false
MDROFF_ATTR_CODE=false
local buffer="$1"
local before
local found
local pos
while [[ "${#buffer}" -gt 0 ]]; do
[[ "$buffer" =~ \*{1,3}|\`|\[([^\]]+)\]\(([^\)]+)\) ]] || {
mdroff:emit text "$(mdroff:trim_right "$buffer")"
mdroff:emit text $'\n'
return
}
found="${BASH_REMATCH[0]}"
pos="$(awk -v search="$found" '{print index($0,search) - 1}' <<< "$buffer")"
before="${buffer:0:$pos}"
buffer="${buffer:$(($pos + ${#found}))}"
mdroff:emit text "$before"
case "$found" in
'***')
if "$MDROFF_ATTR_STRONG" && "$MDROFF_ATTR_EMPHASIS"; then
MDROFF_ATTR_STRONG=false
MDROFF_ATTR_EMPHASIS=false
else
MDROFF_ATTR_STRONG=true
MDROFF_ATTR_EMPHASIS=true
fi
mdroff:emit attr
;;
'**')
if "$MDROFF_ATTR_STRONG"; then
MDROFF_ATTR_STRONG=false
else
MDROFF_ATTR_STRONG=true
fi
mdroff:emit attr
;;
'*')
if "$MDROFF_ATTR_EMPHASIS"; then
MDROFF_ATTR_EMPHASIS=false
else
MDROFF_ATTR_EMPHASIS=true
fi
mdroff:emit attr
;;
'`')
if "$MDROFF_ATTR_CODE"; then
MDROFF_ATTR_CODE=false
else
MDROFF_ATTR_CODE=true
fi
mdroff:emit attr
;;
'['*)
mdroff:emit link "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
esac
done
}
mdroff() {
MDROFF_HEADING_LEVEL=0
MDROFF_HEADING=''
MDROFF_ATTR_STRONG=false
MDROFF_ATTR_EMPHASIS=false
MDROFF_ATTR_CODE=false
MDROFF_IN_TABLE=false
MDROFF_PARAGRAPH=false
MDROFF_TABLE_HEADER=()
local line
local empty=0
local empty_continue=0
while IFS='' read -r line; do
line="$(mdroff:parseln "$line")"
# Empty
if [[ "$line" =~ ^[[:space:]]*$ ]]; then
((empty_continue++)) || true
if "$MDROFF_IN_TABLE"; then
mdroff:emit table_end
fi
MDROFF_PARAGRAPH=true
MDROFF_TABLE_HEADER=()
MDROFF_IN_TABLE=false
continue
fi
empty="$empty_continue"
empty_continue=0
# Headings
if [[ "$line" =~ ^(#{1,})[[:space:]]{1,}(.*)$ ]]; then
local level="${#BASH_REMATCH[1]}"
local text="${BASH_REMATCH[2]}"
MDROFF_HEADING_LEVEL="$level"
MDROFF_HEADING="$text"
mdroff:emit "h${level}" "$text"
MDROFF_PARAGRAPH=true
continue
fi
# Tables (Partially Supported)
if [[ "$line" =~ ^[[:space:]]*\| ]]; then
local raw_cells=()
local cells=()
local table_cell
line="$(sed 's/^[[:space:]]*|//; s/|[[:space:]]*$//' <<< "$line")"
# shellcheck disable=SC2206
IFS='|' raw_cells=($line)
for table_cell in "${raw_cells[@]}"; do
cells+=("$(mdroff:trim "$table_cell")")
done
if [[ "${cells[0]}" =~ ^[[:space:]]*-+[[:space:]]*$ ]]; then
# Calculate the column alignments.
local table_alignments=()
local table_cell
for table_cell in "${cells[@]}"; do
case "$table_cell" in
:-*:)
table_alignments+=('c')
;;
:-*)
table_alignments+=('l')
;;
*-:)
table_alignments+=('r')
;;
*)
table_alignments+=('l') # Unknown, but let's assume left.
esac
done
# Emit the table start and table header.
mdroff:emit table_start "${table_alignments[@]}"
mdroff:emit table_heading "${MDROFF_TABLE_HEADER[@]}"
MDROFF_TABLE_HEADER=()
continue
fi
if ! "$MDROFF_IN_TABLE"; then
MDROFF_IN_TABLE=true
MDROFF_TABLE_HEADER=("${cells[@]}")
else
if [[ "${#MDROFF_TABLE_HEADER[@]}" -ne 0 ]]; then
mdroff:emit table_heading "${MDROFF_TABLE_HEADER[@]}"
MDROFF_TABLE_HEADER=()
fi
mdroff:emit table_row "${cells[@]}"
fi
MDROFF_PARAGRAPH=true
continue
fi
mdroff:emit line "$line"
done < <(sed 's/<br *\/?>//')
}
# -----------------------------------------------------------------------------
# bat-extras:
# -----------------------------------------------------------------------------
mdroff:rewrite:h1() {
emitted_name=false
emitted_description=false
sed 's/^bat-extras: //' <<< "$1" | tr '[[:lower:]]' '[[:upper:]]'
}
mdroff:emit_hook:h2() {
case "$MDROFF_HEADING" in
"Installation") return ;;
"Issues?") return ;;
esac
mdroff:emit:h2 "$(tr '[[:lower:]]' '[[:upper:]]' <<< "$1")"
}
mdroff:emit_hook:line() {
if [[ "$MDROFF_HEADING_LEVEL" = "1" ]] && [[ "$emitted_name" != true ]]; then
emitted_name=true
printf ".SH NAME\n%s - %s\n" "$(sed 's/^bat-extras: //' <<< "$MDROFF_HEADING" | tr '[[:upper:]]' '[[:lower:]]')" "$1"
printf ".SH DESCRIPTION\n"
return
fi
case "$MDROFF_HEADING" in
"Installation") return ;;
"Issues?") return ;;
esac
mdroff:emit:line "$@"
}
# -----------------------------------------------------------------------------
# Main:
# -----------------------------------------------------------------------------
if [[ "${#BASH_SOURCE[@]}" -eq 1 ]]; then
case "$1" in
"") mdroff ;;
*) {
if ! [ -f "$1" ]; then
printf_err "%s: cannot find or read file %s\n" "$0" "$1"
exit 1;
fi
mdroff < "$1"
} ;;
esac
fi