forked from the-tcpdump-group/tcpdump-htdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregen_html_pages.sh
executable file
·234 lines (207 loc) · 5.36 KB
/
regen_html_pages.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
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
#!/bin/sh
# This script generates (updates, but not commits) HTML pages for the
# www.tcpdump.org web-site.
#
# This script has been tested to work on the following systems:
#
# * Ubuntu Linux 18.04
# * Ubuntu Linux 20.04
# * Fedora Linux 30
# * Fedora Linux 32
# * Devuan Linux
# * FreeBSD 12
#
readonly HTML_HEAD='htmlsrc/_html_head.html'
readonly TOP_MENU='htmlsrc/_top_menu.html'
readonly BODY_HEADER='htmlsrc/_body_header.html'
readonly SIDEBAR='htmlsrc/_sidebar.html'
readonly BODY_FOOTER='htmlsrc/_body_footer.html'
substitute_page_title()
{
local readonly f="${1:?}"
local readonly b=$(basename "$f" .html)
local title
case "$b" in
security)
title='Security | '
;;
faq)
title='FAQ | '
;;
index|license)
title=''
;;
linktypes)
title='Link-Layer Header Types | '
;;
mirrors)
title='Mirror sites | '
;;
old_releases)
title='Old releases | '
;;
pcap)
title='Programming with pcap'
;;
related)
title='Related Projects | '
;;
broadcom-switch-tag)
title='Broadcom switch tag | '
;;
marvell-switch-tag)
title='Marvell switch tag | '
;;
LINKTYPE_*)
title="$b | "
;;
*)
echo "Internal error: cannot tell page title for $f" >&2
exit 10
esac
sed "s#%PAGE_TITLE%#${title}TCPDUMP/LIBPCAP public repository#"
}
# Instead of using absolute hyperlinks in all .html files use relative ones
# and amend them for the files under linktypes/ at the generation time. The
# advantage of this is that the user can open the resulting static files
# from their filesystem in a browser and confirm everything works as expected
# before committing the changes and deploying them to the web-server.
rewrite_URLs()
{
if [ "${1:?}" = "${1#linktypes/}" ] && [ "${1:?}" = "${1#manpages/}" ]; then
cat
else
sed 's#<link href="style.css#<link href="../style.css#' | \
sed 's#<img src="images/#<img src="../images/#' | \
sed 's#<a href="index.html#<a href="../index.html#' | \
sed 's#<a href="security.html#<a href="../security.html#' | \
sed 's#<a href="faq.html#<a href="../faq.html#' | \
sed 's#<a href="linktypes.html#<a href="../linktypes.html#' | \
sed 's#<a href="related.html#<a href="../related.html#' | \
sed 's#<a href="license.html#<a href="../license.html#' | \
sed 's#<a href="old_releases.html#<a href="../old_releases.html#' | \
sed 's#<a href="mirrors.html#<a href="../mirrors.html#'
fi
}
highlight_top_menu()
{
local readonly f="${1:?}"
sed "s#<li><a href=\"${f}\">#<li class=\"current_page_item\"><a href=\"${f}\">#"
}
print_html_page()
{
local readonly infile="${1:?}"
local show_sidebar
case $(basename "$infile" .html) in
security|faq|index|license|mirrors|related|old_releases)
show_sidebar='yes'
;;
linktypes|broadcom-switch-tag|marvell-switch-tag|pcap|LINKTYPE_*)
show_sidebar='no'
;;
*)
echo "Internal error: cannot tell if $infile should have sidebar" >&2
exit 11
;;
esac
cat <<ENDOFTEXT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Created by : Luis MartinGarcia <http://www.aldabaknocking.com>
Original design : "Collaboration" by Free CSS Templates <http://www.freecsstemplates.org>
Original license : Creative Commons Attribution 2.5 License
-->
<html>
<!-- HEAD -->
ENDOFTEXT
cat "$HTML_HEAD"
cat <<ENDOFTEXT
<!-- END OF HTML HEAD -->
<!-- BODY -->
<body>
<!-- TOP MENU -->
ENDOFTEXT
cat "$TOP_MENU"
cat <<ENDOFTEXT
<!-- END OF TOP MENU -->
<!-- PAGE HEADER -->
ENDOFTEXT
cat "$BODY_HEADER"
cat <<ENDOFTEXT
<!-- END OF PAGE HEADER -->
<!-- PAGE CONTENTS -->
<div id="page">
ENDOFTEXT
if [ "$show_sidebar" = 'yes' ]; then
cat <<ENDOFTEXT
<!-- RIGHT HAND SIDE PAGE CONTENTS -->
ENDOFTEXT
fi
cat "$infile"
if [ "$show_sidebar" = 'yes' ]; then
cat <<ENDOFTEXT
<!-- RIGHT HAND SIDE PAGE CONTENTS -->
ENDOFTEXT
fi
if [ "$show_sidebar" = 'yes' ]; then
cat <<ENDOFTEXT
<!-- LEFT SIDEBAR -->
ENDOFTEXT
cat "$SIDEBAR"
cat <<ENDOFTEXT
<!-- END OF LEFT SIDEBAR -->
ENDOFTEXT
fi
cat <<ENDOFTEXT
</div>
<!-- END OF PAGE CONTENTS -->
<!-- FOOTER -->
ENDOFTEXT
cat "$BODY_FOOTER"
cat <<ENDOFTEXT
<!-- END OF FOOTER -->
</body>
<!-- END OF HTML BODY -->
</html>
ENDOFTEXT
}
file_exists_in_repository()
{
git cat-file -e HEAD:"${1:?}" 2>/dev/null
}
file_differs_from_repository()
{
! git diff --quiet -- "${1:?}"
}
regenerate_pages()
{
local f_in f_out
for f_in in htmlsrc/[!_]*.html htmlsrc/linktypes/*.html htmlsrc/manpages/index.html; do
f_out="${f_in#htmlsrc/}"
file_exists_in_repository "$f_in" || echo "Warning: input file $f_in does not exist in git" >&2
if file_exists_in_repository "$f_out"; then
file_differs_from_repository "$f_out" && echo "Warning: unsaved changes to $f_out were lost" >&2
else
echo "Warning: output file $f_out does not exist in git" >&2
fi
print_html_page "$f_in" | \
substitute_page_title "$f_out" | \
highlight_top_menu "$f_out" | \
rewrite_URLs "$f_out" > "$f_out"
if [ $? -ne 0 ]; then
echo "Error: failed to overwrite output file $f_out" >&2
continue
fi
file_differs_from_repository "$f_out" && echo "Regenerated: $f_out"
done
return 0
}
which git >/dev/null 2>&1 || {
echo "git must be installed to proceed" >&2
exit 12
}
which sed >/dev/null 2>&1 || {
echo "sed must be installed to proceed" >&2
exit 13
}
regenerate_pages