-
Notifications
You must be signed in to change notification settings - Fork 50
/
regen_html_pages.sh
executable file
·273 lines (245 loc) · 6.52 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/sh -e
# Copyright (c) 2020 The TCPDUMP project
# All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# 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:
#
# * AIX 7
# * FreeBSD
# * GNU/Linux
# * Haiku
# * illumos
# * NetBSD
# * OpenBSD
# * Solaris 11
#
HTML_HEAD='htmlsrc/_html_head.html'
TOP_MENU='htmlsrc/_top_menu.html'
BODY_HEADER='htmlsrc/_body_header.html'
SIDEBAR='htmlsrc/_sidebar.html'
BODY_FOOTER='htmlsrc/_body_footer.html'
: "${SED:=sed}"
substitute_page_title()
{
basename=$(basename "${1:?}" .html)
case "$basename" in
autoindex_header)
cat
return
;;
security)
title='Security | '
;;
faq)
title='FAQ | '
;;
index)
title='Home | '
;;
linktypes)
title='Link-layer header types | '
;;
old_releases)
title='Old releases | '
;;
pcap)
title='Programming with pcap | '
;;
wpcap)
title='Information on WinPcap and WinDump | '
;;
libpcap-module-HOWTO)
title='How to write a libpcap module | '
;;
related)
title='See also | '
;;
broadcom-switch-tag)
title='Broadcom switch tag | '
;;
marvell-switch-tag)
title='Marvell switch tag | '
;;
netanalyzer-header)
title='netANALYZER header | '
;;
season-of-docs)
title='Season of Docs | '
;;
ci)
title='Continuous integration | '
;;
LINKTYPE_*)
title="$basename | "
;;
*)
echo "Internal error: cannot tell page title for $1" >&2
exit 10
esac
"$SED" "s#%PAGE_TITLE%#${title}TCPDUMP \& LIBPCAP#"
}
# 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/}" ]; then
"$SED" 's#\(<link href="\)\(images/\)#\1../\2#' |
"$SED" 's#\(<link href="\)\(style.css\)#\1../\2#' |
"$SED" 's#\(<img src="\)\(images/\)#\1../\2#' |
"$SED" 's#\(<a href="\)\(manpages/\)#\1../\2#' |
"$SED" 's#\(<a href="\)\(bpfexam/\)#\1../\2#' |
"$SED" 's#\(<a href="\)\([a-z_-]\{1,\}.html\)#\1../\2#'
elif [ "$1" = autoindex_header.html ]; then
"$SED" 's#\(<img src="\)\(images/\)#\1/\2#' |
"$SED" 's#\(<a href="\)\([a-z]\{1,\}/\)#\1/\2#' |
"$SED" 's#\(<a href="\)\([a-z_-]\{1,\}.html\)#\1/\2#'
else
cat
fi
}
highlight_top_menu()
{
"$SED" "s#<li><a href=\"${1:?}\">#<li class=\"current_page_item\"><a href=\"${1}\">#"
}
print_html_page()
{
infile="${1:?}"
case $(basename "$infile" .html) in
_top_menu)
cat "$infile" "$BODY_HEADER"
return
;;
index)
show_sidebar='yes'
;;
*)
show_sidebar='no'
;;
esac
cat <<ENDOFTEXT
<!DOCTYPE html>
<html lang="en">
<!-- HEAD -->
$(cat "$HTML_HEAD")
<!-- END OF HTML HEAD -->
<!-- BODY -->
<body>
<!-- TOP MENU -->
$(cat "$TOP_MENU")
<!-- END OF TOP MENU -->
<!-- PAGE HEADER -->
$(cat "$BODY_HEADER")
<!-- 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 -->
$(cat "$SIDEBAR")
<!-- END OF LEFT SIDEBAR -->
ENDOFTEXT
fi
cat <<ENDOFTEXT
</div>
<!-- END OF PAGE CONTENTS -->
<!-- FOOTER -->
$(cat "$BODY_FOOTER")
<!-- 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_page()
{
f_in=${1:?}
f_out=${2:?}
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
# None of the functions below read from $f_out, they only need to know
# what the filename is.
# shellcheck disable=SC2094
print_html_page "$f_in" |
substitute_page_title "$f_out" |
highlight_top_menu "$f_out" |
rewrite_URLs "$f_out" > "$f_out"
# The pipeline is too long to fit into the "if" nicely.
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "Error: failed to overwrite output file $f_out" >&2
return 1
fi
file_differs_from_repository "$f_out" && echo "Regenerated: $f_out"
return 0
}
regenerate_pages()
{
for f_in in htmlsrc/[!_]*.html htmlsrc/linktypes/*.html; do
# Skip editor backup files.
[ "$f_in" != "${f_in#\~}" ] && continue
regenerate_page "$f_in" "${f_in#htmlsrc/}" || return 1
done
regenerate_page "$TOP_MENU" autoindex_header.html
}
command -v git >/dev/null 2>&1 || {
echo "git must be installed to proceed" >&2
exit 12
}
command -v "$SED" >/dev/null 2>&1 || {
echo "sed must be installed to proceed" >&2
exit 13
}
regenerate_pages