-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate.sh
executable file
·382 lines (332 loc) · 8.44 KB
/
generate.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/bin/sh
#
# (P) & (C) 2003-2017 by Dr. Peter Bieringer <pb@bieringer.de>
#
# Generator script
#
# $Id: $
#
# Requires: htmldoc recode docbook-utils-pdf
#
# Changelog
#
# 20020117/PB: review
# 20020128/PB: change PDF generation to LDP conform one, PS is still not LDP conform
# 20070401/PB: disable sgmlfixer (no longer needed)
# add support for XML file, replace nsgmls by onsgmls
# 20090214/PB: remove </?dummy> tag from SGML, onsgmls don't like it
# 20090523/PB: extend required binary check
# 20091220/PB: catch recode problem
# 20110508/PB: force UTF-8 as input format to fix mixed charset problems in HTML
# renice whole script
# support also ldp.dsl stored in same directory as the script
# 20110509/PB: add charset meta header on each html page to force UTF-8
# 20161215/PB: remove VGWort URL appender
# 20170303/PB: add stylesheet hacks based on input from Mike Wright
loglevel=6
# parse options
while getopts "ql:" opt; do
case $opt in
q)
loglevel=4
;;
l)
loglevel=$OPTARG
echo "INFO : set loglevel to: $loglevel"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
file_input="Linux+IPv6-HOWTO.sgml"
#file_input="Linux+IPv6-HOWTO.xml"
else
file_input="$1"
fi
if echo "$file_input" | grep -q ".sgml$"; then
# ok, SGML
true
elif echo "$file_input" | grep -q ".xml$"; then
# ok, XML
true
else
echo "ERR: file is not a SGML/XML file: $file_input"
exit 1
fi
if head -1 "$file_input" |grep -q DOCTYPE ; then
doctype="SGML"
elif head -1 "$file_input" |grep -q "xml version" ; then
doctype="XML"
else
echo "ERR: file is not a SGML file: $file_input"
exit 1
fi
echo "Used SGML file: $file_input"
file_base="${file_input%.*}"
ONSGMLS="/usr/bin/onsgmls"
JADE="/usr/bin/jade"
DB2PS="/usr/bin/db2ps"
DB2PDF="/usr/bin/db2pdf"
LDP_PRINT="/usr/local/bin/ldp_print"
PS2ASCII="/usr/bin/ps2ascii"
RECODE="/usr/bin/recode"
HTMLDOC="/usr/bin/htmldoc"
#LDP_PDFPS="yes"
checklist_bin="ONSGMLS JADE DB2PS DB2PDF PS2ASCII RECODE HTMLDOC"
if [ "$LDP_PDFPS" = "yes" ]; then
checklist_bin="$checklist_bin LDP_PRINT"
fi
file_ps="$file_base.ps"
file_pdf="$file_base.pdf"
file_txt="$file_base.txt"
file_html="$file_base.html"
file_ldpdsl="/usr/local/share/sgml/dsssl/ldp.dsl"
if [ ! -f $file_ldpdsl ]; then
# try local stored one
file_ldpdsl="`dirname $0`"
if [ "$file_ldpdsl" = "." ]; then
file_ldpdsl="`pwd`"
fi
file_ldpdsl="$file_ldpdsl/ldp.dsl"
fi
file_xmldcl="/usr/share/sgml/xml.dcl"
dir_dssslstylesheets="/usr/share/sgml/docbook/dsssl-stylesheets"
if [ ! -f "$file_ldpdsl" ]; then
echo "ERR: Missing DSL file: $file_ldpdsl"
exit 1
fi
if [ ! -f $file_input ]; then
echo "ERR: Missing SGML file, perhaps export DocBook of LyX won't work"
exit 1
fi
# look for required files
for f in $file_ldpdsl $file_xmldcl; do
if [ ! -e $f ]; then
echo "Missing file: $f"
exit 1
fi
done
# look for required binaries
for name in $checklist_bin; do
f="${!name}"
if [ ! -e $f ]; then
echo "Missing file: $f ($name)"
exit 1
fi
if [ ! -x $f ]; then
echo "Cannot executue: $f ($name)"
exit 1
fi
done
# run sgmlfix
#if [ -e ./runsgmlfix.sh ]; then
# ./runsgmlfix.sh "$file_input"
#else
# echo "WARN: cannot execute 'runsgmlfix.sh'"
#fi
export SP_ENCODING=UTF-8
export SP_CHARSET_FIXED=yes
## Functions
validate_sgml() {
# remove tags <dummy>, </dummy>
perl -pi -e 's|</?dummy>||g' "$file_input"
echo "INFO : Validate SGML/XML code '$file_input'"
if [ "$doctype" = "XML" ]; then
local options="$file_xmldcl"
fi
[ $loglevel -ge 7 ] && set -x
$ONSGMLS -s $options $file_input
local retval=$?
[ $loglevel -ge 7 ] && set +x
if [ $retval -gt 0 ]; then
echo "ERR: Validation results in errors!"
return 1
else
echo "INFO : Validation was successfully"
fi
}
create_html_multipage() {
echo "INFO : Create HTML multipages"
if [ ! -d "$file_base" ]; then
mkdir "$file_base" || exit 1
fi
pushd "$file_base" >/dev/null || exit 1
rm -f *
[ $loglevel -ge 7 ] && set -x
$JADE -t sgml -i html -D $dir_dssslstylesheets -d "${file_ldpdsl}#html" ../$file_input
local retval=$?
[ $loglevel -ge 7 ] && set +x
style_multipage=$(cat <<END
><style> /* separate page version */
body {
width:800px;
margin-left:5%;
font:normal normal 10pt/1.6 sans-serif;
}
h1,h2,h3,h4 { font-weight:bold; }
h1 { font-size:16pt; }
h2 { font-size:14pt; }
h3 { font-size:12pt; }
h4 { font-size:12pt; }
td { font-size: 8pt; } /* general table data */
.navheader th,
.navfooter th { font-size:14pt; }
.navheader td,
.navfooter td { font-size:12pt; }
</style
END
)
style_multipage=$(echo "$style_multipage" | sed ':a;N;$!ba;s/\n/\\n/g')
perl -pi -e "s#^(><HEAD)\$#\1\n$style_multipage#g" *.html
local r=$?
if [ $r -ne 0 ]; then
retval=$?
fi
popd >/dev/null
return $retval
}
create_html_singlepage() {
echo "INFO : Create HTML singlepage '$file_html' from '$file_input'"
[ $loglevel -ge 7 ] && set -x
$JADE -t sgml -i html -V nochunks -d "${file_ldpdsl}#html" $file_input >$file_html
local retval=$?
[ $loglevel -ge 7 ] && set +x
style_singlepage=$(cat <<END
><style> /* unified version */
body {
width:800px;
margin-left:5%;
font:normal normal 10pt/1.6 sans-serif;
}
</style
END
)
style_singlepage=$(echo "$style_singlepage" | sed ':a;N;$!ba;s/\n/\\n/g')
# add inline stylesheet
perl -pi -e "s#^(><HEAD)\$#\1\n$style_singlepage#g" $file_html
#perl -pi -e 's#^(><HEAD)$#$1\n><style>.book{ width:640px; padding-left:5%; font:normal normal 10pt/1.6 sans-serif;}</style#' $file_html
local r=$?
if [ $r -ne 0 ]; then
retval=$?
fi
if [ $retval -eq 0 ]; then
echo "INFO : Create HTML singlepage - done"
else
echo "ERR: Create HTML singlepage - an error occurs!"
fi
return $retval
}
create_rtf() {
echo "INFO : Create RTF file '$file_rtf'"
[ $loglevel -ge 7 ] && set -x
$JADE -t rtf -d ${file_ldpdsl} $file_input
local retval=$?
[ $loglevel -ge 7 ] && set +x
if [ $retval -eq 0 ]; then
echo "INFO : Create RTF file - done"
else
echo "ERR: Create RTF file - an error occurs!"
fi
return $retval
}
create_ps() {
echo "INFO : Create PS file '$file_ps'"
[ $loglevel -ge 7 ] && set -x
$DB2PS --dsl ${file_ldpdsl} $file_input
local retval=$?
[ $loglevel -ge 7 ] && set +x
if [ $retval -eq 0 ]; then
echo "INFO : Create PS file - done"
else
echo "ERR: Create PS file - an error occurs!"
fi
return $retval
}
create_pdf() {
if [ "$LDP_PDFPS" = "yes" ]; then
# Use LDP conform mechanism
echo "INFO : Create PDF file (LDP conform) '$file_pdf' from HTML file '$file_html'"
if [ $file_html -ot $file_input ]; then
echo "ERR: Create PDF file - needed single page HTML file '$file_html' is older than original '$file_input'"
return 1
fi
[ $loglevel -ge 7 ] && set -x
$LDP_PRINT $file_html
local retval=$?
[ $loglevel -ge 7 ] && set +x
else
echo "INFO : Create PDF file (NOT LDP conform) '$file_pdf'"
[ $loglevel -ge 7 ] && set -x
$DB2PDF --dsl ${file_ldpdsl} $file_input
local retval=$?
[ $loglevel -ge 7 ] && set +x
fi
if [ $retval -eq 0 ]; then
echo "INFO : Create PDF file - done"
else
echo "ERR: Create PDF file - an error occurs!"
fi
return $retval
}
create_txt() {
echo "INFO : Create TXT file '$file_txt' from PS file '$file_ps'"
[ -f $file_txt ] && rm $file_txt
if [ -f $file_ps ]; then
echo "INFO : Create TXT file '$file_txt'"
[ $loglevel -ge 7 ] && set -x
$PS2ASCII $file_ps > $file_txt
local retval=$?
[ $loglevel -ge 7 ] && set +x
else
echo "ERR: Cannot create TXT because of missing PS file"
fi
if [ $retval -eq 0 ]; then
echo "INFO : Create TXT file - done"
else
echo "ERR: Create TXT file - an error occurs!"
fi
return $retval
}
### Main
validate_sgml
[ $? -ne 0 ] && exit 1
create_html_multipage
if [ $? -ne 0 ]; then
echo "ERROR : create_html_multipage was not successful"
exit 1
fi
create_html_singlepage
if [ $? -ne 0 ]; then
echo "ERROR : create_html_singlepage was not successful"
exit 1
fi
create_pdf
if [ $? -ne 0 ]; then
echo "ERROR : create_pdf was not successful"
exit 1
fi
#create_ps
#[ $? -ne 0 ] && exit 1
#create_txt
#[ $? -ne 0 ] && exit 1
#create_rtf
#[ $? -ne 0 ] && exit 1
## Add VGWort URL
#if [ -x ./adjust-html-vgwort.sh ]; then
# case $file_input in
# 'Linux+IPv6-HOWTO.sgml')
# echo "NOTICE: add vgwort URL"
# ./adjust-html-vgwort.sh en
# ;;
# 'Linux+IPv6-HOWTO.de.sgml')
# echo "NOTICE: add vgwort URL"
# ./adjust-html-vgwort.sh de
# ;;
# esac
#else
# echo "NOTICE : can't add vgwort URL (missing adjust-html-vgwort.sh)"
#fi
exit 0