-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeliver-handouts
executable file
·93 lines (79 loc) · 2.72 KB
/
deliver-handouts
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
#!/bin/bash
#
# Handout Delivery
#
webmaster= # address for email notifications
chrome="${CHROME:?Must set CHROME}"
home=home # special home page handout
set -e
set -u
path="${1:?Must specify path}"
src="${2:?Must specify source location}"
www="${3:?Must specify www location}"
handx="${4:?Must specify handx location}"
address="${5:-}"
abssrc="$(cd "$src" && pwd)"
from="$src/$path/handout"
deploy="$www/$path"
link="$address/$path/"
if [ "$path" == "$home" ]; then
# deploy home page to root
deploy="$www"
link="$address/"
fi
if [ ! -d "$from" ]; then
echo -e "[no handouts] $path in $src"
exit
fi
if [ ! -d "$deploy" ]; then
echo -e "\033[1;31m[deploy skipped]\033[0m $path directory does not exist in $www"
exit
fi
echo "[deploy] $path"
[ -n "$webmaster" ] && sendmail "$webmaster" <<EOM
Subject: [handx] updating $path
$link (handout $path) updated by $USER
$from -> $deploy
EOM
(cd "$from" && find -L . -mindepth 1 -type d) | # find directories
sed 's/^.\///' | # relative dir names
(cd "$deploy" && xargs -I DIR mkdir -p DIR) # create in www
(cd "$from" && find -L . -type f) | # find files
sed 's/^.\///' | # relative file names
while read -r file; do # for each file...
# handouts are HTML files that reference the handout script
if [[ "$file" == *.html ]] && fgrep -q handout-page.js "$from/$file"; then
# pre-render the handout
echo " render $file"
IFS=/ read -r kind handout part <<< "$path/${file%.*html}"
part="${part%index}"; part="${part%/}"
[ -f "$deploy/$file" ] && rm "$deploy/$file"
url="file://$(cd "$from" && pwd)/$file?handout-deliver=$kind/$handout/$part/"
"$chrome" --headless --disable-gpu --dump-dom "$url" |
awk '{ if (/^HANDOUT_DELIVERY\t/) { print } else { print > "'"$deploy/$file"'" } }' |
cut -f2- | (
read -r handoutid metadata
cat > "$handx/$handoutid.json" <<< "$metadata"
)
else
# just copy the file
echo " copy $file"
cp "$from/$file" "$deploy/$file"
fi
case "$file" in
*.html|*.shtml|*.svg)
# fix paths to site CSS & JavaScript
# ="../../../web/handout/handout-file" -> ="../../web/handout-file"
perl -pi -e 's#(="[^"]*)/\.\./([^"]*)/handout/([^"]*")#\1/\2/\3#g' "$deploy/$file"
if [ "$path" == "$home" ]; then
# fix relative paths from home page
# ="../web/something" -> ="web/something"
perl -pi -e 's#(=")\.\./([^"]*")#\1\2#g' "$deploy/$file"
fi
# fix paths to index files
# ="../../web/index.html" -> ="../../web/", ="dir/index.html" -> ="dir/"
perl -pi -e 's#(="(?:[^":]+/)*)index\.s?html([^"]*")#\1\2#g' "$deploy/$file"
;;
esac
done
echo -e "\033[1;32m[deployed]\033[0m $path"