-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·117 lines (95 loc) · 2.11 KB
/
build.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
#!/bin/bash
DEFAULT="\033[0m"
GREEN="\033[92m"
RED="\033[91m"
YELLOW="\033[93m"
OTHER="\033[90m"
INFO="[${YELLOW}INFO${DEFAULT}]"
ERROR="[${RED}ERROR${DEFAULT}]"
GOOD="[${GREEN}GOOD${DEFAULT}]"
SKIP="[${OTHER}SKIP${DEFAULT}]"
# For extra glob features (e.g. ?, *, +, @, !)
shopt -s extglob
FUNCTIONS="build|clean|serve"
build-book() {
local book=$1
printf "${INFO} Building ${book}...\n"
mdbook build "${book}"
}
fix-js-css-path() {
if [ "$1" = "--css" ];
then
local ext="css"
local tag="href"
shift
else
local ext="js"
local tag="src"
fi
local pat="${tag}=\"\(.*\)\(special-.*.${ext}\)\""
if [ "$1" = "--local" ];
then
BASEPATH=""
local dst="${tag}=\"${BASEPATH//\//\\/}\/${ext}\/\2\""
echo "Fixing ${ext} paths (local build)..."
echo " -> pat: ${pat}"
echo " -> dst: ${dst}"
else
local dst="${tag}=\"\/tutorials\/${ext}\/\2\""
echo "Fixing ${ext} paths..."
fi
for file in $(find -name "*.html");
do
sed -i "s/${pat}/${dst}/g" "${file}"
done
}
nofun() {
if [ -z $1 ]; then
echo "Usage: ${0} <function>"
else
echo "No function \"${1}\"."
fi
echo "Available functions: "
echo " $(echo ${FUNCTIONS} | sed 's/|/\n /g')"
exit 1
}
# Commands
build() {
check-mdbook
echo "Build flags: $*"
build-book main
build-book tools
build-book general
for book in langs/*; do
if [ -f ${book} ]; then
printf "${SKIP} ${book}: not a directory.\n"
elif [ ! -d "${book}/src" ]; then
printf "${SKIP} ${book}: not a valid mdbook.\n"
else
build-book "${book}"
fi
done
fix-js-css-path $1
fix-js-css-path --css $1
}
serve() {
build
python -m http.server -d book
}
clean() {
rm -rf book
}
check-mdbook() {
if ! command -v mdbook &> /dev/null; then
printf "${ERROR} ${book}: mdbook not found or is not installed"
fi
}
FS="@(${FUNCTIONS})"
case $1 in
${FS})
$*
;;
*)
nofun $*
;;
esac