Skip to content

Commit add8cfd

Browse files
mnervPratchaya Khansomboon
authored and
Pratchaya Khansomboon
committed
Fix portable script
1 parent 831b882 commit add8cfd

File tree

3 files changed

+118
-120
lines changed

3 files changed

+118
-120
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Notes
22

3-
Alpine container with LaTeX and neovim installation.
3+
LaTeX first class notes collection.
44

55
## Build
66

@@ -39,7 +39,7 @@ Activate it by using the script inside the .venv `directory`.
3939
Install packages from the `requirements.txt`
4040

4141
```sh
42-
pip install -r ./requirements.txt
42+
pip install -r requirements.txt
4343
```
4444

4545
## Resources

compile.sh

+71-71
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ build_dir=build
66
input_file=""
77

88
# Arg flags
9-
is_skip_bitex=false
10-
is_dry_run=false
9+
# is_skip_bitex=false
10+
# is_dry_run=false
1111
is_no_cache=false
12-
is_no_spin=false
12+
# is_no_spin=false
1313

1414
# State flags
1515
is_cached=false
@@ -20,7 +20,7 @@ is_clean=false
2020
filepath=""
2121
filename=""
2222
name=""
23-
ext=""
23+
# ext=""
2424
rel_location=""
2525
latex_build_dir=""
2626
pdf_dir=""
@@ -31,7 +31,7 @@ file_deps=""
3131

3232
help() {
3333
cat << EOF
34-
usage: $arg0 [options] <file.tex>
34+
usage: $script_name [options] <file.tex>
3535
3636
options:
3737
-h, --help Show this menu.
@@ -45,39 +45,39 @@ EOF
4545
}
4646

4747
parse_args() {
48-
if [[ $# = 0 ]]; then
48+
if [ $# = 0 ]; then
4949
printf "error: no input file\n"
5050
help
5151
exit 1
5252
fi
5353

54-
while [[ $# -gt 0 ]]; do
54+
while [ $# -gt 0 ]; do
5555
key="$1"
5656
case $key in
5757
--help | -h)
5858
help
5959
exit 0
6060
;;
61-
--dry-run)
62-
is_dry_run=true
63-
;;
61+
# --dry-run)
62+
# is_dry_run=true
63+
# ;;
6464
--build-dir=*)
6565
build_dir="${key#*=}"
6666
;;
67-
--skip-bibtex)
68-
is_skip_bitex=true
69-
;;
67+
# --skip-bibtex)
68+
# is_skip_bitex=true
69+
# ;;
7070
--no-cache)
7171
is_no_cache=true
7272
;;
73-
--no-spin)
74-
is_no_spin=true
75-
;;
73+
# --no-spin)
74+
# is_no_spin=true
75+
# ;;
7676
--clean)
7777
is_clean=true
7878
;;
7979
*)
80-
if [[ $has_input = true ]]; then
80+
if [ $has_input = true ]; then
8181
printf "Only one input file is allowed!\n"
8282
exit 1
8383
fi
@@ -92,85 +92,85 @@ parse_args() {
9292

9393
recursive_index() {
9494
# Check if file exist before we analyze it
95-
if ! [[ -e "$1" ]]; then
95+
if ! [ -e "$1" ]; then
9696
return 0
9797
fi
9898

9999
_grep_regex='((input)|(includegraphics\[.*\])|(addbibresource))\{.+\}'
100100
_replace_regex='s/\\((input)|(includegraphics\[.*\])|(addbibresource))\{(.+)\}/\5/'
101101
if ! [ -d "$1" ]; then
102-
_deps=$(cat "$1" | grep -E "$_grep_regex" | sed -r "$_replace_regex")
102+
_deps=$(grep -E "$_grep_regex" < "$1" | sed -r "$_replace_regex")
103103
else
104104
_deps=""
105105
fi
106106

107-
if ! [[ -z "$_deps" ]]; then
108-
if ! [[ -z "$file_deps" ]]; then
107+
if [ -n "$_deps" ]; then
108+
if [ -n "$file_deps" ]; then
109109
file_deps="$file_deps\n$_deps"
110110
else
111111
file_deps="$_deps"
112112
fi
113113

114-
for file in "$_deps"; do
115-
filepath="$(dirname $1)/$file"
116-
recursive_index $filepath
114+
for file in $_deps; do
115+
filepath="$(dirname "$1")/$file"
116+
recursive_index "$filepath"
117117
done
118118
fi
119119
}
120120

121121
prebuild() {
122-
if ! [[ -e $input_file ]]; then
123-
printf "File $input_file does not exist!\n"
122+
if ! [ -e "$input_file" ]; then
123+
printf "File %s does not exist!\n" "$input_file"
124124
exit 1
125125
fi
126126

127-
filepath="$(realpath $input_file)"
127+
filepath="$(realpath "$input_file")"
128128
filename="$(basename "$input_file")"
129129
name="${filename%.*}"
130-
ext="${filename##*.}"
131-
rel_location="$(dirname $input_file)"
130+
# ext="${filename##*.}"
131+
rel_location="$(dirname "$input_file")"
132132
latex_build_dir="$build_dir/tex/$rel_location"
133133
pdf_dir="$build_dir/pdf/$rel_location"
134134

135-
if ! [[ -e $build_dir ]]; then
136-
mkdir -p $build_dir
135+
if ! [ -e "$build_dir" ]; then
136+
mkdir -p "$build_dir"
137137
fi
138138

139-
if ! [[ -e $latex_build_dir ]]; then
140-
mkdir -p $latex_build_dir
141-
elif [[ -e $latex_build_dir ]] && [[ $is_clean = true ]]; then
142-
rm -rf $latex_build_dir
143-
mkdir -p $latex_build_dir
139+
if ! [ -e "$latex_build_dir" ]; then
140+
mkdir -p "$latex_build_dir"
141+
elif [ -e "$latex_build_dir" ] && [ $is_clean = true ]; then
142+
rm -rf "$latex_build_dir"
143+
mkdir -p "$latex_build_dir"
144144
fi
145145

146-
if ! [[ -e $pdf_dir ]]; then
147-
mkdir -p $pdf_dir
148-
elif [[ -e $pdf_dir ]] && [[ $is_clean = true ]]; then
149-
rm -rf $pdf_dir
150-
mkdir -p $pdf_dir
146+
if ! [ -e "$pdf_dir" ]; then
147+
mkdir -p "$pdf_dir"
148+
elif [ -e "$pdf_dir" ] && [ $is_clean = true ]; then
149+
rm -rf "$pdf_dir"
150+
mkdir -p "$pdf_dir"
151151
fi
152152

153153
# Convert output path to absolute
154154
abs_latex_build_dir=$(realpath "$build_dir/tex/$rel_location")
155155
abs_pdf_dir=$(realpath "$build_dir/pdf/$rel_location")
156156
pdf_latex_dir="$abs_pdf_dir/$name"
157157

158-
if ! [[ -e "$pdf_latex_dir" ]]; then
158+
if ! [ -e "$pdf_latex_dir" ]; then
159159
mkdir -p "$pdf_latex_dir"
160-
elif [[ -e $pdf_latex_dir ]] && [[ $is_clean = true ]]; then
161-
rm -rf $pdf_latex_dir
160+
elif [ -e "$pdf_latex_dir" ] && [ $is_clean = true ]; then
161+
rm -rf "$pdf_latex_dir"
162162
mkdir -p "$pdf_latex_dir"
163163
fi
164164

165165
# Generate local registry for dependencies
166-
recursive_index $filepath
166+
recursive_index "$filepath"
167167
file_deps=$(echo "$file_deps" | sed -r "s/[ %]*//" | sort -u)
168168
}
169169

170170
parse_info() {
171-
if [ $1 -ne 0 ]; then
172-
printf "\r$2: failed, check $latex_build_dir/$2.log\n"
173-
cat $abs_latex_build_dir/$2.log | awk 'BEGIN{IGNORECASE = 1}/warning|!|Runaway argument\?/,/^$/;'
171+
if [ "$1" -ne 0 ]; then
172+
printf "\r%s: failed, check %s\n" "$2" "$latex_build_dir/$2.log"
173+
awk 'BEGIN{IGNORECASE = 1}/warning|!|Runaway argument\?/,/^$/;' < "$abs_latex_build_dir/$2.log"
174174
exit 1
175175
fi
176176
}
@@ -180,35 +180,35 @@ exec_build_cmd() {
180180
cmd=$2
181181
log_file="$abs_latex_build_dir/$exec_name.log"
182182

183-
$cmd > $log_file
183+
$cmd > "$log_file"
184184
exit_code=$?
185185
parse_info $exit_code "$exec_name"
186186
}
187187

188188
check_cache() {
189189
is_cached=false
190190

191-
if ! [[ -e "$abs_pdf_dir/$name.pdf" ]] || ! [[ -e "$pdf_latex_dir/$filename" ]]; then
191+
if ! [ -e "$abs_pdf_dir/$name.pdf" ] || ! [ -e "$pdf_latex_dir/$filename" ]; then
192192
return 0
193193
fi
194194

195-
input_check_sum=$(sha256sum $input_file | cut -d ' ' -f 1)
195+
input_check_sum=$(sha256sum "$input_file" | cut -d ' ' -f 1)
196196
output_check_sum=$(sha256sum "$pdf_latex_dir/$filename" | cut -d ' ' -f 1)
197-
if ! [[ $input_check_sum = $output_check_sum ]]; then
197+
if ! [ "$input_check_sum" = "$output_check_sum" ]; then
198198
return 0
199199
fi
200200

201201
for file in $file_deps; do
202-
if [[ -e $rel_location/$file ]] && [[ -e $pdf_latex_dir/$file ]]; then
203-
asum=$(sha256sum $rel_location/$file | cut -d ' ' -f 1)
204-
bsum=$(sha256sum $pdf_latex_dir/$file | cut -d ' ' -f 1)
202+
if [ -e "$rel_location/$file" ] && [ -e "$pdf_latex_dir/$file" ]; then
203+
asum=$(sha256sum "$rel_location/$file" | cut -d ' ' -f 1)
204+
bsum=$(sha256sum "$pdf_latex_dir/$file" | cut -d ' ' -f 1)
205205

206-
if ! [[ $asum = $bsum ]]; then
206+
if ! [ "$asum" = "$bsum" ]; then
207207
return 0
208208
fi
209209
else
210210
# Special case for inline bibtex addbibresource{}
211-
if ! [[ "${file##*.}" = "bib" ]]; then
211+
if ! [ "${file##*.}" = "bib" ]; then
212212
return 0
213213
fi
214214
fi
@@ -218,54 +218,54 @@ check_cache() {
218218
}
219219

220220
build() {
221-
if [[ $is_cached = true ]] && [[ $is_no_cache = false ]]; then
222-
printf "$input_file is cached.\n"
221+
if [ $is_cached = true ] && [ $is_no_cache = false ]; then
222+
printf "%s is cached.\n" "$input_file"
223223
return 0
224224
fi
225225

226-
printf "compiling $input_file..."
226+
printf "compiling %s..." "$input_file"
227227

228228
current_dir=$(pwd)
229-
cd $rel_location
229+
cd "$rel_location" || exit 1
230230
exec_build_cmd "stage1" "pdflatex -halt-on-error --interaction=nonstopmode -output-directory=$abs_latex_build_dir $filename"
231231
exec_build_cmd "stage2" "biber $abs_latex_build_dir/$name"
232232
exec_build_cmd "stage3" "pdflatex -halt-on-error --interaction=nonstopmode -output-directory=$abs_latex_build_dir $filename"
233-
cd $current_dir
233+
cd "$current_dir" || exit 1
234234

235235
printf "done!\n"
236236
}
237237

238238
postbuild() {
239-
if [[ $is_cached = true ]] && [[ $is_no_cache = false ]]; then
239+
if [ $is_cached = true ] && [ $is_no_cache = false ]; then
240240
return 0
241241
fi
242242

243-
if ! [[ -e $abs_pdf_dir ]]; then
243+
if ! [ -e "$abs_pdf_dir" ]; then
244244
printf "PDF output path does not exist!\n"
245245
return 1
246246
fi
247247

248-
if [[ -e "$abs_latex_build_dir/$name.pdf" ]]; then
249-
cp "$abs_latex_build_dir/$name.pdf" $abs_pdf_dir
248+
if [ -e "$abs_latex_build_dir/$name.pdf" ]; then
249+
cp "$abs_latex_build_dir/$name.pdf" "$abs_pdf_dir"
250250
fi
251251

252252
# Copy LaTeX and dependency files
253-
cp $input_file $pdf_latex_dir
253+
cp "$input_file" "$pdf_latex_dir"
254254
for file in $file_deps; do
255-
file_dir=$(dirname $file)
256-
if ! [[ $file_dir = '.' ]]; then
255+
file_dir=$(dirname "$file")
256+
if ! [ "$file_dir" = '.' ]; then
257257
mkdir -p "$pdf_latex_dir/$file_dir"
258258
else
259259
file_dir=""
260260
fi
261-
if [[ -e "$rel_location/$file" ]]; then
261+
if [ -e "$rel_location/$file" ]; then
262262
cp "$rel_location/$file" "$pdf_latex_dir/$file_dir"
263263
fi
264264
file_dir=""
265265
done
266266
}
267267

268-
parse_args $@
268+
parse_args "$@"
269269
prebuild
270270
check_cache
271271
build

0 commit comments

Comments
 (0)