forked from twitter/effectivescala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.sh
59 lines (52 loc) · 819 Bytes
/
toc.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
#!/bin/bash
tmp=/tmp/pre$$
trap "rm -f $tmp" 0 1 2
cat > $tmp
toc=/tmp/toc$$
trap "rm -f $toc" 0 1 2
awk '
function pr(){
if(!topic) return
printf "* **<a href=\"#%s\">%s</a>**", topic, topic
for(i=0; i<t; i++){
if(i==0) printf ": "
else printf ", "
printf "*<a href=\"#%s-%s\">%s</a>*", topic, topics[i], topics[i]
}
printf "\n"
}
/^## / {
pr()
t=0
topic=substr($0, 4, length($0)-3)
delete topics
}
/^### /{
topics[t++]=substr($0, 5, length($0)-4)
}
END{pr()}
' $tmp > $toc
awk '
/^## /{
topic=substr($0, 4, length($0)-3)
print "<a id=\"" topic "\" />"
print
next
}
/^### /{
subtopic=substr($0, 5, length($0)-4)
printf "<a id=\"%s-%s\" />\n", topic, subtopic
print
next
}
{ print $0 }
' $tmp > $tmp.1
mv $tmp.1 $tmp
ed $tmp >/dev/null <<EOF
1
/^\.TOC/r $toc
1
/^\.TOC/d
wq
EOF
cat $tmp