@ysongzybl
2015-04-30T18:39:17.000000Z
字数 4170
阅读 1703
bash
comments.sh - it's used to generate author and problem description comments
Notes:
- Thecomments.shneeds xidel to grab & parse the html contents with filtering out the HTML tags. The script would check thexidelwhether installed or not in your Linux box, if not, it will download the Universal Linux Version .
- Thecomments.shcurrently can automatically detect the problem's type, if the problem isalgorithmtype, it's going to create.cppfile, if the problem isshelltype, the.shfile will be created.
1) Create a file named largestNumber.cpp, and add Copyright & Problem description
./comments.sh https://oj.leetcode.com/problems/largest-number/
2) Add Copyright & Problem description into existed file
./comments.sh https://oj.leetcode.com/problems/largest-number/ largestNumber.cpp
The comments would be generated by above examples as below:
// Source : https://oj.leetcode.com/problems/largest-number/// Author : Hao Chen// Date : 2015-01-25/************************************************************************************ Given a list of non negative integers, arrange them such that they form the largest number.** For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.** Note: The result may be very large, so you need to return a string instead of an integer.** Credits:Special thanks to @ts for adding this problem and creating all test cases.***********************************************************************************/
#!/bin/bashset -eAUTHOR="Hao Chen"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"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"echo -e " ${0} https://oj.leetcode.com/problems/largest-number/"echo -e ""echo -e " 2) Add Copyright & Problem description into existed file"echo -e " ${0} https://oj.leetcode.com/problems/largest-number/ largestNumber.cpp"echo -e ""}function install_xidel(){echo "Install xidel ..."if [ ! -d ./xidel ]; thenmkdir xidelficd xidellinux=`uname -m`xidel_tar=xidel-0.8.4.linux64.tar.gzcase $linux inx86_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;esacif [ ! -f ${xidel_tar} ]; thenecho "Downloading xidel....."curl -L http://softlayer-sng.dl.sourceforge.net/project/videlibri/Xidel/Xidel%200.8.4/${xidel_tar} -o ${xidel_tar}fitar -zxvf ${xidel_tar}./install.shcd ..echo "Install xidel successfullly !"}if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; thenusageexit 255fiif [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; thenLEETCODE_URL=${LEETCODE_OLD_URL}fiIS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l`if [ ${IS_SHELL} -gt 0 ]; thenCOMMENT_TAG='#'FILE_EXT='.sh'fileetcode_url=$1current_time=`date +%Y-%m-%d`if [ $# -gt 1 ] && [ -f $2 ]; thensource_file=$2current_time=`stat -c %x ${source_file} | awk '{print \$1}'`elsesource_file=${1#${LEETCODE_URL}}source_file=${source_file::${#source_file}-1}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}if [ ! -f ${source_file} ]; thenecho "Create a new file - ${source_file}."echo -e "\n" > ${source_file}current_time=`date +%Y-%m-%d`elsecurrent_time=`stat -c %x ${source_file} | awk '{print \$1}'`fifi#adding the Copyright Commentsif ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; thensed -i '1i\'"${COMMENT_TAG} Source : ${leetcode_url}" $source_filesed -i '2i\'"${COMMENT_TAG} Author : ${AUTHOR}" $source_filesed -i '3i\'"${COMMENT_TAG} Date : ${current_time}\n" $source_filefi#grab the problem description and add the commentsxidel=`type -P xidel || /bin/true`if [ -z "${xidel}" ]; thenecho "xidel not found !"install_xidelficase $FILE_EXT in.cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \grep -v ' ' |sed '/^$/N;/^\n$/D' | \sed 's/^/ * /' | sed "1i/*$(printf '%.0s*' {0..80}) \n * " | \sed "\$a \ $(printf '%.0s*' {0..80})*/\n" > /tmp/tmp.txt;;.sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \grep -v ' ' |sed '/^$/N;/^\n$/D' | \sed 's/^/# /' | sed "1i#$(printf '%.0s#' {0..80}) \n# " | \sed "\$a \#$(printf '%.0s#' {0..80})\n" > /tmp/tmp.txt;;* ) echo "Bad file extension!"exit 1;esacsed -i '4 r /tmp/tmp.txt' ${source_file}rm -f /tmp/tmp.txtecho "${source_file} updated !"