8686 fi
8787fi
8888
89+ # the source file is existed but it is empty, add a line,
90+ # otherwise it could casue the comments inserts failed.
91+ if [ ! -s $source_file ]; then
92+ echo " " > $source_file
93+ fi
94+
8995# adding the Copyright Comments
9096if ! grep -Fq " ${COMMENT_TAG} Author :" $source_file ; then
9197 sed -i ' 1i\' " ${COMMENT_TAG} Source : ${leetcode_url} " $source_file
@@ -100,14 +106,17 @@ if [ -z "${xidel}" ]; then
100106 install_xidel
101107fi
102108
109+ # using xidel grab the problem description
110+ # 1) the `fold` command is used to wrap the text at centain column
111+ # 2) the last two `sed` commands are used to add the comments tags
103112case $FILE_EXT in
104113 .cpp ) xidel ${leetcode_url} -q -e " css('div.question-content')" | \
105- grep -v ' ' | sed ' /^$/N;/^\n$/D' | \
114+ grep -v ' ' | sed ' /^$/N;/^\n$/D' | fold -w 85 -s | \
106115 sed ' s/^/ * /' | sed " 1i/*$( printf ' %.0s*' {0..80}) \n * " | \
107116 sed " \$ a \ $( printf ' %.0s*' {0..80}) */\n" > /tmp/tmp.txt
108117 ;;
109118 .sh ) xidel ${leetcode_url} -q -e " css('div.question-content')" | \
110- grep -v ' ' | sed ' /^$/N;/^\n$/D' | \
119+ grep -v ' ' | sed ' /^$/N;/^\n$/D' | fold -w 85 -s | \
111120 sed ' s/^/# /' | sed " 1i#$( printf ' %.0s#' {0..80}) \n# " | \
112121 sed " \$ a \#$( printf ' %.0s#' {0..80}) \n" > /tmp/tmp.txt
113122 ;;
@@ -116,11 +125,9 @@ case $FILE_EXT in
116125
117126esac
118127
128+ # insert the problem description into the source file, and remove it
119129sed -i ' 4 r /tmp/tmp.txt' ${source_file}
120-
121130rm -f /tmp/tmp.txt
122131
123132echo " ${source_file} updated !"
124133
125-
126-
0 commit comments