-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_anchor.sh
executable file
·79 lines (72 loc) · 1.26 KB
/
add_anchor.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
#!/usr/bin/env bash
# Add Markdown anchors to all Heading elements (only level 1 and level 2)
# The "Heading Alternate Syntax" must have at least 4 = or - characters otherwise the anchor is not added
if [ $# -eq 0 ]
then
echo Missing file parameter
exit 1
fi
gawk '
func toAnchor(str) {
// is already link
if (str ~ /^\s?\[/) {
return str;
}
anchor = tolower(str);
gsub(/[^a-z0-9]/, "_", anchor);
gsub(/_+$/, "", anchor);
return "[" str "](#" anchor ")";
}
func flush_buffer() {
# --line_index;
# line = lines[line_index];
# delete lines[line_index];
for (i = 0; i < line_index; i++) {
print lines[i];
}
line_index = 0;
}
func isHeaderAlreadySkipped(line) {
if (header_read) {
return 0;
}
print line;
if (line == "---") {
if (inside_header) {
header_read=1;
} else {
inside_header = 1;
}
}
return 1;
}
BEGIN {
header_read = 0;
inside_header = 0;
line_index = 0;
}
END {
flush_buffer();
}
/^(##?)/ {
if (!inside_header) {
flush_buffer();
line=$0;
gsub(/^#+/, "", line);
print toAnchor(line);
}
}
/^(====+)|(----+)/ {
--line_index;
last = lines[line_index];
delete lines[line_index];
flush_buffer();
print toAnchor(last);
}
{
if (!isHeaderAlreadySkipped($0)) {
lines[line_index++] = $0;
}
}' \
$1 >$1.bak
mv $1.bak $1