-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.sh
executable file
·287 lines (201 loc) · 7.35 KB
/
launch.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env bash
echo "❌❌❌ TODO: FIX RM -RF ROOT BUG ON LINE 212!!!"
echo "❌❌❌ EXITING..."
exit 1
# ✅ TODO: Silent erroring if different Python version
# TODO: Error message & usage info if Notion archive is not found
# ✅ TODO: Race condition in run server & open in browser
# TODO: Message if no wifi is connected
# TODO: <meta viewport> injection for mobile screens
unzip_archive() {
filename=$(basename "$1" .zip)
dirname=$(dirname "$1")
path="$dirname/$filename"
rm -rf "$path"
mkdir "$path"
tar -xf "$1" -C "$path"
echo "$path"
}
create_index_file() {
index_file="$2/index.txt"
header="<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Teleprompter</title>
</head>
<body class='teleprompter-index'>
<h1>$(basename "$2").zip</h1>"
footer="$1</body></html>"
echo "$header" > "$index_file"
generate_index_recursively_from_filetree "$index_file" "$2" "$2"
echo "$footer" >> "$index_file"
mv "$index_file" "$2/index.html"
}
generate_index_recursively_from_filetree() {
index_file="$1"
current_dir="$2"
link_replacement="$3"
echo "<ul>" >> "$index_file"
for page_file in "$current_dir"/*.html; do
if [[ -f "$page_file" ]]; then
page_name=$(extract_page_name_from_html "$page_file")
page_folder=$(basename "$page_file" .html)
link_url=$(echo "$page_file" | sed -e "s,$link_replacement/,,g")
echo "<li><a href='$link_url'>$page_name</a>" >> "$index_file"
if [ -d "$current_dir/$page_folder" ]; then
generate_index_recursively_from_filetree "$index_file" "$current_dir/$page_folder" "$link_replacement"
fi
echo "</li>" >> "$index_file"
fi
done
echo "</ul>" >> "$index_file"
}
extract_page_name_from_html() {
file="$1"
xpath_emoji="string(//header//span[@class='icon'])"
xpath_title="string(//*[@class='page-title'])"
emoji_string=$(xmllint --xpath "$xpath_emoji" "$file")
title_string=$(xmllint --xpath "$xpath_title" "$file")
echo "$emoji_string $title_string"
}
get_code_injection() {
echo "\
<link rel='stylesheet' href='/teleprompter/teleprompter.css'/>\
<script src='/teleprompter/socket.io.js'></script>\
<script src='/teleprompter/teleprompter.babel.js'></script>"
}
patch_notion_files_recursively() {
for html_file in "$2"/*.html; do
echo "Patching $html_file"
perl -pi -e "s,</body></html>,$1</body></html>,g" "$html_file"
done
for folder in "$2"/*; do
if [ -d "$folder" ]; then
patch_notion_files_recursively "$1" "$folder"
fi
done
}
install_dependencies() {
npm install --prefix "$1"
}
link_teleprompter_files() {
mkdir "$2/teleprompter"
ln -s "$1/node_modules/socket.io/client-dist/socket.io.js" "$2/teleprompter/socket.io.js"
ln -s "$1/static/teleprompter.css" "$2/teleprompter/teleprompter.css"
}
compile_teleprompter_javascript(){
npx babel "$1/static/teleprompter.js" \
--out-file "$2/teleprompter/teleprompter.babel.js" \
--presets=@babel/preset-env
}
get_local_ip_address() {
ifconfig | grep "inet " | grep -Fv -m 1 127.0.0.1 | awk '{print $2}'
}
show_user_message() {
url="http://$2:$1"
dirname=$(basename "$3")
format_error="$(tput smul)$(tput setaf 006)"
format_filename="$(tput setaf 002)"
format_reset="$(tput sgr0)"
echo -e "
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠿⠛⠋⠉⢁⣤⠀⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠉⠁⢤⣴⣤⡀⠛⠿⣷⣦⠌⠁⠸⣿⣿⣿⣿
⣿⣿⣿⠿⠟⠛⠉⠁⣠⣤⣄⡘⠻⢿⣶⣤⡈⠙⠛⠃⢀⣀⣤⣴⣶⣾⣿⣿⣿⣿
⣿⣿⡆⠀⠐⠿⣷⣦⣄⠉⠛⠟⠒⢀⣈⣠⣤⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣷⠘⠟⠂⣀⣩⣤⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⡆⢉⣉⠉⢉⣉⣉⠉⢉⣉⡉⠉⣉⣉⡉⠉⣉⣉⠉⢉⣉⣉⠉⢹⣿⣿⣿
⣿⣿⣿⡇⠟⢁⣴⣿⠟⢁⣴⡿⠋⣠⣾⡿⠋⣠⣾⠟⢁⣴⣿⠟⢁⣴⢸⣿⣿⣿
⣿⣿⣿⡇⣀⣉⣉⣁⣀⣉⣉⣀⣈⣉⣉⣀⣈⣉⣁⣀⣉⣉⣁⣀⣉⣁⢸⣿⣿⣿
⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿
⣿⣿⣿⡇⣿⣿⣿⣧⣤⣤⣤⣤⣤⣤⣤⣤⣤⣤⣼⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿
⣿⣿⣿⡇⣿⣿⣿⡟⠛⠛⠛⠛⠛⣿⣿⠛⠛⠛⢻⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿
⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿
⣿⣿⣿⡇⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⢸⣿⣿⣿
⣿⣿⣿⣷⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣾⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
TELEPROMPTER IS READY
1. Connect your teleprompter device (e.g. tablet or phone)
to the same Wi-Fi as this laptop.
2. On your device, open ${format_error}${url}${format_reset}
3. You're good to go!
------
WITHOUT USING WI-FI:
- Alternatively, upload the contents of the
${format_filename}${dirname}${format_reset} folder
to any static HTML-hosting
TO READ HARDWARE SETUP TIPS:
- See ${format_error}https://github.com/egorvinogradov/notion-teleprompter${format_reset}
TO QUIT TELEPROMPTER:
- Press Ctrl+C here in the console
"
}
get_python_version(){
python -c 'import sys; version=sys.version_info[:1]; print("{0}".format(*version))'
}
open_in_browser() {
sleep 2s
url="http://localhost:$1?start=$2"
echo "Opening $url..."
open "$url"
}
get_script_path() {
path=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
echo "$path";
}
# TODO: FIX rm -rf / ERROR
#if [ -f "$1" ]; then
# ARCHIVE_PATH="$1"
#else
# if [ -f "$2" ]; then
# ARCHIVE_PATH="$1"
#
# if [[ "$1" == "--dev" ]]; then
# echo "Yes, 2 is file"
# IS_DEVELOPMENT=true
# fi
# else
# format_error="$(tput setaf 001)"
# echo "${format_error}
# Notion Export archive is not found.
#
# See how to export from Notion:
# https://github.com/egorvinogradov/notion-teleprompter#user-content-1-export-from-notion
#
# Usage:
# bash launch.sh /path/to/Export-XXXX-XXXX-XXXX.zip
# "
# exit 1
# fi
#fi
ARCHIVE_PATH="$1"
UNPACKED_PATH=$(unzip_archive "$ARCHIVE_PATH")
SCRIPT_PATH=$(get_script_path)
CODE_INJECTION=$(get_code_injection "$SCRIPT_PATH")
TELEPROMPTER_PORT=7777
TELEPROMPTER_IP_ADDRESS=$(get_local_ip_address)
PYTHON_VERSION=$(get_python_version)
echo ""
patch_notion_files_recursively "$CODE_INJECTION" "$UNPACKED_PATH"
create_index_file "$CODE_INJECTION" "$UNPACKED_PATH"
install_dependencies "$SCRIPT_PATH"
link_teleprompter_files "$SCRIPT_PATH" "$UNPACKED_PATH"
compile_teleprompter_javascript "$SCRIPT_PATH" "$UNPACKED_PATH"
show_user_message "$TELEPROMPTER_PORT" "$TELEPROMPTER_IP_ADDRESS" "$UNPACKED_PATH"
# Server Initialization
trap 'kill $pid1 $pid2' 2
node "$SCRIPT_PATH"/sync-server.js & pid1=$!
echo "Node Socket Server launched (process ID: $pid1)"
cd "$UNPACKED_PATH"
if [ "$PYTHON_VERSION" -ge 3 ]; then
echo "Python 3 detected";
python -m http.server "$TELEPROMPTER_PORT" & pid2=$!
else
echo "Python 2 detected";
python -m SimpleHTTPServer "$TELEPROMPTER_PORT" & pid2=$!
fi
echo "Python HTTP Server launched (process ID: $pid2)"
open_in_browser "$TELEPROMPTER_PORT" "$TELEPROMPTER_IP_ADDRESS"
echo ""
wait