2015-01-25 23:23:56 +08:00
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
|
2016-05-29 15:58:40 +08:00
|
|
|
AUTHOR="NOBODY"
|
2015-03-31 15:08:20 +08:00
|
|
|
LEETCODE_URL=https://leetcode.com/problems/
|
|
|
|
LEETCODE_NEW_URL=https://leetcode.com/problems/
|
|
|
|
LEETCODE_OLD_URL=https://oj.leetcode.com/problems/
|
|
|
|
COMMENT_TAG="//"
|
|
|
|
FILE_EXT=".cpp"
|
2015-01-25 23:23:56 +08:00
|
|
|
|
|
|
|
function usage()
|
|
|
|
{
|
|
|
|
|
|
|
|
echo -e "Usage: ${0} [url] [source_file]"
|
|
|
|
echo -e ""
|
|
|
|
echo -e "Example:"
|
|
|
|
echo -e ""
|
|
|
|
echo -e " 1) Create a file named largestNumber.cpp, and add Copyright & Problem description"
|
2015-10-19 16:12:50 +08:00
|
|
|
echo -e " ${0} https://leetcode.com/problems/largest-number/"
|
2015-01-25 23:23:56 +08:00
|
|
|
echo -e ""
|
|
|
|
echo -e " 2) Add Copyright & Problem description into existed file"
|
2015-10-19 16:12:50 +08:00
|
|
|
echo -e " ${0} https://leetcode.com/problems/largest-number/ largestNumber.cpp"
|
2015-01-25 23:23:56 +08:00
|
|
|
echo -e ""
|
|
|
|
}
|
|
|
|
|
2016-05-29 15:58:40 +08:00
|
|
|
function get_author_name()
|
|
|
|
{
|
|
|
|
TRUE_CMD=`which true`
|
|
|
|
git=`type -P git || ${TRUE_CMD}`
|
|
|
|
if [ ! -z "${git}" ]; then
|
|
|
|
AUTHOR=`git config --get user.name`
|
|
|
|
else
|
|
|
|
AUTHOR=`whoami`
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 16:12:50 +08:00
|
|
|
function detect_os()
|
|
|
|
{
|
|
|
|
platform='unknown'
|
|
|
|
ostype=`uname`
|
|
|
|
if [[ "$ostype" == 'Linux' ]]; then
|
|
|
|
platform='linux'
|
|
|
|
elif [[ "$ostype" == 'Darwin' ]]; then
|
|
|
|
platform='macos'
|
|
|
|
fi
|
|
|
|
echo ${platform}
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:23:56 +08:00
|
|
|
function install_xidel()
|
|
|
|
{
|
|
|
|
echo "Install xidel ..."
|
|
|
|
if [ ! -d ./xidel ]; then
|
|
|
|
mkdir xidel
|
|
|
|
fi
|
|
|
|
cd xidel
|
2015-10-19 16:12:50 +08:00
|
|
|
|
|
|
|
platform=`detect_os`
|
|
|
|
|
|
|
|
if [[ "$platform" == "unknown" ]]; then
|
|
|
|
echo "Unknown platform, please install 'xidel' manually!"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
#install the xidel on Linux platform
|
|
|
|
if [[ "$platform" == "linux" ]]; then
|
|
|
|
hardware=`uname -m`
|
|
|
|
xidel_tar=xidel-0.8.4.linux64.tar.gz
|
|
|
|
case $hardware in
|
|
|
|
x86_64 ) xidel_tar=xidel-0.8.4.linux64.tar.gz
|
|
|
|
;;
|
|
|
|
i686 ) xidel_tar=xidel-0.8.4.linux32.tar.gz
|
|
|
|
;;
|
|
|
|
* ) echo "Cannot install xidel, please install it manually!"
|
|
|
|
exit 1;
|
|
|
|
esac
|
|
|
|
if [ ! -f ${xidel_tar} ]; then
|
|
|
|
echo "Downloading xidel......"
|
|
|
|
curl -L http://softlayer-sng.dl.sourceforge.net/project/videlibri/Xidel/Xidel%200.8.4/${xidel_tar} -o ${xidel_tar}
|
|
|
|
fi
|
|
|
|
tar -zxvf ${xidel_tar}
|
|
|
|
./install.sh
|
2015-01-25 23:23:56 +08:00
|
|
|
fi
|
2015-10-19 16:12:50 +08:00
|
|
|
|
|
|
|
#install the xidel on MacOS platform
|
|
|
|
#refer to: https://www.evernote.com/shard/s69/sh/ff1e78f3-a369-4855-b18f-6184ce789c45/f3511927d0fb356ce883835f2eb712e0
|
|
|
|
if [[ "$platform" == "macos" ]]; then
|
|
|
|
echo "Downloading xidel......"
|
|
|
|
xidel_zip=xidel.zip
|
|
|
|
if [ ! -f ${xidel_zip} ]; then
|
|
|
|
curl -L https://www.evernote.com/shard/s69/sh/ff1e78f3-a369-4855-b18f-6184ce789c45/f3511927d0fb356ce883835f2eb712e0/res/9f156868-01b4-4838-9c2f-935d7a236e05/${xidel_zip} -o ${xidel_zip}
|
|
|
|
fi
|
|
|
|
unzip ${xidel_zip}
|
|
|
|
mv xidel /usr/local/bin/
|
|
|
|
fi
|
|
|
|
|
2015-01-25 23:23:56 +08:00
|
|
|
cd ..
|
|
|
|
echo "Install xidel successfullly !"
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:12:50 +08:00
|
|
|
|
2015-03-31 15:08:20 +08:00
|
|
|
if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; then
|
2015-01-25 23:23:56 +08:00
|
|
|
usage
|
|
|
|
exit 255
|
|
|
|
fi
|
|
|
|
|
2015-03-31 15:08:20 +08:00
|
|
|
if [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; then
|
|
|
|
LEETCODE_URL=${LEETCODE_OLD_URL}
|
|
|
|
fi
|
|
|
|
|
|
|
|
IS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l`
|
|
|
|
if [ ${IS_SHELL} -gt 0 ]; then
|
|
|
|
COMMENT_TAG='#'
|
|
|
|
FILE_EXT='.sh'
|
|
|
|
fi
|
|
|
|
|
2015-01-25 23:23:56 +08:00
|
|
|
|
|
|
|
leetcode_url=$1
|
|
|
|
current_time=`date +%Y-%m-%d`
|
2015-10-19 16:12:50 +08:00
|
|
|
platform=`detect_os`
|
2015-01-25 23:23:56 +08:00
|
|
|
|
|
|
|
if [ $# -gt 1 ] && [ -f $2 ]; then
|
|
|
|
source_file=$2
|
2015-10-19 16:12:50 +08:00
|
|
|
if [[ "$platform" == "linux" ]]; then
|
|
|
|
current_time=`stat -c %x ${source_file} | awk '{print \$1}'`
|
|
|
|
elif [[ "$platform" == "macos" ]]; then
|
|
|
|
current_time=`stat -f %a ${source_file} | xargs -I time date -r time +%Y-%m-%d`
|
|
|
|
fi
|
2015-01-25 23:23:56 +08:00
|
|
|
else
|
|
|
|
source_file=${1#${LEETCODE_URL}}
|
|
|
|
source_file=${source_file::${#source_file}-1}
|
2015-03-31 15:08:20 +08:00
|
|
|
source_file=`echo $source_file | awk -F '-' '{for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }'`${FILE_EXT}
|
2015-01-25 23:23:56 +08:00
|
|
|
|
|
|
|
if [ ! -f ${source_file} ]; then
|
|
|
|
echo "Create a new file - ${source_file}."
|
|
|
|
echo -e "\n" > ${source_file}
|
|
|
|
current_time=`date +%Y-%m-%d`
|
|
|
|
else
|
2015-10-19 16:12:50 +08:00
|
|
|
if [[ "$platform" == "linux" ]]; then
|
|
|
|
current_time=`stat -c %x ${source_file} | awk '{print \$1}'`
|
|
|
|
elif [[ "$platform" == "macos" ]]; then
|
|
|
|
current_time=`stat -f %a ${source_file} | xargs -I time date -r time +%Y-%m-%d`
|
|
|
|
fi
|
2015-01-25 23:23:56 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-07-05 00:41:16 +08:00
|
|
|
# the source file is existed but it is empty, add a line,
|
|
|
|
# otherwise it could casue the comments inserts failed.
|
|
|
|
if [ ! -s $source_file ]; then
|
|
|
|
echo "" > $source_file
|
|
|
|
fi
|
|
|
|
|
2016-05-29 15:58:40 +08:00
|
|
|
#detect the author name
|
|
|
|
get_author_name;
|
|
|
|
|
2015-01-25 23:23:56 +08:00
|
|
|
#adding the Copyright Comments
|
2015-03-31 15:08:20 +08:00
|
|
|
if ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; then
|
2015-10-19 16:12:50 +08:00
|
|
|
sed -i.bak '1i\'$'\n'"${COMMENT_TAG} Source : ${leetcode_url}"$'\n' $source_file
|
|
|
|
sed -i.bak '2i\'$'\n'"${COMMENT_TAG} Author : ${AUTHOR}"$'\n' $source_file
|
|
|
|
sed -i.bak '3i\'$'\n'"${COMMENT_TAG} Date : ${current_time}"$'\n' $source_file
|
|
|
|
sed -i.bak '4i\'$'\n'""$'\n' $source_file
|
|
|
|
rm ${source_file}.bak
|
2015-01-25 23:23:56 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
#grab the problem description and add the comments
|
2015-10-19 16:12:50 +08:00
|
|
|
TRUE_CMD=`which true`
|
|
|
|
xidel=`type -P xidel || ${TRUE_CMD}`
|
2015-01-25 23:23:56 +08:00
|
|
|
if [ -z "${xidel}" ]; then
|
|
|
|
echo "xidel not found !"
|
|
|
|
install_xidel
|
|
|
|
fi
|
|
|
|
|
2015-07-05 00:41:16 +08:00
|
|
|
# using xidel grab the problem description
|
|
|
|
# 1) the `fold` command is used to wrap the text at centain column
|
|
|
|
# 2) the last two `sed` commands are used to add the comments tags
|
2015-03-31 15:08:20 +08:00
|
|
|
case $FILE_EXT in
|
|
|
|
.cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
|
2016-01-16 21:02:16 +08:00
|
|
|
grep -v ' ' | sed '/^$/N;/^\n$/D' | fold -w 85 -s |\
|
2015-10-19 16:12:50 +08:00
|
|
|
sed 's/^/ * /' | sed '1i\'$'\n'"/*$(printf '%.0s*' {0..85}) "$'\n' |\
|
|
|
|
sed '2i\'$'\n''!@#$%'$'\n' | sed 's/!@#$%/ */' | \
|
|
|
|
sed '$a\'$'\n'"*$(printf '%.0s*' {0..85})*/"$'\n'| \
|
|
|
|
sed 's/^*/ /' > /tmp/tmp.txt
|
2015-03-31 15:08:20 +08:00
|
|
|
;;
|
|
|
|
.sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
|
2016-01-16 21:02:16 +08:00
|
|
|
grep -v ' ' |sed '/^$/N;/^\n$/D' | fold -w 85 -s| \
|
2015-10-19 16:12:50 +08:00
|
|
|
sed 's/^/# /' | sed '1i\'$'\n'"#$(printf '%.0s#' {0..85}) "$'\n' | \
|
|
|
|
sed '2i\'$'\n''#'$'\n' | sed '$a\'$'\n'"#$(printf '%.0s#' {0..85})"$'\n'\
|
|
|
|
> /tmp/tmp.txt
|
2015-03-31 15:08:20 +08:00
|
|
|
;;
|
|
|
|
* ) echo "Bad file extension!"
|
|
|
|
exit 1;
|
|
|
|
|
|
|
|
esac
|
2015-01-25 23:23:56 +08:00
|
|
|
|
2015-07-05 00:41:16 +08:00
|
|
|
#insert the problem description into the source file, and remove it
|
2015-10-19 20:32:00 +08:00
|
|
|
sed -i.bak '4 r /tmp/tmp.txt' ${source_file}
|
2015-10-19 16:12:50 +08:00
|
|
|
rm -f ${source_file}.bak
|
2015-01-25 23:23:56 +08:00
|
|
|
rm -f /tmp/tmp.txt
|
|
|
|
|
|
|
|
echo "${source_file} updated !"
|
|
|
|
|