forked from szcf-weiya/ESL-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escapeHTML.sh
48 lines (42 loc) · 1.58 KB
/
escapeHTML.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
#!/bin/bash
#
# escapeHTML.sh by Martin Wermers[1], 2018
# Written for my answer on the StackOverflow question 'Include another HTML file in a HTML file':
# https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file/15250208#15250208
#
# This work is licensed under a Creative Commons Attribution-ShareAlike 4.0
# International License. See https://creativecommons.org/licenses/by-sa/4.0 .
#
# Credits to Greg Minshall[2] for the improved sed command that also escapes
# back slashes and single quotes, which my original sed command did not
# consider.
#
# [1] https://stackoverflow.com/users/841103/tafkadasoh
# [2] https://stackoverflow.com/users/1527747/greg-minshall
# Checking correct number of arguments
if [[ "$#" == 0 ]]; then
echo 'Please enter the filename of the HTML file that should be escaped for insertion via JavaScript.'
exit 1
fi
if [[ "$#" > 1 ]]; then
echo 'Too many arguments. Please pass only one filename at a time.'
exit 1
fi
# Checking for existance of entered file
if [[ ! -f $1 ]]; then
echo "WARNING! The file you have entered does not exist!"
exit 1
fi
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
targetFile="${filename}.js"
echo "document.write('\\" > $targetFile
sed 's/\\/\\\\/g;s/^.*$/&\\/g;s/'\''/\\'\''/g' $1 >> $targetFile
echo "');" >> $targetFile
echo "The file '$1' was converted into '$targetFile'."
echo "To include it in another HTML file, just enter the following line at the desired position for insertion:"
echo ""
echo " <script src=\"$targetFile\"></script>"
echo ""
exit 0