发出来顺便试一下新写的编辑器代码高亮插件。。。
目前测试结果是:
chrome 23.0.1271.97/FF16.0.1/FF17.0.1 完全符合预期
IE9+ 显示正常,但选择文字会把行号选中以及多出一些空白行
IE8- 不支持
#!/bin/bash
PROG=`basename $0`
_color_echo() {
tput setaf "$1"
shift
echo "$@"
tput sgr0
}
_yes_no() {
_color_echo 3 "$1 (y/n)"
local yesno
read yesno
while true
do
case "$yesno" in
[Yy])
return 0
;;
[Nn])
return 1
;;
*)
echo "Please input y or n!"
read yesno
;;
esac
done
}
_usage() {
echo Usage:
echo
echo "$PROG [-h|--help] [-f] [-d] [-t {js|css}|--type={js|css}] [-o DST] SRC[...]"
echo
echo "Options:"
echo -e " -h\t--help\t\tshow help"
echo -e " -t\t--type={js|css}\tspecify the source file type"
echo -e " -f\t\t\tforce overwriting existing file"
echo -e " -d\t\t\toutput to STDOUT"
echo -e " -o DST\t\t\toutput results into file DST"
echo
echo -n "When you specify only one SRC "
_color_echo 2 -n a.js
echo " (and no -o),"
echo -n "the output will be "
_color_echo 2 -n a.min.js
echo " in the same directory."
echo
echo "When there are more than one SRC, you MUST"
echo "either specify the DST(using -o) or specify"
echo "the -d parameter."
echo
echo "The file type will be auto-detected by the extension"
echo "of the file if not specified."
return 0
}
_is_js_file() {
for each in $@
do
[ "${each:`expr ${#each} - 3`}" != ".js" ] && return 1
done
return 0
}
_is_css_file() {
for each in $@
do
[ "${each:`expr ${#each} - 4`}" != ".css" ] && return 1
done
return 0
}
_is_readable_file() {
for each in $@
do
[ ! -f $each ] && _color_echo 1 "$each is not a regular file!" && return 1
[ ! -r $each ] && _color_echo 1 "$each is not a readable file!" && return 1
done
return 0
}
_is_writable_file() {
for each in $@
do
[ -e $each ] && [ ! -f $each ] && _color_echo 1 "$each is not a regular file! Please specify another one!" && return 1
[ -f $each ] && [ ! -w $each ] && _color_echo 1 "$each is not a writable file! Please specify another one!" && return 1
done
return 0
}
_parse_args() {
if [ $# -eq 0 ]
then
_usage
exit 0
fi
ARGS=(`getopt -uo o:fhdt: -l help,type: -n $PROG -- "$@"`) || exit 1
while [ ${#ARGS[@]} -gt 0 ]
do
EACH=${ARGS[0]}
ARGS=(${ARGS[@]:1})
case $EACH in
-h|--help)
_usage
exit 0
;;
-f)
FORCE_OVERWRITE="1"
;;
-d)
TO_STDOUT="1"
;;
-o)
OUTPUT=${ARGS[0]}
ARGS=(${ARGS[@]:1})
;;
-t|--type)
TYPE=${ARGS[0]}
ARGS=(${ARGS[@]:1})
;;
--)
break
;;
*)
_color_echo 1 "Internal error!"
exit 1
;;
esac
done
if [ ${#ARGS[@]} -eq 0 ]
then
_color_echo 1 "You must specify at least one input file!"
exit 1
fi
if [ "x$TO_STDOUT" = "x" ] && [ ${#ARGS[@]} -gt 1 ] && [ "x$OUTPUT" = "x" ]
then
_color_echo 1 "You must specify the output file when there are more than one input file!"
exit 1
fi
INPUT=(${ARGS[@]})
}
_parse_args $@
_is_readable_file ${INPUT[@]} || exit 1
case "x$TYPE" in
xjs)
;;
xcss)
;;
x)
if (_is_js_file ${INPUT[@]})
then
TYPE="js"
elif (_is_css_file ${INPUT[@]})
then
TYPE="css"
else
_color_echo 1 "Can't detect the file type! You must specify it yourself!"
exit 1
fi
;;
*)
_color_echo 1 -n "The type can only be "
_color_echo 2 -n "js"
_color_echo 1 -n " or "
_color_echo 2 -n "css"
_color_echo 1 "!!"
exit 1
;;
esac
if [ ${#INPUT[@]} -eq 1 ] && [ "x$OUTPUT" = "x" ]
then
OUTPUT=${INPUT%.$TYPE}.min.$TYPE
fi
_is_writable_file $OUTPUT || exit 1
if [ -f $OUTPUT ] && [ "x$FORCE_OVERWRITE" != "x1" ] && [ "x$TO_STDOUT" = "x" ]
then
_yes_no "Overwrite existing file $OUTPUT?" || exit 0
fi
if [ "x$TYPE" = "xjs" ]
then
CMD="java -jar `dirname $0`/closurecompiler.jar --js ${INPUT[@]} 2>/dev/null"
RESULT=`echo $CMD|bash`
if [ $? -ne 0 ]
then
_color_echo 1 "Compress error!"
exit 1
fi
RESULT=`echo $RESULT|tr -d '\n'`
elif [ "x$TYPE" = "xcss" ]
then
for each in ${INPUT[@]}
do
CMD="java -jar `dirname $0`/yuicompressor.jar --type css $each 2>/dev/null"
RESULT=$RESULT`echo $CMD|bash`
if [ $? -ne 0 ]
then
_color_echo 1 -n "Compressing "
_color_echo 2 -n "$each"
_color_echo 1 " error!"
exit 1
fi
done
RESULT=`echo $RESULT|tr -d '\n'`
else
_color_echo 1 "Internal error!"
exit 1
fi
if [ "x$TO_STDOUT" = "x" ]
then
echo $RESULT > $OUTPUT
else
echo $RESULT
fi