-
Notifications
You must be signed in to change notification settings - Fork 2
/
MDviewer.sh
executable file
·83 lines (67 loc) · 1.93 KB
/
MDviewer.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
#!/bin/bash
# (c) 2022 Leif Sawyer
# License: GPL 3.0 (see https:/akhepcat/)
# Permanent home: https:/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/MDviewer.sh
#
trap do_exit SIGINT SIGTERM SIGKILL SIGQUIT SIGABRT SIGSTOP SIGSEGV
do_exit() {
ls -alF "${FILE}"
exit 0
}
CWD=$(pwd)
FILE="markdown.html"
cat <<EOF > $FILE
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Twitter markdown in the browser</title>
</head>
<body>
<div id="form">
<form name='myform'>
<label>Select file:</label>
<select id="fileOpt" name="fileOpt" class="drop_down">
<div name='options'>
EOF
### End of Header
for i in $(/bin/ls -1 *.md | sort -h)
do
echo "<option id='fileOpt' value=\"$i\">$i</option>" >> $FILE
done
cat <<EOF >>$FILE
</div>
</select>
<button onclick="docOpen(); return false;">Open</button>
</div>
<div id="content"></div>
<script src="marked.min.js"></script>
<script>
function docOpen(){
var fSelect = document.getElementById('fileOpt');
var fileOpt = fSelect.options[fSelect.selectedIndex].value;
var client = new XMLHttpRequest();
client.open('GET', fileOpt);
client.onreadystatechange = function() {
document.getElementById('content').innerHTML = marked.parse(client.responseText);
}
client.send();
}
</script>
</body>
</html>
EOF
# END of page
if [ -n "$(which http-server)" ]
then
# Pop open a window, try not to allow raw browsing, even though we're 'chrooted' into a temp directory
http-server -id --no-dotfiles --silent -o "${FILE}"
elif [ -n "$(which python3)" ]
then
echo "browse to http://localhost:8080/${FILE}"
python3 -m http.server --directory "${CWD}"
else
echo "You'll need to copy all the .md files, as well as the following file to a webserver to view them"
fi
do_exit