|
2 | 2 | set -e |
3 | 3 |
|
4 | 4 | AUTHOR="Hao Chen" |
5 | | -LEETCODE_URL=https://oj.leetcode.com/problems/ |
| 5 | +LEETCODE_URL=https://leetcode.com/problems/ |
| 6 | +LEETCODE_NEW_URL=https://leetcode.com/problems/ |
| 7 | +LEETCODE_OLD_URL=https://oj.leetcode.com/problems/ |
| 8 | +COMMENT_TAG="//" |
| 9 | +FILE_EXT=".cpp" |
6 | 10 |
|
7 | 11 | function usage() |
8 | 12 | { |
@@ -46,11 +50,21 @@ function install_xidel() |
46 | 50 | echo "Install xidel successfullly !" |
47 | 51 | } |
48 | 52 |
|
49 | | -if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_URL}* ]]; then |
| 53 | +if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; then |
50 | 54 | usage |
51 | 55 | exit 255 |
52 | 56 | fi |
53 | 57 |
|
| 58 | +if [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; then |
| 59 | + LEETCODE_URL=${LEETCODE_OLD_URL} |
| 60 | +fi |
| 61 | + |
| 62 | +IS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l` |
| 63 | +if [ ${IS_SHELL} -gt 0 ]; then |
| 64 | + COMMENT_TAG='#' |
| 65 | + FILE_EXT='.sh' |
| 66 | +fi |
| 67 | + |
54 | 68 |
|
55 | 69 | leetcode_url=$1 |
56 | 70 | current_time=`date +%Y-%m-%d` |
@@ -61,7 +75,7 @@ if [ $# -gt 1 ] && [ -f $2 ]; then |
61 | 75 | else |
62 | 76 | source_file=${1#${LEETCODE_URL}} |
63 | 77 | source_file=${source_file::${#source_file}-1} |
64 | | - source_file=`echo $source_file | awk -F '-' '{for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }'`.cpp |
| 78 | + 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} |
65 | 79 |
|
66 | 80 | if [ ! -f ${source_file} ]; then |
67 | 81 | echo "Create a new file - ${source_file}." |
|
73 | 87 | fi |
74 | 88 |
|
75 | 89 | #adding the Copyright Comments |
76 | | -if ! grep -Fq "// Author :" $source_file ; then |
77 | | - sed -i '1i\'"// Source : ${leetcode_url}" $source_file |
78 | | - sed -i '2i\'"// Author : ${AUTHOR}" $source_file |
79 | | - sed -i '3i\'"// Date : ${current_time}\n" $source_file |
| 90 | +if ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; then |
| 91 | + sed -i '1i\'"${COMMENT_TAG} Source : ${leetcode_url}" $source_file |
| 92 | + sed -i '2i\'"${COMMENT_TAG} Author : ${AUTHOR}" $source_file |
| 93 | + sed -i '3i\'"${COMMENT_TAG} Date : ${current_time}\n" $source_file |
80 | 94 | fi |
81 | 95 |
|
82 | 96 | #grab the problem description and add the comments |
@@ -86,14 +100,27 @@ if [ -z "${xidel}" ]; then |
86 | 100 | install_xidel |
87 | 101 | fi |
88 | 102 |
|
89 | | -xidel ${leetcode_url} -q -e "css('div.question-content')" | \ |
90 | | - grep -v ' ' |sed '/^$/N;/^\n$/D' | \ |
91 | | - sed 's/^/ * /' | sed "1i/*$(printf '%.0s*' {0..80}) \n * " | \ |
92 | | - sed "\$a \ $(printf '%.0s*' {0..80})*/\n" > /tmp/tmp.txt |
| 103 | +case $FILE_EXT in |
| 104 | + .cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \ |
| 105 | + grep -v ' ' |sed '/^$/N;/^\n$/D' | \ |
| 106 | + sed 's/^/ * /' | sed "1i/*$(printf '%.0s*' {0..80}) \n * " | \ |
| 107 | + sed "\$a \ $(printf '%.0s*' {0..80})*/\n" > /tmp/tmp.txt |
| 108 | + ;; |
| 109 | + .sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \ |
| 110 | + grep -v ' ' |sed '/^$/N;/^\n$/D' | \ |
| 111 | + sed 's/^/# /' | sed "1i#$(printf '%.0s#' {0..80}) \n# " | \ |
| 112 | + sed "\$a \#$(printf '%.0s#' {0..80})\n" > /tmp/tmp.txt |
| 113 | + ;; |
| 114 | + * ) echo "Bad file extension!" |
| 115 | + exit 1; |
| 116 | + |
| 117 | +esac |
93 | 118 |
|
94 | 119 | sed -i '4 r /tmp/tmp.txt' ${source_file} |
95 | 120 |
|
96 | 121 | rm -f /tmp/tmp.txt |
97 | 122 |
|
98 | 123 | echo "${source_file} updated !" |
99 | 124 |
|
| 125 | + |
| 126 | + |