forked from OpenIndiana/oi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makepdf.sh
executable file
·648 lines (526 loc) · 18.9 KB
/
makepdf.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#!/usr/bin/env bash
##############################################################################
# makepdf.sh: A simple bash script to produce pdfs for OpenIndiana Docs
#
# Copyright (C) 2017 Benny Lyons: [email protected]
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at https://illumos.org/license/
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at https://illumos.org/license/
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Contributor(s):
#
#
# This variable holds all supported formats
#
SUPPORTED_FORMATS="pdf epub"
#
# If you would top add a subdirectory with files that should be built, add the
# subdirectory here; or use the -d option
#
# Processing these directories in docs by default:
# books - Needs HTML in markdown to be fixed, so do not include
# contrib
# dev
# handbook
# handbook/community
# misc
PROCESS_THESE_DIRECTORIES="contrib dev handbook handbook/community misc"
help()
{
cat<<EOF
Usage: $0 <options>
Simple script to currently produce pdf and epub formats for
OpenIndiana Docs. The epub format is experimental.
OPTIONS
-d <subdir> convert all files in this subdirectory.
Only all files in subdir are converted, no files in
subdirs of subdir are converted.
-f <filename> only convert filename
Default: convert everything
-h this help
-t <pdf || epub> output type either pdf or epub
Default: pdf
-s <web || opensolaris> output style either web or opensolaris (only applicable to pdf)
Default: web
-v verbose provides more verbose output
For more help, rerun with -vh
EOF
}
help_verbose()
{
cat<<EOF
Usage: $0 <options>
Simple script to produce pdf and epub formats for OpenIndiana Docs
The epub format is experimental.
Without any options, all files ending in .md in the direcxtories listed
in the variable PROCESS_THESE_DIRECTORIES will be processed.
All files produced will be placed in a new directory denoted
according to the output format, i.e., ./pdf or ./epub in the top-level
directory.
REQUIREMENTS
On a Debian system, you must install the following packages:
$ sudo pip install pandoc
$ sudo pip install mkdocs-pandoc
$ sudo apt-get install texlive-full
$ sudo apt-get install texlive-xetex
OPTIONS
-d <subdir> convert all files in this subdirectory.
Only all files in subdir are converted, no files in
subdirs of subdir are converted.
-f <filename> only convert filename
Default: convert everything
-h this help
-t <pdf || epub> output type either pdf or epub
Default: pdf
-s <web || opensolaris> 2 different styles are currently supported
for pdf: web or opensolaris.
Default: web
-v verbose output
ENVIRONMENT
PROCESS_THESE_DIRECTORIES this variable coontains a list of directories.
Running $0 without any options will convert
all files ending in .md in these directories.
EXAMPLES
Convert all files in the contrib sub-dir:
makepdf.sh -d contrib
Convert getting-started.md:
makepdf.sh -f ./docs/contrib/getting-started.md
Produce epub with extra output:
makepdf.sh -vt epub -f ./docs/contrib/getting-started.md
EOF
}
get_file_basename ()
{
this_file=$1
this_file="${this_file##*/}"
echo "${this_file%.*}"
}
get_file_ext ()
{
this_file=$1
echo "${this_file##*.}"
}
get_file_path()
{
this_file=$1
echo $(dirname "$infile")
}
make_dir()
{
this_path="$1"
if [ ! -z "$this_path" ]; then
if [ ! -d $this_path ]; then
mkdir -p $this_path
if [ ! -d "$out_path" ]; then
echo >&2 "ERROR: cannot create directory at: "$this_path
exit 1
fi
fi
fi
}
get_root()
{
root_dir=""
root_dir=$(pwd)
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not get current path"
exit $?
fi
}
# Currently relative paths are specified in some documents
# to load Openindiana.png. The relative paths appear to
# take precedence over --resource-path option to pandoc
# As a workaround we add softlinks
do_workaround()
{
if [ ! -f $root_dir/Openindiana.png ]; then
$(ln -s $root_dir/docs/Openindiana.png $root_dir/Openindiana.png)
fi
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not generate link"
exit $?
fi
if [ ! -f $root_dir/docs/handbook/Openindiana.png ]; then
$(ln -s $root_dir/docs/Openindiana.png $root_dir/docs/handbook/Openindiana.png)
fi
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not generate link"
exit $?
fi
}
undo_workaround()
{
$(rm $root_dir/Openindiana.png)
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not remove link"
exit $?
fi
$(rm $root_dir/docs/handbook/Openindiana.png)
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not remove link"
exit $?
fi
}
clean_up()
{
undo_workaround
}
get_full_path_outputfile()
{
input_basename="$1"
this_top_level_dir="$2"
this_out_dir_stub="$3"
this_format="$4"
local prefix="./docs/"
local this_outdir=""
if [ -z "$input_basename" ]; then
echo "Error: severe developer error: input_basename"
exit 1
fi
if [ -z "$this_top_level_dir" ]; then
echo "top_level_dir: "$this_top_level_dir
echo "Error: severe developer error: top-level-dir not set"
exit 1
fi
if [ -z "$this_out_dir_stub" ]; then
echo "Error: severe developer error: this_out_dir_stub not set"
exit 1
fi
if [ -z "$this_format" ]; then
echo "Error: severe developer error: this_format not set"
exit 1
fi
this_out_dir_stub_rm_prefix=${this_out_dir_stub#"$prefix"}
this_outdir="$this_top_level_dir"/"$format"/"$this_out_dir_stub_rm_prefix"
make_dir "$this_outdir"
out_full_path_outputfile=$this_outdir"/"$input_basename"."$format
}
#
# pandoc 2.8 was released on 2019-11-22 which is the first
# version to support LaTex in the header-includes variable
# For backward compatibility, we'd like to support older
# pandoc versions, but would prefer new features
is_pandoc_version_new()
{
pandoc_version_match="^pandoc ([0-9]+)\.([0-9]+).*$"
if [[ $(pandoc --version) =~ $pandoc_version_match ]]; then
if [ "${BASH_REMATCH[1]}" -gt "2" ]; then
echo "true"
elif [ "${BASH_REMATCH[1]}" -eq "2" ] && [ "${BASH_REMATCH[2]}" -ge "8" ]; then
echo "true"
else
echo "false"
fi
else
echo "false"
fi
}
do_conversion_epub()
{
this_basename="$1"
the_output="$2"
this_format="$3"
if [ "$this_format" != "epub" ]; then
echo >&2 -n "SERIOUS ERROR: trying to process for epub, but "
echo >&2 "requested format is not epub"
exit 1
fi
this_title=$this_basename
$(pandoc -t epub --toc \
--epub-cover-image=../Openindiana.png \
-f markdown+grid_tables+table_captions \
--metadata title="$this_title" \
--metadata-file $OLDPWD/pandoc/config-sb.yaml \
--lua-filter $OLDPWD/pandoc/filter-sb.lua \
-o $the_output $this_basename".md" --pdf-engine=xelatex)
if [ "$?" -ne "0" ]; then
echo "..." $root_dir
echo "ERROR: severe error from poandoc"
exit $?
fi
}
do_conversion_with_older_pandoc()
{
this_basename="$1"
this_output="$2"
this_format="$3"
if [ "$this_format" != "pdf" ]; then
echo >&2 -n "SERIOUS ERROR: trying to process for pdf using"
echo >&2 "older pandoc, but requested format is not pdf"
exit 1
fi
#get_full_path_outputfile "$this_basename" "$this_format"
$(pandoc --toc -f markdown+grid_tables+table_captions \
-o $this_output $this_basename".md" \
--pdf-engine=xelatex)
}
do_conversion_pdf()
{
this_basename="$1"
this_output="$2"
this_format="$3"
this_style="$4"
if [ "$this_format" != "pdf" ]; then
echo >&2 -n "SERIOUS ERROR: trying to process for pdf, but "
echo >&2 "requested format is not pdf"
exit 1
fi
if [ "$this_style" == "opensolaris" ]; then
$(pandoc --pdf-engine=xelatex \
--lua-filter $OLDPWD/pandoc/filter-sb.lua \
--metadata-file $OLDPWD/pandoc/config-sb.yaml \
-o $this_output $this_basename".md")
elif [ "$this_style" == "web" ]; then
$(pandoc --pdf-engine=xelatex \
--lua-filter $OLDPWD/pandoc/filter-web.lua \
--metadata-file $OLDPWD/pandoc/config-web.yaml \
-o $this_output $this_basename".md")
else
echo "ERROR: can only process one of these styles: opensolaris or web"
exit 1
fi
}
do_conversion()
{
input_basename="$1"
this_top_level_dir="$2"
this_out_dir_stub="$3"
format="$4"
style="$5"
local this_outpath=""
get_full_path_outputfile "$input_basename" "$this_top_level_dir" \
"$this_out_dir_stub" "$format"
if [ -z "$out_full_path_outputfile" ]; then
echo "Error: severe developer error: out_full_path_outputfile not set"
exit 1
fi
if [ "$format" == "epub" ]; then
do_conversion_epub "$input_basename" "$out_full_path_outputfile" \
"$format"
elif [ "$format" == "pdf" ]; then
if [ "$(is_pandoc_version_new)" == "false" ]; then
# This call for backward compatibility
do_conversion_with_older_pandoc "$input_basename" \
"$out_full_path_outputfile" \
"$format"
else
do_conversion_pdf "$input_basename" \
"$out_full_path_outputfile" \
"$format" "$style"
fi
fi
}
# This function sets is_format_valid_result = True || False
# True: users select a valid format
# False: users select an invalid format
is_format_valid()
{
input_format=$1
is_format_valid_result="False"
for i in $SUPPORTED_FORMATS ; do
if [ "${i}" == "$input_format" ]; then
is_format_valid_result="True"
fi
done
}
is_dir_valid()
{
input_dir="$1"
is_dir_valid_result="False"
if [ -d "$indir" ]; then
is_dir_valid_result="True"
fi
}
make_out_toplevel_dir()
{
the_output_format=$1
the_output_dir=$2
if [ -z "$the_output_dir" ]; then
echo "Error: severe developer error: top-level-dir not set: the_output_dir"
exit 1
fi
is_format_valid $the_output_format
if [ "$is_format_valid_result" == "False" ]; then
echo >&2 "ERROR: -t only supports the following formats: "
for i in $SUPPORTED_FORMATS ; do
echo " "$i
done
exit 1
fi
out_path="$the_output_dir""/""${the_output_format}/"
mkdir -p $out_path
if [ ! -d "$out_path" ]; then
echo >&2 "ERROR: cannot create directory at: "$out_path
exit 1
fi
}
main ()
{
#
# Command Line Options
#
optverbose="False"
opthelp="False"
while getopts "hd:f:t:s:v" opt; do
case $opt in
d)
indir=$OPTARG
;;
f)
infile=$OPTARG
;;
h)
opthelp="True"
;;
t)
outformat=$OPTARG
;;
s)
outstyle=$OPTARG
;;
v)
optverbose="True"
;;
\?)
echo "Don't know this option"
exit 0
;;
esac
done
if [ "$opthelp" == "True" ]; then
if [ "$optverbose" == "True" ]; then
help_verbose
else
help
fi
exit 0
fi
if [[ -n "$indir" && -n "$infile" ]]; then
echo "ERROR: specify -d OR -f, but not both"
exit 1
fi
if [ ! -n "$outformat" ]; then
outformat="pdf" # Default format
fi
is_format_valid $outformat
if [ "$is_format_valid_result" == "False" ]; then
echo >&2 "ERROR: -t only supports the following formats: "
for i in $SUPPORTED_FORMATS ; do
echo " "$i
done
exit 1
fi
if [ ! -n "$outstyle" ]; then
outstyle="web" # Default style
elif [ "$outstyle" != "web" ] && [ "$outstyle" != "opensolaris" ] ; then
echo >&2 "ERROR: -s can only be web or opensolaris"
exit 1
fi
# Required packages ok?
type pandoc >/dev/null 2>&1 || {
echo >&2 "ERROR: require package pandoc, please install pandoc."
exit 1; }
# Older versions of pandoc work, but we'd prefer the newer
# features.
# Issue a warning if an older version of pandoc is in use
if [ "$(is_pandoc_version_new)" == "false" ]; then
echo "WARNING: you are using an older version of pandoc"
fi
# Get and set top-level dir
get_root
# workaround
do_workaround
#
# -f <filename>
# Only a single file
#
if [ -n "$infile" ]; then
# File must be present
if [ -f "$infile" ]; then
this_path=$(get_file_path $infile)
file_basename=$(get_file_basename $infile)
make_out_toplevel_dir "$outformat" "$root_dir"
cd $this_path
do_conversion $file_basename $root_dir $this_path $outformat $outstyle
else
printf "\n"
echo "ERROR: cannot find file: " $infile
clean_up
exit 1
fi
clean_up
cd - > /dev/null
exit 0
fi
#
# -d <subdirectory> or all directories
# An entire subdirectory
#
if [ -n "$indir" ]; then
is_dir_valid "$indir"
if [ "$is_dir_valid_result" == "False" ]; then
echo "ERROR: not a directory: "$indir
echo " The argument to -d must be a directory"
clean_up
exit 1
else
PROCESS_THESE_DIRECTORIES="$indir"
fi
fi
for in_dir in $PROCESS_THESE_DIRECTORIES; do
if [ "$optverbose" == "True" ]; then
echo "====Processing directory: $in_dir ===="
fi
# Directory must be present
if [ -d "$in_dir" ]; then
this_path="$in_dir"
elif [ -d "./docs/$in_dir" ]; then
this_path="./docs/$in_dir"
else
printf "\n"
echo "ERROR: cannot find directory: " $in_dir
clean_up
exit 1
fi
make_out_toplevel_dir "$outformat" "$root_dir"
if [ "$optverbose" == "True" ]; then
echo " Writing output to this directory: " $out_path$in_dir
fi
infiles=$(ls "$this_path")
cd $this_path
for infile in $infiles; do
file_basename=$(get_file_basename $infile)
file_ext=$(get_file_ext $infile)
# Only process *md files
if [ "$file_ext" == "md" ]; then
if [ "$optverbose" == "True" ]; then
echo " Generating: " $file_basename"."$outformat
fi
$(do_conversion $file_basename $root_dir $this_path $outformat $outstyle)
if [ "$?" -ne "0" ]; then
echo "ERROR: severe error. Could not get current path"
clean_up
exit $?
fi
fi
done
cd $OLDPWD
done
clean_up
cd - > /dev/null
}
# Execute this script
main $@