|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "github.com/halfrost/LeetCode-Go/ctl/util" |
4 | 7 | "github.com/spf13/cobra"
|
| 8 | + "io" |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + cleanString1 = "{{< columns >}}" |
| 17 | + cleanString2 = "<--->" |
| 18 | + cleanString3 = "{{< /columns >}}" |
| 19 | + cleanString4 = "<img src=\"https://books.halfrost.com/leetcode/logo.png\" alt=\"logo\" height=\"600\" align=\"right\" style=\"padding-left: 30px;\"/>" |
| 20 | + pdfPreface = `<img src="https://books.halfrost.com/leetcode/logo.png" alt="logo" heigth="1300px" align="center"/> |
| 21 | +
|
| 22 | +
|
| 23 | +# 说明 |
| 24 | + |
| 25 | +此版本是 https://books.halfrost.com/leetcode 网页的离线版,由于网页版实时会更新,所以此 PDF 版难免会有一些排版或者错别字。如果读者遇到了,可以到网页版相应页面,点击页面 edit 按钮,提交 pr 进行更改。此 PDF 版本号是 V%v.%v.%v。PDF 永久更新地址是 https://github.com/halfrost/LeetCode-Go/releases/,以版本号区分不同版本。笔者还是强烈推荐看在线版,有任何错误都会立即更新。如果觉得此书对刷题有一点点帮助,可以给此书点一个 star,鼓励一下笔者早点更新更多题解。 |
| 26 | + |
| 27 | +> 版本号说明,V%v.%v.%v,%v 是大版本号,%v 代表当前题解中有几百题,目前是 %v 题,所以第二个版本号是 %v,%v 代表当前题解中有几十题,目前是 %v 题,所以第三个版本号是 %v 。 |
| 28 | + |
| 29 | +# 目录 |
| 30 | + |
| 31 | +[toc] |
| 32 | +
|
| 33 | +` |
| 34 | + |
| 35 | + majorVersion = 1 |
| 36 | + midVersion = 0 |
| 37 | + lastVersion = 0 |
| 38 | + totalSolutions = 0 |
5 | 39 | )
|
6 | 40 |
|
7 | 41 | func newPDFCommand() *cobra.Command {
|
8 | 42 | cmd := &cobra.Command{
|
9 |
| - Use: "pdf <subcommand>", |
| 43 | + Use: "pdf", |
10 | 44 | Short: "PDF related commands",
|
11 | 45 | Run: func(cmd *cobra.Command, args []string) {
|
12 |
| - |
| 46 | + generatePDF() |
13 | 47 | },
|
14 | 48 | }
|
15 |
| - //mc.PersistentFlags().StringVar(&logicEndpoint, "endpoint", "localhost:5880", "endpoint of logic service") |
| 49 | + // cmd.Flags().StringVar(&alias, "alias", "", "alias") |
| 50 | + // cmd.Flags().StringVar(&appId, "appid", "", "appid") |
16 | 51 | return cmd
|
17 | 52 | }
|
| 53 | + |
| 54 | +func generatePDF() { |
| 55 | + var ( |
| 56 | + pdf, tmp []byte |
| 57 | + err error |
| 58 | + ) |
| 59 | + // 先删除 pre-next |
| 60 | + delPreNext() |
| 61 | + |
| 62 | + chapterFourFileOrder := util.LoadChapterFourDir() |
| 63 | + totalSolutions = len(chapterFourFileOrder) |
| 64 | + midVersion = totalSolutions / 100 |
| 65 | + lastVersion = totalSolutions % 100 |
| 66 | + fmt.Printf("[当前的版本号是 V%v.%v.%v]\n", majorVersion, midVersion, lastVersion) |
| 67 | + // 删除原始文档中的头部,并创建临时文件夹 |
| 68 | + prepare(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion)) |
| 69 | + // PDF 前言 |
| 70 | + pdf = append(pdf, []byte(fmt.Sprintf(pdfPreface, majorVersion, midVersion, lastVersion, majorVersion, midVersion, lastVersion, majorVersion, midVersion, totalSolutions, midVersion, lastVersion, totalSolutions, lastVersion))...) |
| 71 | + // PDF 第一章 |
| 72 | + tmp, err = loadChapter(chapterOneFileOrder, "./pdftemp", "ChapterOne") |
| 73 | + pdf = append(pdf, tmp...) |
| 74 | + // PDF 第二章 |
| 75 | + tmp, err = loadChapter(chapterTwoFileOrder, "./pdftemp", "ChapterTwo") |
| 76 | + pdf = append(pdf, tmp...) |
| 77 | + // PDF 第三章 |
| 78 | + tmp, err = loadChapter(chapterThreeFileOrder, "./pdftemp", "ChapterThree") |
| 79 | + pdf = append(pdf, tmp...) |
| 80 | + // PDF 第四章 |
| 81 | + tmp, err = util.LoadFile("./pdftemp/ChapterFour/_index.md") |
| 82 | + pdf = append(pdf, tmp...) |
| 83 | + tmp, err = loadChapter(chapterFourFileOrder, "../website/content", "ChapterFour") |
| 84 | + pdf = append(pdf, tmp...) |
| 85 | + if err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + } |
| 88 | + // 生成 PDF |
| 89 | + util.WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf) |
| 90 | + // 还原现场 |
| 91 | + addPreNext() |
| 92 | + util.DestoryDir("./pdftemp") |
| 93 | +} |
| 94 | + |
| 95 | +func loadChapter(order []string, path, chapter string) ([]byte, error) { |
| 96 | + var ( |
| 97 | + res, tmp []byte |
| 98 | + err error |
| 99 | + ) |
| 100 | + for index, v := range order { |
| 101 | + if chapter == "ChapterOne" && index == 0 { |
| 102 | + // 清理不支持的特殊 MarkDown 语法 |
| 103 | + tmp, err = clean(fmt.Sprintf("%v/%v/%v.md", path, chapter, v)) |
| 104 | + } else { |
| 105 | + tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v.md", path, chapter, v)) |
| 106 | + } |
| 107 | + if err != nil { |
| 108 | + fmt.Println(err) |
| 109 | + return []byte{}, err |
| 110 | + } |
| 111 | + res = append(res, tmp...) |
| 112 | + } |
| 113 | + return res, err |
| 114 | +} |
| 115 | + |
| 116 | +func prepare(path string) { |
| 117 | + err := os.Remove(path) |
| 118 | + if err != nil { |
| 119 | + fmt.Println("pdf 还没有创建") |
| 120 | + fmt.Println(err) |
| 121 | + } |
| 122 | + fmt.Println("pdf 删除成功,开始构建全新版本") |
| 123 | + |
| 124 | + err = os.MkdirAll("./pdftemp/ChapterOne", os.ModePerm) |
| 125 | + if err != nil { |
| 126 | + fmt.Println(err) |
| 127 | + } |
| 128 | + for _, v := range chapterOneFileOrder { |
| 129 | + removeHeader(fmt.Sprintf("../website/content/ChapterOne/%v.md", v), fmt.Sprintf("./pdftemp/ChapterOne/%v.md", v), 4) |
| 130 | + } |
| 131 | + |
| 132 | + err = os.MkdirAll("./pdftemp/ChapterTwo", os.ModePerm) |
| 133 | + if err != nil { |
| 134 | + fmt.Println(err) |
| 135 | + } |
| 136 | + // 生成外部链接的 ChapterTwo |
| 137 | + buildChapterTwo(false) |
| 138 | + util.CopyFile("./pdftemp/ChapterTwo/_index.md", "../website/content/ChapterTwo/_index.md") |
| 139 | + |
| 140 | + for _, v := range chapterTwoFileOrder { |
| 141 | + removeHeader(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), 4) |
| 142 | + } |
| 143 | + |
| 144 | + err = os.MkdirAll("./pdftemp/ChapterThree", os.ModePerm) |
| 145 | + if err != nil { |
| 146 | + fmt.Println(err) |
| 147 | + } |
| 148 | + for _, v := range chapterThreeFileOrder { |
| 149 | + removeHeader(fmt.Sprintf("../website/content/ChapterThree/%v.md", v), fmt.Sprintf("./pdftemp/ChapterThree/%v.md", v), 4) |
| 150 | + } |
| 151 | + |
| 152 | + err = os.MkdirAll("./pdftemp/ChapterFour", os.ModePerm) |
| 153 | + if err != nil { |
| 154 | + fmt.Println(err) |
| 155 | + } |
| 156 | + removeHeader(fmt.Sprintf("../website/content/ChapterFour/_index.md"), fmt.Sprintf("./pdftemp/ChapterFour/_index.md"), 4) |
| 157 | +} |
| 158 | + |
| 159 | +func clean(filePath string) ([]byte, error) { |
| 160 | + f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + defer f.Close() |
| 165 | + reader, output := bufio.NewReader(f), []byte{} |
| 166 | + for { |
| 167 | + line, _, err := reader.ReadLine() |
| 168 | + if err != nil { |
| 169 | + if err == io.EOF { |
| 170 | + return output, nil |
| 171 | + } |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + if ok, _ := regexp.Match(cleanString1, line); ok { |
| 175 | + reg := regexp.MustCompile(cleanString1) |
| 176 | + newByte := reg.ReplaceAll(line, []byte("")) |
| 177 | + output = append(output, newByte...) |
| 178 | + output = append(output, []byte("\n")...) |
| 179 | + } else if ok, _ := regexp.Match(cleanString2, line); ok { |
| 180 | + reg := regexp.MustCompile(cleanString2) |
| 181 | + newByte := reg.ReplaceAll(line, []byte("")) |
| 182 | + output = append(output, newByte...) |
| 183 | + output = append(output, []byte("\n")...) |
| 184 | + } else if ok, _ := regexp.Match(cleanString3, line); ok { |
| 185 | + reg := regexp.MustCompile(cleanString3) |
| 186 | + newByte := reg.ReplaceAll(line, []byte("")) |
| 187 | + output = append(output, newByte...) |
| 188 | + output = append(output, []byte("\n")...) |
| 189 | + } else if ok, _ := regexp.Match(cleanString4, line); ok { |
| 190 | + reg := regexp.MustCompile(cleanString4) |
| 191 | + newByte := reg.ReplaceAll(line, []byte("")) |
| 192 | + output = append(output, newByte...) |
| 193 | + output = append(output, []byte("\n")...) |
| 194 | + } else { |
| 195 | + output = append(output, line...) |
| 196 | + output = append(output, []byte("\n")...) |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func removeHeader(path, newPath string, lineNumber int) { |
| 202 | + file, err := ioutil.ReadFile(path) |
| 203 | + if err != nil { |
| 204 | + panic(err) |
| 205 | + } |
| 206 | + info, _ := os.Stat(path) |
| 207 | + mode := info.Mode() |
| 208 | + array := strings.Split(string(file), "\n") |
| 209 | + array = array[lineNumber:] |
| 210 | + ioutil.WriteFile(newPath, []byte(strings.Join(array, "\n")), mode) |
| 211 | + //fmt.Println("remove line successful") |
| 212 | +} |
0 commit comments