-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
generate.sh
executable file
·119 lines (102 loc) · 3.19 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
#!/bin/bash
if [[ "${DEBUG}" == 1 ]]; then
set -x
fi
set -euo pipefail
IFS=$'\n\t'
# Get book details.
source books.sh
export ${BOOKS[${BOOK_SLUG^^}]}
# Common vars.
IMGS_DOMAIN="lh3.googleusercontent.com"
PDF_OPTIONS=()
for i in ${!PDF_OPT_*}; do
declare -n val=$i
key=${i#PDF_OPT_}
PDF_OPTIONS+=("-V" "${key,,}=${val}");
done
# Make sure that links are relative \
# # Remove the /sre/ directories
# Save stuff in html/ directory
# Do not create a sre.google directory
# Enable recursion, timestamping (--mirror)
# Images are hosted elsewhere, download them as well.
# We need to go up a level from /toc/ where we start
# The "ture" at the end to ignore non-200 URLs like 404.
wget \
--convert-links \
--directory-prefix=html \
--page-requisites \
--adjust-extension \
--span-hosts \
--trust-server-names \
--backup-converted \
--mirror \
--no-verbose \
--recursive \
--domains=${IMGS_DOMAIN},sre.google ${BOOK_TOC_URL} || true
#
echo "Get working mode..."
MODE=${1:-}
if [ "$MODE" != "docker" ];then
bundle install
fi
# Add extension to files.
# That because `pandoc` cannot generate the right `mime type` without the extension.
# https://github.com/captn3m0/google-sre-ebook/issues/19
echo "Fix images extension issue ..."
IMGS_FILES="$(ls html/${IMGS_DOMAIN}/*)"
for FILE_NAME_FULL in ${IMGS_FILES}; do
# Get file vars.
FILE_NAME_BASE="$(basename ${FILE_NAME_FULL})"
FILE_TYPE=$(file -b -- "${FILE_NAME_FULL}" | cut -f1 -d " ")
# Rename and replace file.
if grep -rl -- "${FILE_NAME_BASE}" ./html; then
mv "${FILE_NAME_FULL}" "${FILE_NAME_FULL}.${FILE_TYPE,,}" &&
grep -rl -- "${FILE_NAME_BASE}" ./html | xargs sed -i -- "s/${FILE_NAME_BASE}/${FILE_NAME_BASE}.${FILE_TYPE,,}/g"
fi
done
if [[ $@ == *'--only-download'* ]]; then
echo "Skipping generation"
exit 0
fi
# Generate epub from html.
echo "Generate book ..."
bundle exec ruby generate.rb
pushd html/sre.google/${BOOK_NAME}/table-of-contents
pandoc --from=html --to=epub \
--output=../../../../${BOOK_FILE}.epub \
--epub-metadata=../../../../metadata/${BOOK_NAME}.xml \
--epub-cover-image=../../../../cover/${BOOK_NAME}.jpg \
--metadata title="$BOOK_NAME" \
complete.html
# generate PDF from HTML
pandoc --from=html \
--pdf-engine=xelatex \
--output=../../../../${BOOK_FILE}.pdf \
--metadata title="$BOOK_NAME" \
--dpi=300 \
-V book \
--top-level-division=chapter \
--toc \
-V lang=en-US \
-V classoption=oneside \
-V titlepage=true \
-V logo=../../../../cover/${BOOK_NAME}.jpg \
-V titlepage-background=../../../../cover/${BOOK_NAME}.jpg \
-V toc-own-page=true \
-V footnotes-pretty=true \
-V subparagraph \
-V geometry=margin=2cm \
-V fig_caption=false \
${PDF_OPTIONS[@]} \
--columns=60 \
complete.html
popd
# Generate mobi using calibre
ebook-convert ${BOOK_FILE}.epub ${BOOK_FILE}.mobi
# If it works inside docker.
if [ "$MODE" == "docker" ]; then
chown -v $(id -u):$(id -g) ${BOOK_FILE}.*
mv -f ${BOOK_FILE}.* /output
fi