This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
build.sh
executable file
·239 lines (214 loc) · 6.98 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
base_dir="/base"
compile_dir="$base_dir/gt-linalg"
build_base="/home/vagrant"
build_dir="$build_base/build"
latex_dir="$build_base/build-pdf"
cache_dir="$build_base/pretex-cache"
static_dir="$build_dir/static"
figure_img_dir="$build_dir"/figure-images
pretex="$base_dir/gt-text-common/pretex/pretex.py"
node_dir="$build_base/node_modules"
# Run in the vagrant build vm
if [ ! -d "$base_dir" ]; then
if [ ! $(vagrant status --machine-readable | grep state,running) ]; then
vagrant up || die "Cannot start build environment virtual machine"
fi
vagrant ssh -- "$compile_dir/build.sh" "$@"
exit $?
fi
die() {
echo "$@"
exit 1
}
compile_latex() {
(cd "$latex_dir" && \
TEXINPUTS=".:$latex_dir/style:" pdflatex \
-interaction=nonstopmode "\input{index}" \
|| die "pdflatex failed")
}
make_hashes() {
cat >xsl/git-hash.xsl <<EOF
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:template name="git-hash">
<xsl:text>$(git rev-parse HEAD)</xsl:text>
</xsl:template>
<xsl:template name="git-hash-short">
<xsl:text>$(git rev-parse HEAD | cut -c 1-6)</xsl:text>
</xsl:template>
<xsl:template name="versioned-file">
<xsl:param name="file"/>
<xsl:variable name="commit">
<xsl:choose>
<xsl:when test="\$file='static/gt-linalg.js'">
<xsl:text>$(git hash-object "$build_dir"/static/gt-linalg.js | cut -c 1-6)</xsl:text>
</xsl:when>
<xsl:when test="\$file='static/gt-linalg.css'">
<xsl:text>$(git hash-object "$build_dir"/static/gt-linalg.css | cut -c 1-6)</xsl:text>
</xsl:when>
<xsl:when test="\$file='demos/cover.js'">
<xsl:text>$(git hash-object "$build_dir"/demos/cover.js | cut -c 1-6)</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="\$file"/>
<xsl:text>?vers=</xsl:text>
<xsl:value-of select="\$commit"/>
</xsl:template>
</xsl:stylesheet>
EOF
}
combine_css() {
if [ -n "$MINIFY" ]; then
"$node_dir"/clean-css-cli/bin/cleancss --skip-rebase "$@"
else
cat "$@"
fi
}
combine_js() {
if [ -n "$MINIFY" ]; then
"$node_dir"/uglify-js/bin/uglifyjs -m -- "$@"
else
(
for file in "$@"; do
cat "$file"
echo ";"
done
)
fi
}
VERSION=default
PRETEX_ALL=
LATEX_TOO=
MINIFY=
DEMOS=
CHUNKSIZE="250"
while [[ $# -gt 0 ]]; do
case $1 in
--version)
shift
VERSION=$1
;;
--reprocess-latex)
PRETEX_ALL="true"
;;
--pdf-vers)
LATEX_TOO="true"
;;
--minify)
MINIFY="true"
;;
--demos)
DEMOS="true"
;;
--chunk)
shift
CHUNKSIZE=$1
;;
*)
die "Unknown argument: $1"
;;
esac
shift
done
if [ -n "$DEMOS" ]; then
echo "Generating demos..."
"$compile_dir/demos/generate.py" \
|| die "Can't generate demos"
fi
echo "Preprocessing "
XML_FILE="$build_base/$VERSION.xml"
if [ $VERSION == "default" ]; then
PDF_FILE="ila.pdf"
else
PDF_FILE="ila-$VERSION.pdf"
fi
cd "$compile_dir"
xsltproc -o "$XML_FILE" --xinclude --stringparam version $VERSION \
xsl/versioning.xsl linalg.xml
echo "Checking xml..."
xmllint --xinclude --noout --relaxng "$base_dir/mathbook/schema/pretext.rng" \
"$XML_FILE"
if [[ $? == 3 || $? == 4 ]]; then
echo "Input is not valid MathBook XML; exiting"
exit 1
fi
echo "Cleaning up previous build..."
rm -rf "$build_dir"
mkdir -p "$build_dir"
mkdir -p "$static_dir"
mkdir -p "$static_dir/fonts"
mkdir -p "$static_dir/images"
if [ -n "$LATEX_TOO" ]; then
echo "***************************************************************************"
echo "BUILDING PDF"
echo "***************************************************************************"
rm -rf "$latex_dir"
mkdir -p "$latex_dir"
cp -r "$compile_dir/style" "$latex_dir/style"
cp -r "$compile_dir/figure-images" "$latex_dir/figure-images"
mkdir -p "$latex_dir/static"
cp -r "$compile_dir/images" "$latex_dir/static/images"
echo "Generating master LaTeX file"
xsltproc -o "$latex_dir/" --xinclude \
"$compile_dir/xsl/mathbook-latex.xsl" "$XML_FILE" \
|| die "xsltproc failed!"
echo "Compiling PDF version (pass 1)"
compile_latex
echo "Compiling PDF version (pass 2)"
compile_latex
mv "$latex_dir"/index.pdf "$build_dir/$PDF_FILE"
fi
echo "***************************************************************************"
echo "BUILDING HTML"
echo "***************************************************************************"
echo "Copying static files..."
combine_css "$base_dir/mathbook-assets/stylesheets/mathbook-gt.css" \
"$base_dir/mathbook/css/mathbook-add-on.css" \
"$base_dir/gt-text-common/css/mathbook-gt-add-on.css" \
"$base_dir/gt-text-common/css/knowlstyle.css" \
"$compile_dir/demos/mathbox/mathbox.css" \
> "$static_dir/gt-linalg.css"
combine_js "$base_dir/gt-text-common/js/jquery.min.js" \
"$base_dir/gt-text-common/js/jquery.sticky.js" \
"$base_dir/gt-text-common/js/knowl.js" \
"$base_dir/gt-text-common/js/GTMathbook.js" \
> "$static_dir/gt-linalg.js"
cp "$base_dir/mathbook-assets/stylesheets/fonts/ionicons/fonts/"* "$static_dir/fonts"
cp -r "$base_dir/gt-text-common/fonts/"* "$static_dir/fonts"
cp "$compile_dir/images/"* "$static_dir/images"
cp "$compile_dir/manifest.json" "$build_dir"
cp "$compile_dir/extra/google9ccfcae89045309c.html" "$build_dir"
cp -r "$compile_dir/build-demos" "$build_dir/demos"
if [ -n "$MINIFY" ]; then
for js in "$build_dir/demos/"*.js "$build_dir/demos/"*/*.js; do
"$node_dir"/uglify-js/bin/uglifyjs -m -- "$js" > "$js".min
mv "$js".min "$js"
done
for css in "$build_dir/demos/css/"*.css; do
"$node_dir"/clean-css-cli/bin/cleancss --skip-rebase "$css" > "$css".min
mv "$css".min "$css"
done
fi
echo "Converting xml to html..."
make_hashes
xsltproc -o "$build_dir/" --xinclude --stringparam pdf.online $PDF_FILE \
"$compile_dir/xsl/mathbook-html.xsl" "$XML_FILE" \
|| die "xsltproc failed!"
echo "Preprocessing LaTeX (be patient)..."
[ -n "$PRETEX_ALL" ] && rm -r "$cache_dir"
python3 "$pretex" --chunk-size $CHUNKSIZE --preamble "$build_dir/preamble.tex" \
--cache-dir "$cache_dir" --style-path "$compile_dir"/style \
--build-dir "$build_dir" \
|| die "Can't process html!"
mkdir "$figure_img_dir"
cp "$cache_dir"/*.png "$figure_img_dir"
echo "Cleaning up..."
cp "$build_dir"/index2.html "$build_dir"/index.html
rm "$build_dir"/preamble.tex
echo "Build successful! Open or reload"
echo " http://localhost:8081/"
echo "in your browser to see the result."