-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathgenerate-toc.sh
executable file
·62 lines (53 loc) · 1.24 KB
/
generate-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
60
61
62
#!/usr/bin/env bash
cleanDirectoryName() {
# Remove leading number
# Replace dash with space
# Remove trailing '/'
# trim spaces
dirname=$(echo "$1" \
| sed 's/^[0-9]*//g' \
| sed 's/-/ /g' \
| sed 's/\/$//g' \
| xargs)
capitalize "$dirname"
}
cleanFileTitle() {
# Get title
# Remove formatting markdown characters
# Also trim spaces
head -n 1 "$1" | sed 's/[*#]//g' | xargs
}
capitalize() {
# Capitalize first letter
head=$(echo "$1" | cut -c1 | tr [a-z] [A-Z])
tail=$(echo "$1" | cut -c2-)
echo "$head$tail"
}
root=$(git rev-parse --show-toplevel)
docs="$root/docs"
pushd $docs > /dev/null
generate() {
echo "# Table Of Contents"
echo "*(Do NOT edit manually. Generated using generate-toc.sh)*"
echo ""
for dir in */; do
dirname=$(cleanDirectoryName $dir)
echo "## [$dirname]($dir)"
cd $dir > /dev/null
for doc in *.md; do
if [ "$doc" != "README.md" ]; then
filepath=$dir$doc
title=$(cleanFileTitle $doc)
echo "- [$title]($filepath)"
fi
done
cd ..
echo ""
done
echo ""
echo "---"
cat how-to-create-a-doc-file.md
echo ""
popd > /dev/null
}
generate > README.md