@@ -74,12 +74,12 @@ var (
74
74
}
75
75
)
76
76
77
- func getChapterFourFileOrder () []string {
78
- solutions := util .LoadChapterFourDir ()
77
+ func getChapterFourFileOrder () ( []string , [] int ) {
78
+ solutions , solutionIds := util .LoadChapterFourDir ()
79
79
chapterFourFileOrder := []string {"_index" }
80
80
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
83
83
}
84
84
85
85
func newLabelCommand () * cobra.Command {
@@ -123,57 +123,75 @@ func newDeletePreNext() *cobra.Command {
123
123
124
124
func addPreNext () {
125
125
// Chpater one add pre-next
126
- addPreNextLabel (chapterOneFileOrder , []string {}, "" , "ChapterOne" , "ChapterTwo" )
126
+ addPreNextLabel (chapterOneFileOrder , []string {}, [] int {}, "" , "ChapterOne" , "ChapterTwo" )
127
127
// Chpater two add pre-next
128
- addPreNextLabel (chapterTwoFileOrder , chapterOneFileOrder , "ChapterOne" , "ChapterTwo" , "ChapterThree" )
128
+ addPreNextLabel (chapterTwoFileOrder , chapterOneFileOrder , [] int {}, "ChapterOne" , "ChapterTwo" , "ChapterThree" )
129
129
// Chpater three add pre-next
130
- addPreNextLabel (chapterThreeFileOrder , chapterTwoFileOrder , "ChapterTwo" , "ChapterThree" , "ChapterFour" )
130
+ addPreNextLabel (chapterThreeFileOrder , chapterTwoFileOrder , [] int {}, "ChapterTwo" , "ChapterThree" , "ChapterFour" )
131
131
// Chpater four add pre-next
132
132
//fmt.Printf("%v\n", getChapterFourFileOrder())
133
- addPreNextLabel (getChapterFourFileOrder (), chapterThreeFileOrder , "ChapterThree" , "ChapterFour" , "" )
133
+ chapterFourFileOrder , solutionIds := getChapterFourFileOrder ()
134
+ addPreNextLabel (chapterFourFileOrder , chapterThreeFileOrder , solutionIds , "ChapterThree" , "ChapterFour" , "" )
134
135
}
135
136
136
- func addPreNextLabel (order , preOrder []string , preChapter , chapter , nextChapter string ) {
137
+ func addPreNextLabel (order , preOrder []string , chapterFourIds [] int , preChapter , chapter , nextChapter string ) {
137
138
var (
138
139
exist bool
139
140
err error
140
141
res []byte
141
142
count int
142
143
)
143
- for index , v := range order {
144
+ for index , path := range order {
144
145
tmp := ""
145
146
if index == 0 {
146
147
if chapter == "ChapterOne" {
147
148
// 第一页不需要“上一章”
148
149
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 ])
149
150
} 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
+ }
151
156
}
152
157
} else if index == len (order )- 1 {
153
158
if chapter == "ChapterFour" {
154
159
// 最后一页不需要“下一页”
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 ])
156
161
} else {
157
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/\" >下一章➡️</a></p>\n " , nextChapter ) + preNextFotter
158
163
}
159
164
} 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
+ }
161
170
} 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
+ }
163
176
}
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 ))
165
183
if err != nil {
166
184
fmt .Println (err )
167
185
return
168
186
}
169
187
// 当前没有上一页和下一页,才添加
170
188
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 )
172
190
if err != nil {
173
191
fmt .Println (err )
174
192
return
175
193
}
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 )
177
195
count ++
178
196
}
179
197
}
@@ -205,30 +223,35 @@ func eofAdd(filePath string, labelString string) ([]byte, error) {
205
223
206
224
func delPreNext () {
207
225
// Chpater one del pre-next
208
- delPreNextLabel (chapterOneFileOrder , "ChapterOne" )
226
+ delPreNextLabel (chapterOneFileOrder , [] int {}, "ChapterOne" )
209
227
// Chpater two del pre-next
210
- delPreNextLabel (chapterTwoFileOrder , "ChapterTwo" )
228
+ delPreNextLabel (chapterTwoFileOrder , [] int {}, "ChapterTwo" )
211
229
// Chpater three del pre-next
212
- delPreNextLabel (chapterThreeFileOrder , "ChapterThree" )
230
+ delPreNextLabel (chapterThreeFileOrder , [] int {}, "ChapterThree" )
213
231
// Chpater four del pre-next
214
- delPreNextLabel (getChapterFourFileOrder (), "ChapterFour" )
232
+ chapterFourFileOrder , solutionIds := getChapterFourFileOrder ()
233
+ delPreNextLabel (chapterFourFileOrder , solutionIds , "ChapterFour" )
215
234
}
216
235
217
- func delPreNextLabel (order []string , chapter string ) {
236
+ func delPreNextLabel (order []string , chapterFourIds [] int , chapter string ) {
218
237
count := 0
219
- for index , v := range order {
238
+ for index , path := range order {
220
239
lineNum := 5
221
240
if index == 0 && chapter == "ChapterOne" || index == len (order )- 1 && chapter == "ChapterFour" {
222
241
lineNum = 3
223
242
}
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 ))
225
248
if err != nil {
226
249
fmt .Println (err )
227
250
return
228
251
}
229
252
// 存在才删除
230
253
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 )
232
255
count ++
233
256
}
234
257
}
0 commit comments