-
Notifications
You must be signed in to change notification settings - Fork 16
/
build-index.sh
executable file
·144 lines (118 loc) · 5.14 KB
/
build-index.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
#!/bin/bash
if [ "$1" == "toc" ]
then
buildtoc=true;
# >&2 echo Building Table of Contents...
else
buildtoc=false;
# >&2 echo Building Page List...
fi
if $buildtoc; then
echo "# OpenSoundControl.org Table of Contents"
echo
echo "OpenSoundControl.org table of contents (listing every section of every page),"
else
echo "# OpenSoundControl.org Page List"
echo
echo "OpenSoundControl.org page list (listing every page on the site),"
fi
echo "generated `date` by `whoami`"
tmpfile=/tmp/osc-org-index.txt
# Find all immediate subfolders (should be recursive!) containing at least one .md file
dirs=`ls -1 */*.md | awk -F / '{print $1}' | sort -df | uniq`
# Better way, to recursively find every dir that contains any .md file:
# dirs=`find . -name '*.md' -exec dirname {} \; | sort -u`
# but then we'd need to deal with multiple depths, which we don't yet.
# Start with the toplevel then all the subdirs (alphabetically)
for d in . $dirs; do
echo
echo
if [ "$d" != "." ]
then
# link to folder isn't useful yet
# echo "## [$d]($d)";
echo "## $d";
fi
for m in `ls -1 $d/*.md | sort -f` ; do
mhtml=`echo "$m" | rev | cut -c 3- | rev`html;
if [ "$d" == "." ]; then
mhtml=`echo $mhtml | cut -c 3-`;
fi
base=`basename $m .md`;
if [ "${base:0:15}" == "navigation-menu" ] || [ "$base" == "page-list" ] || [ "$base" == "toc" ]
then
# This file shouldn't be indexed
continue;
fi
# >&2 echo file $m is html $mhtml;
if $buildtoc; then
# All headings | strip out any legacy HTML anchors
egrep "^\#+ " $m | sed 's/\[\]{.*}//g' > $tmpfile;
else
# The "page list" = only H1 toplevel headings
# XXX this is a kludge! This requires each file to have exactly one H1
egrep "^\# " $m | head -1 > $tmpfile;
fi
# Make index or toc able to be output as rawtext
if [ "$1" == "rawtext" ]
then
# Raw text mode: Just the filenames and section titles
echo $m;
cat $tmpfile;
else
# Output Markdown with everything as links too:
echo
# thisFileWithLink=`echo "[$base]($mhtml): "`;
thisFileWithLink=`echo "[$base]($mhtml)"`;
if $buildtoc; then
# Full ToC has a bullet list for each .md file corresponding to heading structure
# echo "[$mhtml]($mhtml)" contents:
echo "### ${thisFileWithLink}"
echo
# XXX these two different while loops could be refactored a bit
while IFS= read -r line || [[ -n $line ]]; do
# bug: this assumes sharp signs occur only at the beginning of a line
# sharpsigns=`echo $line | tr -Cd \#`;
sharpsigns=`echo $line | sed 's|\(^#*\).*$|\1|g'`
mdListIndentation=`echo $sharpsigns | cut -c2- | sed 's/#/ /g'`;
text=`echo $line | tr -d \# | cut -c2-` ;
# Unix version of Markdown's rules for making header lines
# automatically be HTML link targets: convert to lowercase,
# remove all punctuation (except minus signs), convert spaces
# to minus signs, and consolidate repeated minus signs to a single one:
link=`echo $text | \
tr '[:upper:]' '[:lower:]' | \
tr '-' ' ' | \
tr -d '[:punct:]' | \
tr ' ' '-' | \
tr -s '-'`;
# was sed 's/--*/-/g'` ;
printf "%s* [%s](%s#%s)\n" "$mdListIndentation" "$text" "$mhtml" "$link";
done < $tmpfile;
else
# Page List has one line per .md file
# kludge to solve the "nerdy version" problem
if [ "$d" != "implementations" ] && [ "$d" != "publications" ]
then
# printf instead of echo because
# https://apple.stackexchange.com/questions/173836/why-echo-n-doesnt-work-in-this-script-on-mac-terminal
printf "${thisFileWithLink}: "
fi
# But we need to see publications' year because it's in that order because the filename begins with year
if [ "$d" == "publications" ]
then
# Luckily it has to be the first 4 characters of the filename
YEAR=`echo $base | cut -c 1-4`
printf "$YEAR, "
fi
while IFS= read -r line || [[ -n $line ]]; do
text=`echo $line | tr -d \# | cut -c2-` ;
link=`echo $text | \
tr '[:upper:]' '[:lower:]' | tr -d '[:punct:]' | \
tr ' ' '-' | sed 's/--*/-/g'`
printf "[%s](%s)\n" "$text" "$mhtml";
done < $tmpfile;
fi
fi
done
done