This repository has been archived by the owner on Jul 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate
executable file
·104 lines (85 loc) · 2.7 KB
/
generate
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
#!/bin/bash
set -e # crash on any error
set -u # crash on undefined vars
# TODO: Doesn't work on MACs AFAIK due to missing realpath
PROJECT_ROOT=$(dirname $(realpath "$0"))
# TODO: Sensible fallback
HTML_ROOT="${HTML_ROOT-/var/www/quinox/processing}"
INDEXFILE="${HTML_ROOT}/index.html"
SOLUTIONS_ROOT="${PROJECT_ROOT}/solutions"
MISC_ROOT="${PROJECT_ROOT}/misc"
log() {
>&2 echo "$(date) $*"
}
die() {
>&2 echo "$*"
exit 1
}
copy() {
log "Copying $1..."
cp "$1" "$HTML_ROOT"
}
join_array() {
end_of_line="$1"
shift
for element in "$@"
do
echo "$element$end_of_line"
done
}
# Sanity checks
[ -d "$HTML_ROOT" ] || die "The specified \$HTML_ROOT does not exist: $HTML_ROOT. Please create it."
[ -e "$INDEXFILE" ] && die "The file $INDEXFILE already exists, please remove it before running this command."
# Real run
[ -d "$HTML_ROOT/solutions" ] || mkdir "$HTML_ROOT/solutions"
copy "$MISC_ROOT/processing.min.js"
copy "$MISC_ROOT/p5.min.js"
copy "$SOLUTIONS_ROOT/library.js"
cd "$SOLUTIONS_ROOT"
do_processing() {
solutions_as_ul=()
for i in *.pde
do
log "Copying $i ..."
solutions_as_ul+=("<li><a href='$i.html'>$i</a></li>")
cp "$SOLUTIONS_ROOT/$i" "$HTML_ROOT/solutions/$i"
cp "$MISC_ROOT/template_processingjs.html" "$HTML_ROOT/$i.html"
sed -i "
s#%FILENAME%#$i#g;
s#%FILEPATH%#solutions/$i#g;
s#%GENERATED%#$(date)#g;
s#%TIMESTAMP%#$(stat -c "%y" "$SOLUTIONS_ROOT/$i")#g;
" "$HTML_ROOT/$i.html"
done
solutions_as_ul_html=$(join_array '' "${solutions_as_ul[@]}")
echo "$solutions_as_ul_html"
}
do_p5() {
solutions_as_ul=()
for i in exercise*.js
do
log "Copying $i ..."
solutions_as_ul+=("<li><a href='$i.html'>$i</a></li>")
cp "$SOLUTIONS_ROOT/$i" "$HTML_ROOT/solutions/$i"
cp "$MISC_ROOT/template_p5.html" "$HTML_ROOT/$i.html"
sed -i "
s#%FILENAME%#$i#g;
s#%FILEPATH%#solutions/$i#g;
s#%GENERATED%#$(date)#g;
s#%TIMESTAMP%#$(stat -c "%y" "$SOLUTIONS_ROOT/$i")#g;
" "$HTML_ROOT/$i.html"
done
solutions_as_ul_html=$(join_array '' "${solutions_as_ul[@]}")
echo "$solutions_as_ul_html"
}
# If tac isn't available use echo; the list won't be reversed but at least the
# HTML will be generated
solutions_as_ul_reversed=$( { do_processing; do_p5; } | tac || echo "$solutions_as_ul_html" )
solutions_as_ul_reversed_no_newlines=$(echo "$solutions_as_ul_reversed" | tr -d '\n')
log "Generating index.html ..."
cp "${MISC_ROOT}/index.html" "$HTML_ROOT/index.html"
sed -i "
s#%SOLUTIONS%#${solutions_as_ul_reversed_no_newlines}#g;
s#%GENERATED%#$(date)#g;
" "$HTML_ROOT/index.html"
log "Done."