|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | | - "errors" |
6 | 4 | "fmt" |
7 | | - "io" |
8 | | - "io/ioutil" |
9 | | - "os" |
10 | | - "regexp" |
11 | | - "strings" |
12 | 5 |
|
13 | 6 | "github.com/halfrost/LeetCode-Go/ctl/util" |
14 | | - "github.com/spf13/cobra" |
15 | 7 | ) |
16 | 8 |
|
| 9 | +// 上/下页导航已改由 Hugo 主题在渲染时动态生成 |
| 10 | +// (themes/book/layouts/partials/docs/inject/content-after.html), |
| 11 | +// 不再把导航 HTML 写进 markdown,因此原先 add-pre-next / del-pre-next |
| 12 | +// 这一整套读写 markdown 的逻辑已全部移除。 |
| 13 | + |
17 | 14 | var ( |
18 | 15 | chapterOneFileOrder = []string{"_index", "Data_Structure", "Algorithm", "Time_Complexity"} |
19 | 16 | chapterOneMenuOrder = []string{"_index", "#关于作者", "Data_Structure", "Algorithm", "Time_Complexity"} |
20 | 17 | chapterTwoFileOrder = []string{"_index", "Array", "String", "Two_Pointers", "Linked_List", "Stack", "Tree", "Dynamic_Programming", "Backtracking", "Depth_First_Search", "Breadth_First_Search", |
21 | 18 | "Binary_Search", "Math", "Hash_Table", "Sorting", "Bit_Manipulation", "Union_Find", "Sliding_Window", "Segment_Tree", "Binary_Indexed_Tree"} |
22 | 19 | chapterThreeFileOrder = []string{"_index", "Segment_Tree", "UnionFind", "LRUCache", "LFUCache"} |
23 | | - preNextHeader = "----------------------------------------------\n<div style=\"display: flex;justify-content: space-between;align-items: center;\">\n" |
24 | | - preNextFotter = "</div>" |
25 | | - delLine = "----------------------------------------------\n" |
26 | | - delHeader = "<div style=\"display: flex;justify-content: space-between;align-items: center;\">" |
27 | | - delLabel = "<[a-zA-Z]+.*?>([\\s\\S]*?)</[a-zA-Z]*?>" |
28 | | - delFooter = "</div>" |
29 | | - |
30 | | - //ErrNoFilename is thrown when the path the the file to tail was not given |
31 | | - ErrNoFilename = errors.New("You must provide the path to a file in the \"-file\" flag.") |
32 | | - |
33 | | - //ErrInvalidLineCount is thrown when the user provided 0 (zero) as the value for number of lines to tail |
34 | | - ErrInvalidLineCount = errors.New("You cannot tail zero lines.") |
35 | 20 |
|
36 | 21 | chapterMap = map[string]map[string]string{ |
37 | 22 | "ChapterOne": { |
@@ -82,267 +67,3 @@ func getChapterFourFileOrder() ([]string, []int) { |
82 | 67 | fmt.Printf("ChapterFour 中包括 _index 有 %v 个文件, len(id) = %v\n", len(chapterFourFileOrder), len(solutionIds)) |
83 | 68 | return chapterFourFileOrder, solutionIds |
84 | 69 | } |
85 | | - |
86 | | -func newLabelCommand() *cobra.Command { |
87 | | - mc := &cobra.Command{ |
88 | | - Use: "label <subcommand>", |
89 | | - Short: "Label related commands", |
90 | | - } |
91 | | - //mc.PersistentFlags().StringVar(&logicEndpoint, "endpoint", "localhost:5880", "endpoint of logic service") |
92 | | - mc.AddCommand( |
93 | | - newAddPreNext(), |
94 | | - newDeletePreNext(), |
95 | | - ) |
96 | | - return mc |
97 | | -} |
98 | | - |
99 | | -func newAddPreNext() *cobra.Command { |
100 | | - cmd := &cobra.Command{ |
101 | | - Use: "add-pre-next", |
102 | | - Short: "Add pre-next label", |
103 | | - Run: func(cmd *cobra.Command, args []string) { |
104 | | - addPreNext() |
105 | | - }, |
106 | | - } |
107 | | - // cmd.Flags().StringVar(&alias, "alias", "", "alias") |
108 | | - // cmd.Flags().StringVar(&appId, "appid", "", "appid") |
109 | | - return cmd |
110 | | -} |
111 | | - |
112 | | -func newDeletePreNext() *cobra.Command { |
113 | | - cmd := &cobra.Command{ |
114 | | - Use: "del-pre-next", |
115 | | - Short: "Delete pre-next label", |
116 | | - Run: func(cmd *cobra.Command, args []string) { |
117 | | - delPreNext() |
118 | | - }, |
119 | | - } |
120 | | - // cmd.Flags().StringVar(&alias, "alias", "", "alias") |
121 | | - // cmd.Flags().StringVar(&appId, "appid", "", "appid") |
122 | | - return cmd |
123 | | -} |
124 | | - |
125 | | -func addPreNext() { |
126 | | - // 上/下页导航已改由 Hugo 主题在渲染时动态生成 |
127 | | - // (themes/book/layouts/partials/docs/inject/content-after.html), |
128 | | - // 不再把导航 HTML 写进 markdown,避免与主题渲染的导航重复。 |
129 | | - // 如需恢复写入 markdown 的旧行为,取消下面的注释即可。 |
130 | | - // |
131 | | - // addPreNextLabel(chapterOneFileOrder, []string{}, []int{}, "", "ChapterOne", "ChapterTwo") |
132 | | - // addPreNextLabel(chapterTwoFileOrder, chapterOneFileOrder, []int{}, "ChapterOne", "ChapterTwo", "ChapterThree") |
133 | | - // addPreNextLabel(chapterThreeFileOrder, chapterTwoFileOrder, []int{}, "ChapterTwo", "ChapterThree", "ChapterFour") |
134 | | - // chapterFourFileOrder, solutionIds := getChapterFourFileOrder() |
135 | | - // addPreNextLabel(chapterFourFileOrder, chapterThreeFileOrder, solutionIds, "ChapterThree", "ChapterFour", "") |
136 | | -} |
137 | | - |
138 | | -func addPreNextLabel(order, preOrder []string, chapterFourIds []int, preChapter, chapter, nextChapter string) { |
139 | | - var ( |
140 | | - exist bool |
141 | | - err error |
142 | | - res []byte |
143 | | - count int |
144 | | - ) |
145 | | - for index, path := range order { |
146 | | - tmp := "" |
147 | | - if index == 0 { |
148 | | - if chapter == "ChapterOne" { |
149 | | - // 第一页不需要“上一章” |
150 | | - 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]) |
151 | | - } else { |
152 | | - if chapter == "ChapterFour" { |
153 | | - 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 |
154 | | - } else { |
155 | | - 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 |
156 | | - } |
157 | | - } |
158 | | - } else if index == len(order)-1 { |
159 | | - if chapter == "ChapterFour" { |
160 | | - // 最后一页不需要“下一页” |
161 | | - 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]) |
162 | | - } else { |
163 | | - 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 |
164 | | - } |
165 | | - } else if index == 1 { |
166 | | - if chapter == "ChapterFour" { |
167 | | - 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 |
168 | | - } else { |
169 | | - 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 |
170 | | - } |
171 | | - } else { |
172 | | - if chapter == "ChapterFour" { |
173 | | - 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 |
174 | | - } else { |
175 | | - 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 |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - if chapter == "ChapterFour" && index > 0 { |
180 | | - path = fmt.Sprintf("%v/%v", util.GetChpaterFourFileNum(chapterFourIds[(index-1)]), path) |
181 | | - } |
182 | | - |
183 | | - exist, err = needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path)) |
184 | | - if err != nil { |
185 | | - fmt.Println(err) |
186 | | - return |
187 | | - } |
188 | | - // 当前没有上一页和下一页,才添加 |
189 | | - if !exist && err == nil { |
190 | | - res, err = eofAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), tmp) |
191 | | - if err != nil { |
192 | | - fmt.Println(err) |
193 | | - return |
194 | | - } |
195 | | - util.WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), res) |
196 | | - count++ |
197 | | - } |
198 | | - } |
199 | | - fmt.Printf("添加了 %v 个文件的 pre-next\n", count) |
200 | | -} |
201 | | - |
202 | | -func eofAdd(filePath string, labelString string) ([]byte, error) { |
203 | | - f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) |
204 | | - if err != nil { |
205 | | - return nil, err |
206 | | - } |
207 | | - defer f.Close() |
208 | | - reader, output := bufio.NewReader(f), []byte{} |
209 | | - |
210 | | - for { |
211 | | - line, _, err := reader.ReadLine() |
212 | | - if err != nil { |
213 | | - if err == io.EOF { |
214 | | - output = append(output, []byte(labelString)...) |
215 | | - output = append(output, []byte("\n")...) |
216 | | - return output, nil |
217 | | - } |
218 | | - return nil, err |
219 | | - } |
220 | | - output = append(output, line...) |
221 | | - output = append(output, []byte("\n")...) |
222 | | - } |
223 | | -} |
224 | | - |
225 | | -func delPreNext() { |
226 | | - // Chpater one del pre-next |
227 | | - delPreNextLabel(chapterOneFileOrder, []int{}, "ChapterOne") |
228 | | - // Chpater two del pre-next |
229 | | - delPreNextLabel(chapterTwoFileOrder, []int{}, "ChapterTwo") |
230 | | - // Chpater three del pre-next |
231 | | - delPreNextLabel(chapterThreeFileOrder, []int{}, "ChapterThree") |
232 | | - // Chpater four del pre-next |
233 | | - chapterFourFileOrder, solutionIds := getChapterFourFileOrder() |
234 | | - delPreNextLabel(chapterFourFileOrder, solutionIds, "ChapterFour") |
235 | | -} |
236 | | - |
237 | | -func delPreNextLabel(order []string, chapterFourIds []int, chapter string) { |
238 | | - count := 0 |
239 | | - for index, path := range order { |
240 | | - lineNum := 5 |
241 | | - if index == 0 && chapter == "ChapterOne" || index == len(order)-1 && chapter == "ChapterFour" { |
242 | | - lineNum = 3 |
243 | | - } |
244 | | - if chapter == "ChapterFour" && index > 0 { |
245 | | - path = fmt.Sprintf("%v/%v", util.GetChpaterFourFileNum(chapterFourIds[(index-1)]), path) |
246 | | - } |
247 | | - |
248 | | - exist, err := needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path)) |
249 | | - if err != nil { |
250 | | - fmt.Println(err) |
251 | | - return |
252 | | - } |
253 | | - // 存在才删除 |
254 | | - if exist && err == nil { |
255 | | - removeLine(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), lineNum+1) |
256 | | - count++ |
257 | | - } |
258 | | - } |
259 | | - fmt.Printf("删除了 %v 个文件的 pre-next\n", count) |
260 | | - // 另外一种删除方法 |
261 | | - // res, err := eofDel(fmt.Sprintf("../website/content/ChapterOne/%v.md", v)) |
262 | | - // if err != nil { |
263 | | - // fmt.Println(err) |
264 | | - // return |
265 | | - // } |
266 | | - // util.WriteFile(fmt.Sprintf("../website/content/ChapterOne/%v.md", v), res) |
267 | | -} |
268 | | - |
269 | | -func needAdd(filePath string) (bool, error) { |
270 | | - f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) |
271 | | - if err != nil { |
272 | | - return false, err |
273 | | - } |
274 | | - defer f.Close() |
275 | | - reader, output := bufio.NewReader(f), []byte{} |
276 | | - for { |
277 | | - line, _, err := reader.ReadLine() |
278 | | - if err != nil { |
279 | | - if err == io.EOF { |
280 | | - return false, nil |
281 | | - } |
282 | | - return false, err |
283 | | - } |
284 | | - if ok, _ := regexp.Match(delHeader, line); ok { |
285 | | - return true, nil |
286 | | - } else if ok, _ := regexp.Match(delLabel, line); ok { |
287 | | - return true, nil |
288 | | - } else { |
289 | | - output = append(output, line...) |
290 | | - output = append(output, []byte("\n")...) |
291 | | - } |
292 | | - } |
293 | | -} |
294 | | - |
295 | | -func eofDel(filePath string) ([]byte, error) { |
296 | | - f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) |
297 | | - if err != nil { |
298 | | - return nil, err |
299 | | - } |
300 | | - defer f.Close() |
301 | | - reader, output := bufio.NewReader(f), []byte{} |
302 | | - for { |
303 | | - line, _, err := reader.ReadLine() |
304 | | - if err != nil { |
305 | | - if err == io.EOF { |
306 | | - return output, nil |
307 | | - } |
308 | | - return nil, err |
309 | | - } |
310 | | - if ok, _ := regexp.Match(delLine, line); ok { |
311 | | - reg := regexp.MustCompile(delLine) |
312 | | - newByte := reg.ReplaceAll(line, []byte("")) |
313 | | - output = append(output, newByte...) |
314 | | - output = append(output, []byte("\n")...) |
315 | | - } else if ok, _ := regexp.Match(delHeader, line); ok { |
316 | | - reg := regexp.MustCompile(delHeader) |
317 | | - newByte := reg.ReplaceAll(line, []byte("")) |
318 | | - output = append(output, newByte...) |
319 | | - output = append(output, []byte("\n")...) |
320 | | - } else if ok, _ := regexp.Match(delLabel, line); ok { |
321 | | - reg := regexp.MustCompile(delLabel) |
322 | | - newByte := reg.ReplaceAll(line, []byte("")) |
323 | | - output = append(output, newByte...) |
324 | | - output = append(output, []byte("\n")...) |
325 | | - } else if ok, _ := regexp.Match(delFooter, line); ok { |
326 | | - reg := regexp.MustCompile(delFooter) |
327 | | - newByte := reg.ReplaceAll(line, []byte("")) |
328 | | - output = append(output, newByte...) |
329 | | - output = append(output, []byte("\n")...) |
330 | | - } else { |
331 | | - output = append(output, line...) |
332 | | - output = append(output, []byte("\n")...) |
333 | | - } |
334 | | - } |
335 | | -} |
336 | | - |
337 | | -func removeLine(path string, lineNumber int) { |
338 | | - file, err := ioutil.ReadFile(path) |
339 | | - if err != nil { |
340 | | - panic(err) |
341 | | - } |
342 | | - info, _ := os.Stat(path) |
343 | | - mode := info.Mode() |
344 | | - array := strings.Split(string(file), "\n") |
345 | | - array = array[:len(array)-lineNumber-1] |
346 | | - ioutil.WriteFile(path, []byte(strings.Join(array, "\n")), mode) |
347 | | - //fmt.Println("remove line successful") |
348 | | -} |
0 commit comments