-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseparateJSFromHTMFile
executable file
·185 lines (180 loc) · 6.12 KB
/
separateJSFromHTMFile
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
#! /bin/bash -
#从.htm文件中提取javascript代码(只有一段内嵌代码时),并保存到.js/目录下同名.js文件中,然后重命名.htm为.html文件,并清除内嵌javascript代码,增加js文件引用。
LC_ALL=C
path="."
jsFolderName="js"
sourceHTMLFileType=".htm"
destinationHTMLFileType=".htm"
jsBlockStartMarkStr='<script type="text/javascript">'
jsBlockStartMarkGrepReg='<script type=\"text\/javascript\">'
jsBlockStartMarkSedReg='<script\ type=\"text\/javascript\">'
### startRegion Option
#while getopts "p:t:r:f:" optname
#do
# case "$optname" in
# "p")
# path=$OPTARG
# ;;
# "s")
# sourceHTMLFileType=$OPTARG
# ;;
# "d")
# destinationHTMLFileType=$OPTARG
# ;;
# "f")
# targetFileName=$OPTARG
# ;;
# "?")
# echo "unknown option $OPTARG"
# ;;
# ":")
# echo "No argument value for option $OPTARG"
# ;;
# *)
# ## should not occur
# echo "unknown error while processing options"
# ;;
# esac
#done
### endRegion Option
## STARTRegion LongOption
# from /usr/share/doc/util-linux/examples/getopt-parse.bash
# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o p:s:d:j:m: --long path:,sourceHTMLFileType:,destinationHTMLFileType:,jsFolderName:,jsBlockStartMarkStr: \
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-p|--path) path=$2 ; shift 2 ;;
-s|--sourceHTMLFileType) sourceHTMLFileType=$2 ; shift 2 ;;
## c has an optional argument. As we are in quoted mode,
## an empty parameter will be generated if its optional
## argument is not found.
#case "$2" in
# "") echo "Option sourceHTMLFileType, no argument"; shift 2 ;;
# *) echo "Option sourceHTMLFileType, argument \`$2'"; sourceHTMLFileType=$2 ; shift 2 ;;
#esac ;;
-d|--destinationHTMLFileType) destinationHTMLFileType=$2 ; shift 2 ;;
-j|--jsFolderName) jsFolderName=$2 ; shift 2 ;;
-m|--jsBlockStartMarkStr) jsBlockStartMarkStr=$2 ; shift 2 ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
if [ 0 -ne ${#1} ];
then
echo "Remaining arguments:";
for arg do echo '--> '"\`$arg'" ; done
fi
## ENDRegion LongOption
### BEGIN TIPS
### /etc/bash_completion.d/
##function _separateJSFromHTMLFile() {
## local cur prev opts
##
## COMPREPLY=()
##
## cur="${COMP_WORDS[COMP_CWORD]}"
## prev="${COMP_WORDS[COMP_CWORD-1]}"
## opts="-h --help -f --file -o --output"
##
## if [[ ${cur} == -* ]] ; then
## COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
## return 0
## fi
##}
##complete -F _separateJSFromHTMLFile separateJSFromHTMLFile
### END TIPS
# BEGIN Tool Functions
strToGrepReg(){
local s=$1
#s=${s//\ /\\\ }
s=${s//\//\\\/}
s=${s//\"/\\\"}
echo ${s}
}
strToSedReg(){
local s=$1
#s=${s//\ /\\\ }
s=${s//\//\\\/}
s=${s//\"/\\\"}
echo ${s}
}
# END Tool Functions
# pre-format parameters
path=${path%\/}"/"
jsFolderName=${jsFolderName%%\/*}
echo "path: "$path
echo "js folder name: "$jsFolderName
jsBlockStartMarkGrepReg=$(strToGrepReg "${jsBlockStartMarkStr}")
jsBlockStartMarkSedReg=$(strToSedReg "${jsBlockStartMarkStr}")
echo "jsBlockStartMarkGrepReg "$jsBlockStartMarkGrepReg
echo "jsBlockStartMarkSedReg "$jsBlockStartMarkSedReg
successNum=0
failureNum=0
for fileName in $(grep "${jsBlockStartMarkGrepReg}" -rl --exclude=*.git* --exclude=*.svn* --exclude=*.swp --include=*${sourceHTMLFileType} ${path})
do
echo ">>--------------------------"
#pre-deal $fileName
fileName=${fileName#${path}}
echo "Found ${sourceHTMLFileType} file: ${fileName}"
if [ -w ${path}${fileName} ]
then
dos2unix -q ${path}${fileName}
else
echo "You don't have permission to modify this file( ${fileName} )";
echo "-------------------------<<";
let "failureNum=${failureNum}+1";
continue;
fi
#create folder for .js file
if [ -d ${path}${jsFolderName} ]
then
echo "Found directory: ${jsFolderName} in path: ${path}";
else
mkdir -p ${path}${jsFolderName};
echo "Made directory: ${jsFolderName} in path: ${path}";
fi
jsFileName=${fileName##*/}
jsFileName=${jsFileName%${sourceHTMLFileType##*.}}"js"
#copy javascript code from htm file and write to js file.
jsFileDir=${path}${jsFolderName}"/"${jsFileName}
#sed -n -e "/<script\ type=\"text\/javascript\">/,/<\/script>/w ${jsFileDir}" ${path}${fileName}
sed -n -e "/${jsBlockStartMarkSedReg}/,/<\/script>/w ${jsFileDir}" ${path}${fileName}
dos2unix -q ${jsFileDir}
#sed -i -e "/<script\ type=\"text\/javascript\">$/d" -e "/<\/script>$/d" ${jsFileDir}
sed -i -e "/${jsBlockStartMarkSedReg}$/d" -e "/<\/script>$/d" ${jsFileDir}
echo ".js file:"${jsFileDir}" is created."
#rename htm file
sourceFileDir=${path}${fileName}
destFileDir=${path}${fileName%${sourceHTMLFileType##*.}}${destinationHTMLFileType##*.}
if [ ${destFileDir} != ${sourceFileDir} ]
then
mv -i ${sourceFileDir} ${destFileDir}
fi
if [ -f ${sourceFileDir} ]
then
echo "${sourceFileDir} is not renamed.";
elif [ -f ${destFileDir} ]
then
echo "${sourceFileDir} is renamed as ${destFileDir}";
fileName=${fileName%${sourceHTMLFileType##*.}}${destinationHTMLFileType##*.};
else
echo "${sourceFileDir} lost!";
fi
#rm javascript code from html file
regStr='\t<script type="text/javascript" src="'${jsFolderName}'/'${jsFileName}'"></script>\n</head>'
regStr=$(strToGrepReg "${regStr}")
#sed -i -e "/<script\ type=\"text\/javascript\">/,/<\/script>/g" -e "/^$/d" -e "s/<\/head>/${regStr}/g" ${path}${fileName}
sed -i -e "/${jsBlockStartMarkSedReg}/,/<\/script>/g" -e "/^$/d" -e "s/<\/head>/${regStr}/g" ${path}${fileName}
echo "Removed js code from ${sourceHTMLFileType} file."
#sed -i -e "/<script\ type=\"text\/javascript\">/,/<\/script>/g" -e "/^$/d" ${fileName}
#sed -i -e "s/<\/head>/${regStr}/g" ${fileName}
echo "-------------------------<<"
((successNum=successNum+1))
done
echo "Done. Succeeded:${successNum}; Failed:${failureNum}."