-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5p-view
executable file
·84 lines (78 loc) · 2.77 KB
/
h5p-view
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
#!/bin/bash
#===================================================================================
#
# FILE: h5p-view
#
# USAGE: h5p-view -f <h5p-FILEname> -s <SERVER initiator: python or php>
#
# Example: e.g. h5p-view -f question-set-616.h5p -s php
# e.g. h5p-view -f question-set-616.h5p -s python
#
# DESCRIPTION: Used as an H5P file player/viewer at a linux command line.
# After the command is issued, a temporary directory
# (/tmp/h5ptemp/h5p) is created and a server is started
# (using either python or php) at port 8000. The temporary
# folder created serves as the root directory for the server.
# Ctrl-C stops the server and erases the temporary files.
# This uses h5p-standalone (by https://github.com/tunapanda)
# which is an essential need for this script to work.
#
# AUTHOR: J.L.A. Uro (justineuro@gmail.com)
# VERSION: 0.0.1
# LICENSE: Creative Commons Attribution 4.0 International License (CC-BY)
# CREATED: 2023/03/25 13:33:55
# REVISION: 2024/06/19 18:49:04
#==================================================================================
# parse through the issued command to get
# the h5p filename and type of server
while getopts ":f:s:" flag; do
case $flag in
f)
FILE=$OPTARG;;
s)
SERVER=$OPTARG;;
esac
done
# create the temporary folder: /tmp/h5temp
# ensure it is an empty folder then extract the contents of the
# h5p file into this folder
if [ -d /tmp/h5ptemp ]; then
rm -fr /tmp/h5ptemp
fi
mkdir -p /tmp/h5ptemp/h5p
unzip $FILE -d /tmp/h5ptemp/h5p >/dev/null
# make a soft link from the h5p-standalone dist file to /tmp/h5ptemp;
# the dist dir should already be in the directory from which you are
# running this script
DIST=$PWD/dist
ln -s $DIST /tmp/h5ptemp
# change directory to /tmp/h5ptemp, the root directory of the server,
# then create an index.html file giving instructions of how to play
# the h5p file (see h5p-standalone documentation)
cd /tmp/h5ptemp
cat > index.html << EOT
<div id='h5p-container'></div>
<script type="text/javascript" src="dist/main.bundle.js"></script>
<script type="text/javascript">
const el = document.getElementById('h5p-container');
const options = {
h5pJsonPath: 'h5p',
frameJs: 'dist/frame.bundle.js',
frameCss: 'dist/styles/h5p.css',
customJs: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-AMS-MML_HTMLorMML',
}
new H5PStandalone.H5P(el, options);
</script>
EOT
# start server at 0.0.0.0:8000 either using PHP or Python
case $SERVER in
python)
python -m http.server;;
php)
php -S 0.0.0.0:8000;;
esac
# Pless Ctrl-C at the linux terminal in which the server is run
# to stop the server
# remove the temporary directory when done playing/viewing the h5p file
rm -fr /tmp/h5ptemp
cd $OLDPWD