@@ -74,12 +74,12 @@ var (
7474 }
7575)
7676
77- func getChapterFourFileOrder () []string {
78- solutions := util .LoadChapterFourDir ()
77+ func getChapterFourFileOrder () ( []string , [] int ) {
78+ solutions , solutionIds := util .LoadChapterFourDir ()
7979 chapterFourFileOrder := []string {"_index" }
8080 chapterFourFileOrder = append (chapterFourFileOrder , solutions ... )
81- fmt .Printf ("ChapterFour 中包括 _index 有 %v 个文件\n " , len (chapterFourFileOrder ))
82- return chapterFourFileOrder
81+ fmt .Printf ("ChapterFour 中包括 _index 有 %v 个文件, len(id) = %v \n " , len (chapterFourFileOrder ), len ( solutionIds ))
82+ return chapterFourFileOrder , solutionIds
8383}
8484
8585func newLabelCommand () * cobra.Command {
@@ -123,57 +123,75 @@ func newDeletePreNext() *cobra.Command {
123123
124124func addPreNext () {
125125 // Chpater one add pre-next
126- addPreNextLabel (chapterOneFileOrder , []string {}, "" , "ChapterOne" , "ChapterTwo" )
126+ addPreNextLabel (chapterOneFileOrder , []string {}, [] int {}, "" , "ChapterOne" , "ChapterTwo" )
127127 // Chpater two add pre-next
128- addPreNextLabel (chapterTwoFileOrder , chapterOneFileOrder , "ChapterOne" , "ChapterTwo" , "ChapterThree" )
128+ addPreNextLabel (chapterTwoFileOrder , chapterOneFileOrder , [] int {}, "ChapterOne" , "ChapterTwo" , "ChapterThree" )
129129 // Chpater three add pre-next
130- addPreNextLabel (chapterThreeFileOrder , chapterTwoFileOrder , "ChapterTwo" , "ChapterThree" , "ChapterFour" )
130+ addPreNextLabel (chapterThreeFileOrder , chapterTwoFileOrder , [] int {}, "ChapterTwo" , "ChapterThree" , "ChapterFour" )
131131 // Chpater four add pre-next
132132 //fmt.Printf("%v\n", getChapterFourFileOrder())
133- addPreNextLabel (getChapterFourFileOrder (), chapterThreeFileOrder , "ChapterThree" , "ChapterFour" , "" )
133+ chapterFourFileOrder , solutionIds := getChapterFourFileOrder ()
134+ addPreNextLabel (chapterFourFileOrder , chapterThreeFileOrder , solutionIds , "ChapterThree" , "ChapterFour" , "" )
134135}
135136
136- func addPreNextLabel (order , preOrder []string , preChapter , chapter , nextChapter string ) {
137+ func addPreNextLabel (order , preOrder []string , chapterFourIds [] int , preChapter , chapter , nextChapter string ) {
137138 var (
138139 exist bool
139140 err error
140141 res []byte
141142 count int
142143 )
143- for index , v := range order {
144+ for index , path := range order {
144145 tmp := ""
145146 if index == 0 {
146147 if chapter == "ChapterOne" {
147148 // 第一页不需要“上一章”
148149 tmp = "\n \n " + delLine + fmt .Sprintf ("<p align = \" right\" ><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ])
149150 } else {
150- tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一章</a></p>\n " , preChapter , preOrder [len (preOrder )- 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
151+ if chapter == "ChapterFour" {
152+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一章</a></p>\n " , preChapter , preOrder [len (preOrder )- 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/%v/\" >下一页➡️</a></p>\n " , chapter , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )+ 1 ]), order [index + 1 ]) + preNextFotter
153+ } else {
154+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一章</a></p>\n " , preChapter , preOrder [len (preOrder )- 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
155+ }
151156 }
152157 } else if index == len (order )- 1 {
153158 if chapter == "ChapterFour" {
154159 // 最后一页不需要“下一页”
155- tmp = "\n \n " + delLine + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一页</a></p>\n " , chapter , order [index - 1 ])
160+ tmp = "\n \n " + delLine + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/%v/ \" >⬅️上一页</a></p>\n " , chapter , util . GetChpaterFourFileNum ( chapterFourIds [( index - 1 ) - 1 ]) , order [index - 1 ])
156161 } else {
157162 tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一页</a></p>\n " , chapter , order [index - 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/\" >下一章➡️</a></p>\n " , nextChapter ) + preNextFotter
158163 }
159164 } else if index == 1 {
160- tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/\" >⬅️上一页</a></p>\n " , chapter ) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
165+ if chapter == "ChapterFour" {
166+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/\" >⬅️上一页</a></p>\n " , chapter ) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/%v/\" >下一页➡️</a></p>\n " , chapter , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )+ 1 ]), order [index + 1 ]) + preNextFotter
167+ } else {
168+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/\" >⬅️上一页</a></p>\n " , chapter ) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
169+ }
161170 } else {
162- tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一页</a></p>\n " , chapter , order [index - 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
171+ if chapter == "ChapterFour" {
172+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/%v/\" >⬅️上一页</a></p>\n " , chapter , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )- 1 ]), order [index - 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/%v/\" >下一页➡️</a></p>\n " , chapter , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )+ 1 ]), order [index + 1 ]) + preNextFotter
173+ } else {
174+ tmp = "\n \n " + preNextHeader + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >⬅️上一页</a></p>\n " , chapter , order [index - 1 ]) + fmt .Sprintf ("<p><a href=\" https://books.halfrost.com/leetcode/%v/%v/\" >下一页➡️</a></p>\n " , chapter , order [index + 1 ]) + preNextFotter
175+ }
163176 }
164- exist , err = needAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , v ))
177+
178+ if chapter == "ChapterFour" && index > 0 {
179+ path = fmt .Sprintf ("%v/%v" , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )]), path )
180+ }
181+
182+ exist , err = needAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , path ))
165183 if err != nil {
166184 fmt .Println (err )
167185 return
168186 }
169187 // 当前没有上一页和下一页,才添加
170188 if ! exist && err == nil {
171- res , err = eofAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , v ), tmp )
189+ res , err = eofAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , path ), tmp )
172190 if err != nil {
173191 fmt .Println (err )
174192 return
175193 }
176- util .WriteFile (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , v ), res )
194+ util .WriteFile (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , path ), res )
177195 count ++
178196 }
179197 }
@@ -205,30 +223,35 @@ func eofAdd(filePath string, labelString string) ([]byte, error) {
205223
206224func delPreNext () {
207225 // Chpater one del pre-next
208- delPreNextLabel (chapterOneFileOrder , "ChapterOne" )
226+ delPreNextLabel (chapterOneFileOrder , [] int {}, "ChapterOne" )
209227 // Chpater two del pre-next
210- delPreNextLabel (chapterTwoFileOrder , "ChapterTwo" )
228+ delPreNextLabel (chapterTwoFileOrder , [] int {}, "ChapterTwo" )
211229 // Chpater three del pre-next
212- delPreNextLabel (chapterThreeFileOrder , "ChapterThree" )
230+ delPreNextLabel (chapterThreeFileOrder , [] int {}, "ChapterThree" )
213231 // Chpater four del pre-next
214- delPreNextLabel (getChapterFourFileOrder (), "ChapterFour" )
232+ chapterFourFileOrder , solutionIds := getChapterFourFileOrder ()
233+ delPreNextLabel (chapterFourFileOrder , solutionIds , "ChapterFour" )
215234}
216235
217- func delPreNextLabel (order []string , chapter string ) {
236+ func delPreNextLabel (order []string , chapterFourIds [] int , chapter string ) {
218237 count := 0
219- for index , v := range order {
238+ for index , path := range order {
220239 lineNum := 5
221240 if index == 0 && chapter == "ChapterOne" || index == len (order )- 1 && chapter == "ChapterFour" {
222241 lineNum = 3
223242 }
224- exist , err := needAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , v ))
243+ if chapter == "ChapterFour" && index > 0 {
244+ path = fmt .Sprintf ("%v/%v" , util .GetChpaterFourFileNum (chapterFourIds [(index - 1 )]), path )
245+ }
246+
247+ exist , err := needAdd (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , path ))
225248 if err != nil {
226249 fmt .Println (err )
227250 return
228251 }
229252 // 存在才删除
230253 if exist && err == nil {
231- removeLine (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , v ), lineNum + 1 )
254+ removeLine (fmt .Sprintf ("../website/content/%v/%v.md" , chapter , path ), lineNum + 1 )
232255 count ++
233256 }
234257 }
0 commit comments