Skip to content

Commit fb26e8e

Browse files
committed
add new pdf version
1 parent 9dbe915 commit fb26e8e

11 files changed

Lines changed: 79142 additions & 303 deletions

File tree

PDF v1.7.97.md

Lines changed: 79122 additions & 0 deletions
Large diffs are not rendered by default.

ctl/command.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func init() {
2323
rootCmd.AddCommand(
2424
versionCmd,
2525
newBuildCommand(),
26-
newLabelCommand(),
2726
newPDFCommand(),
2827
newRefresh(),
2928
)

ctl/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c config) String() string {
2525
func getConfig() *config {
2626
cfg := new(config)
2727
if _, err := toml.DecodeFile(configTOML, &cfg); err != nil {
28-
log.Panicf(err.Error())
28+
log.Panic(err)
2929
}
3030
// log.Printf("get config: %s", cfg)
3131
return cfg

ctl/label.go

Lines changed: 5 additions & 284 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
11
package main
22

33
import (
4-
"bufio"
5-
"errors"
64
"fmt"
7-
"io"
8-
"io/ioutil"
9-
"os"
10-
"regexp"
11-
"strings"
125

136
"github.com/halfrost/LeetCode-Go/ctl/util"
14-
"github.com/spf13/cobra"
157
)
168

9+
// 上/下页导航已改由 Hugo 主题在渲染时动态生成
10+
// (themes/book/layouts/partials/docs/inject/content-after.html),
11+
// 不再把导航 HTML 写进 markdown,因此原先 add-pre-next / del-pre-next
12+
// 这一整套读写 markdown 的逻辑已全部移除。
13+
1714
var (
1815
chapterOneFileOrder = []string{"_index", "Data_Structure", "Algorithm", "Time_Complexity"}
1916
chapterOneMenuOrder = []string{"_index", "#关于作者", "Data_Structure", "Algorithm", "Time_Complexity"}
2017
chapterTwoFileOrder = []string{"_index", "Array", "String", "Two_Pointers", "Linked_List", "Stack", "Tree", "Dynamic_Programming", "Backtracking", "Depth_First_Search", "Breadth_First_Search",
2118
"Binary_Search", "Math", "Hash_Table", "Sorting", "Bit_Manipulation", "Union_Find", "Sliding_Window", "Segment_Tree", "Binary_Indexed_Tree"}
2219
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.")
3520

3621
chapterMap = map[string]map[string]string{
3722
"ChapterOne": {
@@ -82,267 +67,3 @@ func getChapterFourFileOrder() ([]string, []int) {
8267
fmt.Printf("ChapterFour 中包括 _index 有 %v 个文件, len(id) = %v\n", len(chapterFourFileOrder), len(solutionIds))
8368
return chapterFourFileOrder, solutionIds
8469
}
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-
}

ctl/pdf.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func generatePDF() {
5858
pdf, tmp []byte
5959
err error
6060
)
61-
// 先删除 pre-next
62-
delPreNext()
61+
// 上/下页导航由 Hugo 主题渲染时动态生成,markdown 中不再写入导航 HTML,
62+
// 因此这里无需再做删除/还原导航的操作。
6363

6464
chapterFourFileOrder, _ := util.LoadChapterFourDir()
6565
totalSolutions = len(chapterFourFileOrder)
@@ -90,7 +90,6 @@ func generatePDF() {
9090
// 生成 PDF
9191
util.WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf)
9292
// 还原现场
93-
addPreNext()
9493
util.DestoryDir("./pdftemp")
9594
}
9695

@@ -106,9 +105,9 @@ func loadChapter(order []string, path, chapter string) ([]byte, error) {
106105
} else {
107106
if chapter == "ChapterFour" {
108107
if v[4] == '.' {
109-
num, err := strconv.Atoi(v[:4])
110-
if err != nil {
111-
fmt.Println(err)
108+
num, atoiErr := strconv.Atoi(v[:4])
109+
if atoiErr != nil {
110+
fmt.Println(atoiErr)
112111
}
113112
tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v/%v.md", path, chapter, util.GetChpaterFourFileNum(num), v))
114113
}

ctl/refresh.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ func newRefresh() *cobra.Command {
2020
func refresh() {
2121
//buildBookMenu()
2222
copyLackFile()
23-
delPreNext()
2423
buildREADME()
2524
buildChapterTwo(true)
26-
addPreNext()
2725
}

0 commit comments

Comments
 (0)