|
2 | 2 | set -e |
3 | 3 |
|
4 | 4 | AUTHOR="NOBODY" |
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 | 5 | COMMENT_TAG="//" |
9 | 6 | FILE_EXT=".cpp" |
10 | 7 |
|
| 8 | +pushd `dirname $0` > /dev/null |
| 9 | +SCRIPTPATH=`pwd -P` |
| 10 | +popd > /dev/null |
| 11 | +SCRIPTFILE=`basename $0` |
| 12 | + |
| 13 | +COLOR_INFO='\033[0;36m' |
| 14 | +COLOR_NONE='\033[0m' |
| 15 | + |
| 16 | +source ${SCRIPTPATH}/lib/query_problem.sh |
| 17 | + |
11 | 18 | function usage() |
12 | 19 | { |
13 | 20 |
|
@@ -99,6 +106,7 @@ function install_xidel() |
99 | 106 | } |
100 | 107 |
|
101 | 108 |
|
| 109 | + |
102 | 110 | if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; then |
103 | 111 | usage |
104 | 112 | exit 255 |
@@ -108,17 +116,29 @@ if [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; then |
108 | 116 | LEETCODE_URL=${LEETCODE_OLD_URL} |
109 | 117 | fi |
110 | 118 |
|
111 | | -IS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l` |
112 | | -if [ ${IS_SHELL} -gt 0 ]; then |
113 | | - COMMENT_TAG='#' |
114 | | - FILE_EXT='.sh' |
115 | | -fi |
116 | | - |
117 | 119 |
|
118 | 120 | leetcode_url=$1 |
119 | 121 | current_time=`date +%Y-%m-%d` |
120 | 122 | platform=`detect_os` |
121 | 123 |
|
| 124 | +get_question_slug ${leetcode_url} |
| 125 | + |
| 126 | + |
| 127 | +TRUE_CMD=`which true` |
| 128 | +xidel=`type -P xidel || ${TRUE_CMD}` |
| 129 | +if [ -z "${xidel}" ]; then |
| 130 | + echo "xidel not found !" |
| 131 | + install_xidel |
| 132 | +fi |
| 133 | + |
| 134 | +#grab the problem information |
| 135 | +query_problem ${leetcode_url} ${QUESTION_TITLE_SLUG} |
| 136 | + |
| 137 | +if [ ${QUESTION_CATEGORY} == "Shell" ]; then |
| 138 | + COMMENT_TAG='#' |
| 139 | + FILE_EXT='.sh' |
| 140 | +fi |
| 141 | + |
122 | 142 | if [ $# -gt 1 ] && [ -f $2 ]; then |
123 | 143 | source_file=$2 |
124 | 144 | if [[ "$platform" == "linux" ]]; then |
@@ -127,8 +147,8 @@ if [ $# -gt 1 ] && [ -f $2 ]; then |
127 | 147 | current_time=`stat -f %a ${source_file} | xargs -I time date -r time +%Y-%m-%d` |
128 | 148 | fi |
129 | 149 | else |
130 | | - source_file=${1#${LEETCODE_URL}} |
131 | | - source_file=${source_file::${#source_file}-1} |
| 150 | + source_file=$QUESTION_TITLE_SLUG |
| 151 | + #source_file=${source_file::${#source_file}-1} |
132 | 152 | 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} |
133 | 153 |
|
134 | 154 | if [ ! -f ${source_file} ]; then |
|
144 | 164 | fi |
145 | 165 | fi |
146 | 166 |
|
| 167 | + |
147 | 168 | # the source file is existed but it is empty, add a line, |
148 | 169 | # otherwise it could casue the comments inserts failed. |
149 | 170 | if [ ! -s $source_file ]; then |
@@ -162,38 +183,44 @@ if ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; then |
162 | 183 | rm ${source_file}.bak |
163 | 184 | fi |
164 | 185 |
|
165 | | -#grab the problem description and add the comments |
166 | | -TRUE_CMD=`which true` |
167 | | -xidel=`type -P xidel || ${TRUE_CMD}` |
168 | | -if [ -z "${xidel}" ]; then |
169 | | - echo "xidel not found !" |
170 | | - install_xidel |
171 | | -fi |
172 | 186 |
|
173 | | -# using xidel grab the problem description |
| 187 | + |
| 188 | +#echo "$QUESTION_CONTENT" |
| 189 | +#echo $QUESTION_DIFFICULTY |
| 190 | +#echo $QUESTION_TITLE |
| 191 | +#echo $QUESTION_ID |
| 192 | +#echo $QUESTION_CATEGORY |
| 193 | + |
| 194 | + |
| 195 | +TMP_FILE=/tmp/tmp.txt |
| 196 | + |
174 | 197 | # 1) the `fold` command is used to wrap the text at centain column |
175 | 198 | # 2) the last two `sed` commands are used to add the comments tags |
176 | 199 | case $FILE_EXT in |
177 | | - .cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \ |
178 | | - grep -v ' ' | sed '/^$/N;/^\n$/D' | fold -w 85 -s |\ |
| 200 | + .cpp ) echo "$QUESTION_CONTENT" |\ |
| 201 | + grep -v ' ' | sed '/^$/N;/^\n$/D' | fold -w 85 -s |\ |
179 | 202 | sed 's/^/ * /' | sed '1i\'$'\n'"/*$(printf '%.0s*' {0..85}) "$'\n' |\ |
180 | 203 | sed '2i\'$'\n''!@#$%'$'\n' | sed 's/!@#$%/ */' | \ |
181 | 204 | sed '$a\'$'\n'"*$(printf '%.0s*' {0..85})*/"$'\n'| \ |
182 | | - sed 's/^*/ /' > /tmp/tmp.txt |
| 205 | + sed 's/^*/ /' > ${TMP_FILE} |
183 | 206 | ;; |
184 | | - .sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \ |
| 207 | + .sh ) echo "$QUESTION_CONTENT" |\ |
185 | 208 | grep -v ' ' |sed '/^$/N;/^\n$/D' | fold -w 85 -s| \ |
186 | 209 | sed 's/^/# /' | sed '1i\'$'\n'"#$(printf '%.0s#' {0..85}) "$'\n' | \ |
187 | 210 | sed '2i\'$'\n''#'$'\n' | sed '$a\'$'\n'"#$(printf '%.0s#' {0..85})"$'\n'\ |
188 | | - > /tmp/tmp.txt |
| 211 | + > ${TMP_FILE} |
189 | 212 | ;; |
190 | 213 | * ) echo "Bad file extension!" |
191 | 214 | exit 1; |
192 | 215 |
|
193 | 216 | esac |
194 | 217 |
|
| 218 | +#remove the ^M chars |
| 219 | +cat -v ${TMP_FILE} | tr -d '^M' > ${TMP_FILE}.1 |
| 220 | +mv ${TMP_FILE}.1 ${TMP_FILE} |
| 221 | + |
195 | 222 | #insert the problem description into the source file, and remove it |
196 | | -sed -i.bak '4 r /tmp/tmp.txt' ${source_file} |
| 223 | +sed -i.bak '4 r '${TMP_FILE}'' ${source_file} |
197 | 224 | rm -f ${source_file}.bak |
198 | 225 | rm -f /tmp/tmp.txt |
199 | 226 |
|
|