Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-doc
Path: blob/main/documentation/tools/asciidoctor.sh
18081 views
1
#!/bin/sh
2
#
3
# Copyright (c) 2021 Danilo G. Baio <[email protected]>
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
7
#
8
# 1. Redistributions of source code must retain the above copyright notice, this
9
# list of conditions and the following disclaimer.
10
#
11
# 2. Redistributions in binary form must reproduce the above copyright notice,
12
# this list of conditions and the following disclaimer in the documentation
13
# and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26
# shellcheck disable=SC3043
27
28
29
LOCALBASE="/usr/local"
30
31
if [ "$USE_RUBYGEMS" = "YES" ]; then
32
GEMBASE="${GEM_PATH}"
33
else
34
GEMBASE="${LOCALBASE}"
35
fi
36
37
ASCIIDOCTORPDF_CMD="${GEMBASE}/bin/asciidoctor-pdf"
38
ASCIIDOCTOREPUB3_CMD="${GEMBASE}/bin/asciidoctor-epub3"
39
40
build_pdf() {
41
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
42
exit 1
43
fi
44
45
local doc_type="$1"
46
local doc_lang="$2"
47
local doc_name="$3"
48
49
local cur_dir_source="content/$doc_lang/$doc_type/$doc_name/"
50
local cur_dir_output="public/$doc_lang/$doc_type/$doc_name/"
51
52
local theme_font=""
53
54
if [ ! -d "$cur_dir_output" ]; then
55
mkdir -p "$cur_dir_output"
56
fi
57
58
if [ "$doc_type" = "books" ]; then
59
local asciidoctor_type="book"
60
61
if [ -f "${cur_dir_source}book.adoc" ]; then
62
local asciidoctor_file_name="book.adoc"
63
else
64
local asciidoctor_file_name="_index.adoc"
65
fi
66
fi
67
68
if [ "$doc_type" = "articles" ]; then
69
local asciidoctor_type="article"
70
local asciidoctor_file_name="_index.adoc"
71
fi
72
73
# Check non default fonts
74
case "$doc_lang" in
75
zh-cn)
76
if [ ! -f "$LOCALBASE/share/docproj-fonts-cjk/NotoSansSC-Medium.otf" ]; then
77
echo " font not found, skipping pdf build"
78
return
79
fi
80
theme_font="-a pdf-theme=./shared/zh-cn/zh-cn-theme.yml -a pdf-fontsdir=$LOCALBASE/share/docproj-fonts-cjk"
81
;;
82
zh-tw)
83
if [ ! -f "$LOCALBASE/share/docproj-fonts-cjk/NotoSansTC-Medium.otf" ]; then
84
echo " font not found, skipping pdf build"
85
return
86
fi
87
theme_font="-a pdf-theme=./shared/zh-tw/zh-tw-theme.yml -a pdf-fontsdir=$LOCALBASE/share/docproj-fonts-cjk/"
88
;;
89
*)
90
theme_font="-a pdf-theme=default-with-fallback-font"
91
;;
92
esac
93
94
$ASCIIDOCTORPDF_CMD \
95
-r ./shared/lib/man-macro.rb \
96
-r ./shared/lib/git-macro.rb \
97
-r ./shared/lib/packages-macro.rb \
98
-r ./shared/lib/inter-document-references-macro.rb \
99
-r ./shared/lib/sectnumoffset-treeprocessor.rb \
100
-r ./shared/lib/cross-document-references-macro.rb \
101
--doctype="$asciidoctor_type" \
102
-a skip-front-matter \
103
-a lang="$doc_lang" \
104
-a isonline=1 \
105
-a env-beastie=1 \
106
${theme_font} \
107
-o "${cur_dir_output}${doc_name}_${doc_lang}.pdf" \
108
"${cur_dir_source}${asciidoctor_file_name}"
109
}
110
111
112
build_epub() {
113
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
114
exit 1
115
fi
116
117
local doc_type="$1"
118
local doc_lang="$2"
119
local doc_name="$3"
120
121
local cur_dir_source="content/$doc_lang/$doc_type/$doc_name/"
122
local cur_dir_output="public/$doc_lang/$doc_type/$doc_name/"
123
124
if [ ! -d "$cur_dir_output" ]; then
125
mkdir -p "$cur_dir_output"
126
fi
127
128
if [ "$doc_type" = "books" ]; then
129
local asciidoctor_type="book"
130
131
if [ -f "${cur_dir_source}book.adoc" ]; then
132
local asciidoctor_file_name="book.adoc"
133
else
134
local asciidoctor_file_name="_index.adoc"
135
fi
136
fi
137
138
if [ "$doc_type" = "articles" ]; then
139
local asciidoctor_type="article"
140
local asciidoctor_file_name="_index.adoc"
141
fi
142
143
$ASCIIDOCTOREPUB3_CMD \
144
-r ./shared/lib/man-macro.rb \
145
-r ./shared/lib/git-macro.rb \
146
-r ./shared/lib/packages-macro.rb \
147
-r ./shared/lib/inter-document-references-macro.rb \
148
-r ./shared/lib/sectnumoffset-treeprocessor.rb \
149
-r ./shared/lib/cross-document-references-macro.rb \
150
--doctype="$asciidoctor_type" \
151
-a skip-front-matter \
152
-a lang="$doc_lang" \
153
-a isonline=1 \
154
-a env-beastie=1 \
155
-o "${cur_dir_output}${doc_name}_${doc_lang}_POC_.epub" \
156
"${cur_dir_source}${asciidoctor_file_name}"
157
}
158
159
160
archive() {
161
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
162
exit 1
163
fi
164
165
local doc_type="$1"
166
local doc_lang="$2"
167
local doc_name="$3"
168
169
if [ -d "public/$doc_lang" ]; then
170
local pub_dir="public/$doc_lang/$doc_type/$doc_name/"
171
elif [ -d "public/$doc_type" ]; then
172
# single language build
173
local pub_dir="public/$doc_type/$doc_name/"
174
fi
175
176
if [ -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz" ]; then
177
rm -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz"
178
fi
179
180
local source_doc_dir=""
181
if [ -d "public/source/$doc_type/$doc_name/" ]; then
182
source_doc_dir="public/source/$doc_type/$doc_name/"
183
fi
184
185
local image_doc_dir=""
186
if [ -d "public/images/$doc_type/$doc_name/" ]; then
187
image_doc_dir="public/images/$doc_type/$doc_name/"
188
fi
189
190
tar -czf "public/${doc_name}_${doc_lang}.tar.gz" \
191
"$pub_dir" \
192
public/css/ \
193
public/fonts/ \
194
public/images/FreeBSD-colors.svg \
195
public/images/FreeBSD-monochromatic.svg \
196
public/js/ \
197
public/styles/ \
198
$source_doc_dir \
199
$image_doc_dir
200
201
mv -f "public/${doc_name}_${doc_lang}.tar.gz" "$pub_dir"
202
203
}
204
205
206
main() {
207
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
208
echo "Needs parameters (type, language and format)."
209
echo "$0 articles en pdf"
210
exit 1
211
fi
212
213
local doc_type="$1"
214
local doc_lang="$2"
215
local doc_format="$3"
216
217
if [ ! "$doc_type" = "articles" ] && [ ! "$doc_type" = "books" ]; then
218
echo "First parameter needs to be 'articles' or 'books'"
219
exit 1
220
fi
221
222
case "$doc_format" in
223
pdf)
224
for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
225
if [ "$document" = "pgpkeys" ]; then
226
continue
227
fi
228
echo "asciidoctor build_pdf: $doc_type $doc_lang $document"
229
build_pdf "$doc_type" "$doc_lang" "$document"
230
done
231
;;
232
archive)
233
for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
234
echo "generate archive: $doc_type $doc_lang $document"
235
archive "$doc_type" "$doc_lang" "$document"
236
done
237
;;
238
epub)
239
for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
240
if [ "$document" = "pgpkeys" ]; then
241
continue
242
fi
243
echo "asciidoctor epub: $doc_type $doc_lang $document"
244
build_epub "$doc_type" "$doc_lang" "$document"
245
done
246
;;
247
*)
248
echo "Formats available: archive, pdf, epub"
249
exit 1
250
;;
251
esac
252
}
253
254
main "$@"
255
256