11#! /bin/sh
22
3- set +o errexit +o nounset
3+ MDBOOK_CATPPUCCIN_VERSION=" 2.2.0"
4+
5+ set -o errexit -o nounset
46
57get_asset_version () {
68 [ $# -ne 1 ] && return 1
79 asset_version=" $( grep -o -m 1 -P ' (?<=assets_version = ")[^"]*' " $1 /book.toml" ) "
810}
911
10- populate_cache () {
12+ poppulate_cache_admonish () {
13+ [ $# -ne 1 ] && return 1
14+ cache_dir=" $1 "
15+
1116 if ! type mdbook-admonish > /dev/null 2>&1 ; then
1217 echo " \
1318mdbook-admonish must be installed! See
14- https://github.com/tommilligan/mdbook-admonish" 1 >&2
19+ https://github.com/tommilligan/mdbook-admonish" >&2
1520 exit 1
1621 fi
1722
23+ # Create a fake mdbook config file so that mdbook-admonish can install its
24+ # files to it.
25+ touch " ${cache_dir} /book.toml"
26+
27+ mdbook-admonish install " $cache_dir "
28+
29+ if ! [ -f " ${cache_dir} /mdbook-admonish.css" ]; then
30+ echo " Couldn't generate ${cache_dir} /mdbook-admonish.css!" >&2
31+ exit 1
32+ fi
33+ }
34+
35+ poppulate_cache_catppuccin_index () {
36+ [ $# -ne 3 ] && return 1
37+
38+ base=" $1 "
39+ temporary_default_theme=" $2 "
40+ modified_theme=" $3 "
41+
42+ # We take the default index.hbs and patch in our new themes.
43+ mkdir " $temporary_default_theme "
44+ mdbook init --theme --force --title " index.hbs mdbook book" --ignore none\
45+ " $temporary_default_theme "
46+ patch -d " $temporary_default_theme " -p1\
47+ < " ${base} /theme/add-catppuccin-to-index.patch"
48+
49+ mkdir " $modified_theme "
50+
51+ # Move the only file we care about, index.hbs, somewhere else and leave the rest.
52+ mv " ${temporary_default_theme} /theme/index.hbs" " ${modified_theme} /index.hbs"
53+ }
54+
55+ poppulate_cache_catppuccin_css () {
1856 [ $# -ne 1 ] && return 1
1957
20- dir =" $1 "
58+ cache_dir =" $1 "
2159
22- mkdir -p " $dir "
60+ archive_base_path_that_will_be_stripped= " mdBook- ${MDBOOK_CATPPUCCIN_VERSION} /src/bin/assets "
2361
24- # Create a fake mdbook config file so that mdbook-admonish can install its files
25- # to it.
26- touch " ${dir} /book.toml"
62+ # --strip-components refers to the number of components in
63+ # archive_base_path_that_will_be_stripped
64+ curl -L " https://github.com/catppuccin/mdBook/archive/refs/tags/v${MDBOOK_CATPPUCCIN_VERSION} .tar.gz" | \
65+ tar -C " ${cache_dir} " --strip-components=4 -xzf -\
66+ " ${archive_base_path_that_will_be_stripped} /catppuccin-admonish.css" \
67+ " ${archive_base_path_that_will_be_stripped} /catppuccin.css"
68+ }
69+
70+ populate_cache () {
71+ [ $# -ne 4 ] && return 1
72+
73+ dir=" $1 "
74+ base=" $2 "
75+ modified_theme=" $3 "
76+ enable_catppuccin=" $4 "
2777
28- mdbook-admonish install " $dir "
78+ mkdir -p " $dir " || true
2979
3080 if ! [ -f " ${dir} /mdbook-admonish.css" ]; then
31- echo " Couldn't generate ${dir} /mdbook-admonish.css!" 1>&2
32- exit 1
81+ poppulate_cache_admonish " $dir "
82+ fi
83+
84+ if [ -z " $enable_catppuccin " ]; then
85+ return
3386 fi
87+
88+ if ! [ -f " ${modified_theme} /index.hbs" ]; then
89+ poppulate_cache_catppuccin_index " $base " " ${dir} /temporary-default-theme" \
90+ " $modified_theme "
91+ fi
92+
93+ if ! { [ -f " ${dir} /catppuccin-admonish.css" ] && [ -f " ${dir} /catppuccin.css" ]; }; then
94+ poppulate_cache_catppuccin_css " $dir "
95+ fi
96+
97+ # Try to do smart copy of all static theme files to the cached directory.
98+ # If it fails, try using a simpler cp cmdline (because the failure could have
99+ # been caused by using cp other than the one provided by GNU coreutils).
100+ cp --reflink=auto --update=older " ${base} " /theme/* " $modified_theme " || \
101+ cp " ${base} " /theme/* " $modified_theme "
34102}
35103
36104exec_mdbook () {
37- [ $# -lt 2 ] && return 1
105+ [ $# -lt 3 ] && return 1
38106 asset_version=" $1 "
39- finaldir=" $2 "
40- shift 2
107+ cache_dir=" $2 "
108+ theme_dir=" $3 "
109+ shift 3
110+
111+ # Accumulate all CSS files in cache_dir in JSON-ish format.
112+ additional_css=" "
113+ for css_file in " ${cache_dir} " /* .css; do
114+ if [ -z " $additional_css " ]; then
115+ additional_css=" [\" $css_file \" "
116+ else
117+ additional_css=" ${additional_css} , \" ${css_file} \" "
118+ fi
119+ done
120+ additional_css=" ${additional_css} ]"
121+
122+ if [ " $theme_dir " ]; then
123+ export MDBOOK_OUTPUT__HTML__THEME=" $theme_dir "
124+ fi
125+
41126 exec env MDBOOK_PREPROCESSOR__ADMONISH=" {\" assets_version\" : \" $asset_version \" }" \
42- MDBOOK_OUTPUT__HTML__ADDITIONAL_CSS=" [ \" ${finaldir} /mdbook-admonish.css \" ] " \
43- mdbook " $@ "
127+ MDBOOK_OUTPUT__HTML__ADDITIONAL_CSS=" $additional_css " \
128+ mdbook " $@ "
44129}
45130
46131usage=" \
@@ -57,13 +142,15 @@ directory>/<subdirectory>
57142Directories are created if they do not exist.
58143
59144Usage:
60- $0 [-b <directory>] [-c <directory>] mdbook commandline...
145+ $0 [-b <directory>] [-c <directory>] [-e <true|false>] mdbook commandline...
61146 $0 -h
62147
63148Options:
64149 -h Show this help message.
65150 -b=<directory> Set base directory.
66151 -c=<directory> Set cache directory.
152+ -e=<true|false> Turn on extra themes. This will download mdbook-catppuccin
153+ to cache. True by default.
67154
68155Example usage:
69156 Build the book (calls \` mdbook build\` internally):
@@ -73,10 +160,11 @@ Example usage:
73160"
74161
75162# I assume $0 is set and it's valid.
76- basedir=" $( dirname $0 ) "
77- cachedir=" cache/"
163+ basedir=" $( dirname " $0 " ) "
164+ cache_dir_component=" cache/"
165+ enable_catppuccin=" 1"
78166
79- while getopts hb:c: f; do
167+ while getopts hb:c:e: f; do
80168 case $f in
81169 h)
82170 printf %s " $usage "
@@ -86,7 +174,21 @@ while getopts hb:c: f; do
86174 basedir=" $OPTARG "
87175 ;;
88176 c)
89- cachedir=" $OPTARG "
177+ cache_dir_component=" $OPTARG "
178+ ;;
179+ e)
180+ case " $OPTARG " in
181+ true)
182+ enable_catppuccin=" 1"
183+ ;;
184+ false)
185+ enable_catppuccin=" "
186+ ;;
187+ * )
188+ echo " Invalid argument to -e: ${OPTARG} " >&2
189+ exit 1
190+ ;;
191+ esac
90192 ;;
91193 * )
92194 printf %s " $usage "
@@ -97,13 +199,14 @@ done
97199
98200shift $(( OPTIND - 1 ))
99201
100- finaldir=" ${basedir} /${cachedir } "
202+ finaldir=" ${basedir} /${cache_dir_component } "
101203
102- if [ -f " ${finaldir} /mdbook-admonish.css" ]; then
103- get_asset_version " $finaldir "
104- exec_mdbook " $asset_version " " $finaldir " " $@ "
204+ if [ " $enable_catppuccin " ]; then
205+ cached_theme_dir=" ${finaldir} /theme"
105206else
106- populate_cache " $finaldir "
107- get_asset_version " $finaldir "
108- exec_mdbook " $asset_version " " $finaldir " " $@ "
207+ cached_theme_dir=" "
109208fi
209+
210+ populate_cache " $finaldir " " $basedir " " $cached_theme_dir " " $enable_catppuccin "
211+ get_asset_version " $finaldir "
212+ exec_mdbook " $asset_version " " $finaldir " " $cached_theme_dir " " $@ "
0 commit comments