-
Notifications
You must be signed in to change notification settings - Fork 2
/
book.sh
executable file
·212 lines (170 loc) · 5.18 KB
/
book.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
#!/bin/sh
MDBOOK_CATPPUCCIN_VERSION="2.2.0"
set -o errexit -o nounset
get_asset_version() {
[ $# -ne 1 ] && return 1
asset_version="$(grep -o -m 1 -P '(?<=assets_version = ")[^"]*' "$1/book.toml")"
}
poppulate_cache_admonish() {
[ $# -ne 1 ] && return 1
cache_dir="$1"
if ! type mdbook-admonish > /dev/null 2>&1; then
echo "\
mdbook-admonish must be installed! See
https://github.com/tommilligan/mdbook-admonish" >&2
exit 1
fi
# Create a fake mdbook config file so that mdbook-admonish can install its
# files to it.
touch "${cache_dir}/book.toml"
mdbook-admonish install "$cache_dir"
if ! [ -f "${cache_dir}/mdbook-admonish.css" ]; then
echo "Couldn't generate ${cache_dir}/mdbook-admonish.css!" >&2
exit 1
fi
}
poppulate_cache_catppuccin_index() {
[ $# -ne 3 ] && return 1
base="$1"
temporary_default_theme="$2"
modified_theme="$3"
# We take the default index.hbs and patch in our new themes.
mkdir "$temporary_default_theme"
mdbook init --theme --force --title "index.hbs mdbook book" --ignore none\
"$temporary_default_theme"
patch -d "$temporary_default_theme" -p1\
< "${base}/theme/add-catppuccin-to-index.patch"
mkdir "$modified_theme"
# Move the only file we care about, index.hbs, somewhere else and leave the rest.
mv "${temporary_default_theme}/theme/index.hbs" "${modified_theme}/index.hbs"
}
poppulate_cache_catppuccin_css() {
[ $# -ne 1 ] && return 1
cache_dir="$1"
archive_base_path_that_will_be_stripped="mdBook-${MDBOOK_CATPPUCCIN_VERSION}/src/bin/assets"
# --strip-components refers to the number of components in
# archive_base_path_that_will_be_stripped
curl -L "https://github.com/catppuccin/mdBook/archive/refs/tags/v${MDBOOK_CATPPUCCIN_VERSION}.tar.gz" |\
tar -C "${cache_dir}" --strip-components=4 -xzf -\
"${archive_base_path_that_will_be_stripped}/catppuccin-admonish.css"\
"${archive_base_path_that_will_be_stripped}/catppuccin.css"
}
populate_cache() {
[ $# -ne 4 ] && return 1
dir="$1"
base="$2"
modified_theme="$3"
enable_catppuccin="$4"
mkdir -p "$dir" || true
if ! [ -f "${dir}/mdbook-admonish.css" ]; then
poppulate_cache_admonish "$dir"
fi
if [ -z "$enable_catppuccin" ]; then
return
fi
if ! [ -f "${modified_theme}/index.hbs" ]; then
poppulate_cache_catppuccin_index "$base" "${dir}/temporary-default-theme"\
"$modified_theme"
fi
if ! { [ -f "${dir}/catppuccin-admonish.css" ] && [ -f "${dir}/catppuccin.css" ]; }; then
poppulate_cache_catppuccin_css "$dir"
fi
# Try to do smart copy of all static theme files to the cached directory.
# If it fails, try using a simpler cp cmdline (because the failure could have
# been caused by using cp other than the one provided by GNU coreutils).
cp --reflink=auto --update=older "${base}"/theme/* "$modified_theme" ||\
cp "${base}"/theme/* "$modified_theme"
}
exec_mdbook() {
[ $# -lt 3 ] && return 1
asset_version="$1"
cache_dir="$2"
theme_dir="$3"
shift 3
# Accumulate all CSS files in cache_dir in JSON-ish format.
additional_css=""
for css_file in "${cache_dir}"/*.css; do
if [ -z "$additional_css" ]; then
additional_css="[\"$css_file\""
else
additional_css="${additional_css}, \"${css_file}\""
fi
done
additional_css="${additional_css}]"
if [ "$theme_dir" ]; then
export MDBOOK_OUTPUT__HTML__THEME="$theme_dir"
fi
exec env MDBOOK_PREPROCESSOR__ADMONISH="{\"assets_version\": \"$asset_version\"}"\
MDBOOK_OUTPUT__HTML__ADDITIONAL_CSS="$additional_css"\
mdbook "$@"
}
usage="\
A wrapper for mdBook which handles mdbook-admonish.
$0 needs to generate some files. It puts them to <the directory the script
resides in>/cache/ by default.
You can change the base directory with -b <directory> (directory the script
resides in by default) and the cache subdirectory (cache/ by default) with -c
<directory relative to base directory>. The files are put into <base
directory>/<subdirectory>
Directories are created if they do not exist.
Usage:
$0 [-b <directory>] [-c <directory>] [-e <true|false>] mdbook commandline...
$0 -h
Options:
-h Show this help message.
-b=<directory> Set base directory.
-c=<directory> Set cache directory.
-e=<true|false> Turn on extra themes. This will download mdbook-catppuccin
to cache. True by default.
Example usage:
Build the book (calls \`mdbook build\` internally):
$0 build
Serve the book (calls \`mdbook serve\` internally):
$0 serve
"
# I assume $0 is set and it's valid.
basedir="$(dirname "$0")"
cache_dir_component="cache/"
enable_catppuccin="1"
while getopts hb:c:e: f; do
case $f in
h)
printf %s "$usage"
exit 0
;;
b)
basedir="$OPTARG"
;;
c)
cache_dir_component="$OPTARG"
;;
e)
case "$OPTARG" in
true)
enable_catppuccin="1"
;;
false)
enable_catppuccin=""
;;
*)
echo "Invalid argument to -e: ${OPTARG}" >&2
exit 1
;;
esac
;;
*)
printf %s "$usage"
exit 1
;;
esac
done
shift $((OPTIND - 1))
finaldir="${basedir}/${cache_dir_component}"
if [ "$enable_catppuccin" ]; then
cached_theme_dir="${finaldir}/theme"
else
cached_theme_dir=""
fi
populate_cache "$finaldir" "$basedir" "$cached_theme_dir" "$enable_catppuccin"
get_asset_version "$finaldir"
exec_mdbook "$asset_version" "$finaldir" "$cached_theme_dir" "$@"