forked from OpenIndiana/oi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakepdf.sh
executable file
·204 lines (175 loc) · 5.66 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
#!/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):
#
help()
{
cat<<EOF
Usage: $0 <options>
Simple script to produce pdf for OpenIndiana Docs
Without any options, all files ending in .md in the following
directories are converted:
books
contrib
dev
handbook
misc
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
-f <filename> only convert this filename
Default: convert everything
-h this help
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
EOF
}
get_file_basename ()
{
this_file=$1
echo "${this_file%.*}"
}
get_file_ext ()
{
this_file=$1
echo "${this_file##*.}"
}
get_file_path()
{
this_file=$1
echo $(dirname "$infile")
}
do_conversion()
{
input=$1
output=$2
$(pandoc --toc -f markdown+grid_tables+table_captions -o $output $input --latex-engine=xelatex)
}
main ()
{
#
# Command Line Options
#
while getopts "hd:f:" opt; do
case $opt in
d)
indir=$OPTARG
;;
f)
infile=$OPTARG
;;
h)
help
exit 0
;;
\?)
echo "Don't know this option"
exit 0
;;
esac
done
if [[ -n "$indir" && -n "$infile" ]]; then
echo "ERROR: specify -d OR -f, but not both"
exit 1
fi
# Required packages ok?
type pandoc >/dev/null 2>&1 || {
echo >&2 "ERROR: require package pandoc, please install pandoc."
exit 1; }
# -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)
pdf_outfile=$file_basename".pdf"
$(do_conversion $infile $pdf_outfile)
printf "\n"
echo " OUTPUT: " $pdf_outfile
printf "\n"
fi
exit 0
fi
# -d <subdirectory>
# An entire subdirectory
if [ -n "$indir" ]; then
# Directory must be present
if [ -d "$indir" ]; then
this_dir="$indir"
elif [ -d "./docs/$indir" ]; then
this_dir="./docs/$indir"
else
printf "\n"
echo "ERROR: cannot find directory: " $indir
exit 1
fi
echo " Writing output to this directory: " $this_dir
infiles=$(ls $this_dir)
for infile in $infiles; do
file_basename=$(get_file_basename $infile)
pdf_outfile=$this_dir"/"$file_basename".pdf"
this_infile=$this_dir"/"$infile
$(do_conversion $this_infile $pdf_outfile)
echo " Generating: " $file_basename".pdf"
done
exit 0
fi
# Default: do all directories
# Only these directories in docs:
# books
# contrib
# dev
# handbook
# misc
dirspath=docs
dirs="books contrib"
for dir in $dirs; do
this_path=$dirspath"/"$dir
echo "-------------------------"
echo "Output to this directory: " $this_path
these_files=$(ls $this_path)
for this_file in $these_files; do
file_basename=$(get_file_basename $this_file)
file_ext=$(get_file_ext $this_file)
path_file_basename="./"$this_path"/"$file_basename
pdf_outfile=$path_file_basename".pdf"
this_infile=$path_file_basename".md"
# Only process *md files
if [ "$file_ext" == "md" ]; then
$(do_conversion $this_infile $pdf_outfile)
echo " Generating: " $file_basename".pdf"
fi
done
done
}
# Execute this script
main $@