-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmdlint_usils.sh
More file actions
372 lines (329 loc) · 8.28 KB
/
mdlint_usils.sh
File metadata and controls
372 lines (329 loc) · 8.28 KB
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
#!/usr/bin/env bash
mdlint.title_from_path() {
local file="$1"
local pattern="$2"
local resolve_dot_dir="$3"
local base filename dir1 dir2 title
base="$(basename -- "$file")"
filename="${base%.*}"
dir1="$(basename -- "$(dirname -- "$file")")"
dir2="$(basename -- "$(dirname -- "$(dirname -- "$file")")")"
if [[ -z "$pattern" ]]; then
if [[ "$filename" =~ ^([Rr][Ee][Aa][Dd][Mm][Ee])$ ]] && [[ -n "$dir2" && "$dir2" != "." && "$dir2" != "/" ]]; then
pattern="{dir2}/{dir}"
else
pattern="{dir}/{file}"
fi
fi
local rep_dir1="$dir1"
local rep_dir2="$dir2"
if [[ "$resolve_dot_dir" != "true" ]]; then
[[ "$rep_dir1" == "." ]] && rep_dir1=""
[[ "$rep_dir2" == "." ]] && rep_dir2=""
fi
title="$pattern"
title="${title//\{dir2\}/$rep_dir2}"
title="${title//\{dir\}/$rep_dir1}"
title="${title//\{file\}/$filename}"
title="${title//\// / }"
title="${title//_/ }"
title="${title//-/ }"
title="$(echo "$title" | sed -E 's/[[:space:]]+/ /g; s/^[[:space:]/]+//; s/[[:space:]/]+$//')"
printf '%s' "$title"
}
mdlint.path_from_input_line() {
local line="$1"
local path=""
[[ -z "$line" ]] && return 1
if [[ "$line" =~ ^(.+):[0-9]+([[:space:]]|:).* ]]; then
path="${BASH_REMATCH[1]}"
else
path="$line"
fi
path="$(echo "$path" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//')"
[[ -z "$path" ]] && return 1
printf '%s' "$path"
}
mdlint.fix.md041() {
# Algorithm:
# 1. Take file(s) reported to have MD041 error by `markdownlint`
# 2. If file contains an H1 title - move it to the top
# 3. If file doesn't contain an H1 title - either skip it or construct a title
local deps=(awk basename cmp dirname mktemp mv sed)
for d in "${deps[@]}"; do
command -v "$d" >/dev/null 2>&1 || {
echo "Error: dependency missing: $d" >&2
return 127 2>/dev/null || exit 127
}
done
local new_title=false
local new_title_pattern=""
local resolve_dot_dir=false
local files=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
cat <<- EOF
Usage: ${FUNCNAME[0]} [OPTIONS] [FILES...]
Auto-fix MD041 "First line is not title".
Options:
-h, --help Show help
-t, --new_title Attempt to construct a new title. If false, command skips files without existing H1.
--resolve-dot-dir When {dir}/{dir2} resolves to '.', use the real dirname instead of skipping it.
-T, --new_title_pattern Title pattern using tokens: {dir} (parent), {dir2} (grandparent), {file} (filename).
Default: {dir}/{file} (or {dir2}/{dir} for README when grandparent exists). Sets new_title=true.
Examples:
${FUNCNAME[0]} -t README.md docs/guide.md
${FUNCNAME[0]} -T "{dir2}/{dir}/{file}" docs/guide.md
markdownlint "**/*.md" --ignore node_modules 2>&1 | ${FUNCNAME[0]} -t
cat mdlint.report.MD041.paths.list | ${FUNCNAME[0]} -t
find . -name "*.md" -print0 | xargs -0 ${FUNCNAME[0]} -t
EOF
return 0
;;
-t|--new_title)
new_title=true
shift
;;
--resolve-dot-dir)
resolve_dot_dir=true
shift
;;
-T|--new_title_pattern)
new_title=true
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: --new_title_pattern requires a pattern like '{dir}/{file}'" >&2
return 1
fi
new_title_pattern="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Error: unknown option: $1" >&2
return 1
;;
*)
files+=("$1")
shift
;;
esac
done
if [[ $# -gt 0 ]]; then
files+=("$@")
fi
if [[ ${#files[@]} -eq 0 ]]; then
if [[ -t 0 ]]; then
echo "Error: no files provided" >&2
return 1
fi
while IFS= read -r line; do
local path
path="$(mdlint.path_from_input_line "$line")" || continue
files+=("$path")
done
fi
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
echo "Skip: not a file: $file" >&2
continue
fi
local title
if [[ "$new_title" == "true" ]]; then
title="$(mdlint.title_from_path "$file" "$new_title_pattern" "$resolve_dot_dir")" || return 1
fi
local tmp
tmp="$(mktemp)" || return 1
awk -v new_title="$new_title" -v title="$title" '
{
lines[NR]=$0
}
END {
n=NR
h1_start=0
h1_end=0
had_h1=0
for (i=1; i<=n; i++) {
if (lines[i] ~ /^#[[:space:]]+/) {
had_h1=1
h1_start=i
h1_end=i
break
}
if (i < n && lines[i+1] ~ /^[[:space:]]*=+[[:space:]]*$/) {
had_h1=1
h1_start=i
h1_end=i+1
break
}
}
has_body=0
for (i=1; i<=n; i++) {
if (i >= h1_start && i <= h1_end) continue
if (lines[i] ~ /[^[:space:]]/) {
has_body=1
break
}
}
if (h1_start == 1) {
for (i=1; i<=n; i++) print lines[i]
exit 0
}
if (h1_start == 0) {
if (new_title != "true") {
for (i=1; i<=n; i++) print lines[i]
print "__MDLINT_NO_HEADER__"
exit 0
}
print "# " title
if (has_body == 1) print ""
printed=0
for (i=1; i<=n; i++) {
if (!printed && lines[i] ~ /^[[:space:]]*$/) continue
print lines[i]
printed=1
}
exit 0
}
for (i=h1_start; i<=h1_end; i++) print lines[i]
if (has_body == 1) print ""
printed=0
for (i=1; i<=n; i++) {
if (i >= h1_start && i <= h1_end) continue
if (i == h1_end + 1 && lines[i] ~ /^[[:space:]]*$/) continue
if (!printed && lines[i] ~ /^[[:space:]]*$/) continue
print lines[i]
printed=1
}
}
' "$file" >"$tmp"
if grep -q "__MDLINT_NO_HEADER__" "$tmp"; then
sed -i '' -e '/__MDLINT_NO_HEADER__/d' "$tmp" 2>/dev/null || sed -i -e '/__MDLINT_NO_HEADER__/d' "$tmp"
if cmp -s "$file" "$tmp"; then
rm -f "$tmp"
echo "NO_HEADER: $file"
continue
fi
fi
if cmp -s "$file" "$tmp"; then
rm -f "$tmp"
echo "OK: $file"
continue
fi
mv "$tmp" "$file"
echo "Fixed: $file"
done
}
mdlint.fix.md025() {
# Algorithm:
# 1. Take file(s) reported to have MD025 error by `markdownlint`
# 2. Demote extra H1 headings by adding an extra '#'
# 3. Optionally demote the first H1 with --all/--from-first
local deps=(awk cmp mktemp mv sed)
for d in "${deps[@]}"; do
command -v "$d" >/dev/null 2>&1 || {
echo "Error: dependency missing: $d" >&2
return 127 2>/dev/null || exit 127
}
done
local demote_from_first=false
local files=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
cat <<- EOF
Usage: ${FUNCNAME[0]} [OPTIONS] [FILES...]
Auto-fix MD025 "Multiple top-level headings in the same document".
Options:
-h, --help Show help
--all Demote all H1 headings, including the first
--from-first Same as --all
Examples:
${FUNCNAME[0]} README.md docs/guide.md
${FUNCNAME[0]} --from-first README.md
markdownlint "**/*.md" --ignore node_modules 2>&1 | ${FUNCNAME[0]}
cat mdlint.report.MD025.paths.list | ${FUNCNAME[0]}
EOF
return 0
;;
--all|--from-first)
demote_from_first=true
shift
;;
--)
shift
break
;;
-*)
echo "Error: unknown option: $1" >&2
return 1
;;
*)
files+=("$1")
shift
;;
esac
done
if [[ $# -gt 0 ]]; then
files+=("$@")
fi
if [[ ${#files[@]} -eq 0 ]]; then
if [[ -t 0 ]]; then
echo "Error: no files provided" >&2
return 1
fi
while IFS= read -r line; do
local path
path="$(mdlint.path_from_input_line "$line")" || continue
files+=("$path")
done
fi
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
echo "Skip: not a file: $file" >&2
continue
fi
local tmp
tmp="$(mktemp)" || return 1
awk -v demote_from_first="$demote_from_first" '
{
lines[NR]=$0
}
END {
n=NR
h1_count=0
for (i=1; i<=n; i++) {
if (lines[i] ~ /^#[[:space:]]+/ && lines[i] !~ /^##[[:space:]]+/) {
h1_count++
if (demote_from_first == "true" || h1_count > 1) {
lines[i] = "#" lines[i]
}
continue
}
if (i < n && lines[i+1] ~ /^[[:space:]]*=+[[:space:]]*$/) {
h1_count++
if (demote_from_first == "true" || h1_count > 1) {
lines[i] = "## " lines[i]
lines[i+1] = "__MDLINT_DELETE_LINE__"
}
continue
}
}
for (i=1; i<=n; i++) {
if (lines[i] == "__MDLINT_DELETE_LINE__") continue
print lines[i]
}
}
' "$file" >"$tmp"
if cmp -s "$file" "$tmp"; then
rm -f "$tmp"
echo "OK: $file"
continue
fi
mv "$tmp" "$file"
echo "Fixed: $file"
done
}