-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean-html-js.sh
184 lines (165 loc) · 3.49 KB
/
clean-html-js.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
#!/bin/bash
# clean-html-js.sh
VER="1.4"
tproc=`basename $0`
echo -e "$tproc version $VER"
echo ""
usage()
{
tproc=`basename $0`
echo -e "usage:"
echo -e " bash $tproc input.html"
echo "options:"
echo " -c clean comment (default = false);"
echo " -i iconvert (default = false);"
echo " -n not verify type file (default = false);"
echo " -o str output path (default = input.utf8.html);"
echo " -r rewrite file (default = false);"
echo " -h help."
echo
exit 1
}
testcomponent()
{
tnocomp=""
tcomp="iconv"
[ ! "$(command -v $tcomp)" ] && tnocomp="$tnocomp $tcomp"
tcomp="enca"
[ ! "$(command -v $tcomp)" ] && tnocomp="$tnocomp $tcomp"
tcomp="sed"
[ ! "$(command -v $tcomp)" ] && tnocomp="$tnocomp $tcomp"
tcomp="grep"
[ ! "$(command -v $tcomp)" ] && tnocomp="$tnocomp $tcomp"
if [ "x$tnocomp" != "x" ]
then
echo "Not found:${tnocomp}!"
exit 1
fi
}
main()
{
while getopts ":cino:rh" opt
do
case $opt in
c) tcomment="1"
;;
i) ticonv="1"
;;
n) tnoverify="1"
;;
o) tdest="$OPTARG"
;;
r) trewrite="1"
;;
h) usage
;;
*) echo "Unknown option -$OPTARG"
exit 1
;;
esac
done
shift "$(($OPTIND - 1))"
SRCNAME="$1"
if [ -z "$SRCNAME" ]
then
usage
fi
if [ -z "$tnoverify" ]
then
thtml=`file "$SRCNAME" | grep "HTML document"`
else
thtml="HTML document"
fi
if [ "+$thtml" != "+" ]
then
NEWNAME="${SRCNAME%.htm*}"
NEWNAME="$NEWNAME.utf8.html"
echo " $SRCNAME -> $NEWNAME"
if [ -z "$ticonv" ]
then
cat "$SRCNAME" > "$NEWNAME"
else
CODENAME=$(enca -i "$SRCNAME")
if [ "+$CODENAME" = "+" -o "+$CODENAME" = "+???" ]
then
CODENAME="UTF-8"
elif [ "+$CODENAME" != "+UTF-8" ]
then
echo " Convert: $CODENAME -> UTF8"
fi
cat "$SRCNAME" | iconv -c -f ${CODENAME} -t UTF8 > "$NEWNAME"
fi
echo -e -n "[4]: 0.."
echo -e -n "1.."
sed -i -e '
s/\x0D$//
s/<body/\n\L&/ig
s/<iframe/\n\L&/ig
s/<object/\n\L&/ig
s/<d/\n\L&/ig
s/<h/\n\L&/ig
s/<l/\n\L&/ig
s/<m/\n\L&/ig
s/<p/\n\L&/ig
s/<t/\n\L&/ig
s/<\/body/\n\L&/ig
s/<\/h/\n\L&/ig
s/<script/\n\L&/ig
s/<\/script>/\n\L&\n/ig
s/<noscript/\n\L&/ig
s/<\/noscript>/\n\L&\n/ig
s/<iframe/\n\L&/ig
s/<\/iframe>/\n\L&\n/ig
s/<object/\n\L&/ig
s/<\/object>/\n\L&\n/ig
s/<ins/\n\L&/ig
s/<\/ins>/\n\L&\n/ig
s/<\!--/\n\L&/ig
s/-->/\n\L&\n/ig
' "$NEWNAME"
echo -e -n "2.."
sed -i -e '
/^<script/,/^<\/script>/d
/^<noscript/,/^<\/noscript>/d
/^<iframe/,/^<\/iframe>/d
/^<object/,/^<\/object>/d
/^<ins/,/^<\/ins>/d
' "$NEWNAME"
echo -e -n "3.."
if [ ! -z "$tcomment" ]
then
sed -i -e '
/^<\!--/,/^-->/d
' "$NEWNAME"
fi
echo -e -n "4.."
if [ ! -z "$ticonv" ]
then
sed -i -e '
s/content="text\/html; charset=.*">/content="text\/html; charset=utf-8">/ig
s/[ \t]*$//
/^$/d
' "$NEWNAME"
fi
flgpre=`grep -i "<pre" "$NEWNAME"`
if [ "+$flgpre" = "+" ]
then
echo -e -n "(5).."
sed -i -e '
s/^[ \t]*//
/^$/d
' "$NEWNAME"
fi
echo -e "END"
if [ ! -z "$trewrite" ]
then
mv -fv "$NEWNAME" "$SRCNAME"
fi
exit 0
else
file "$1"
exit 1
fi
}
testcomponent
main "$@"