Skip to content

Commit f0d16fe

Browse files
committed
Change ctl module level
1 parent e3bb5af commit f0d16fe

File tree

13 files changed

+67
-88
lines changed

13 files changed

+67
-88
lines changed

ctl/label.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"regexp"
1111
"strings"
1212

13-
"github.com/halfrost/leetcode-go/ctl/util"
1413
"github.com/spf13/cobra"
1514
)
1615

@@ -76,7 +75,7 @@ var (
7675
)
7776

7877
func getChapterFourFileOrder() ([]string, []int) {
79-
solutions, solutionIds := util.LoadChapterFourDir()
78+
solutions, solutionIds := LoadChapterFourDir()
8079
chapterFourFileOrder := []string{"_index"}
8180
chapterFourFileOrder = append(chapterFourFileOrder, solutions...)
8281
fmt.Printf("ChapterFour 中包括 _index 有 %v 个文件, len(id) = %v\n", len(chapterFourFileOrder), len(solutionIds))
@@ -150,34 +149,34 @@ func addPreNextLabel(order, preOrder []string, chapterFourIds []int, preChapter,
150149
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])
151150
} else {
152151
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
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, GetChpaterFourFileNum(chapterFourIds[(index-1)+1]), order[index+1]) + preNextFotter
154153
} else {
155154
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
156155
}
157156
}
158157
} else if index == len(order)-1 {
159158
if chapter == "ChapterFour" {
160159
// 最后一页不需要“下一页”
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])
160+
tmp = "\n\n" + delLine + fmt.Sprintf("<p><a href=\"https://books.halfrost.com/leetcode/%v/%v/%v/\">⬅️上一页</a></p>\n", chapter, GetChpaterFourFileNum(chapterFourIds[(index-1)-1]), order[index-1])
162161
} else {
163162
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
164163
}
165164
} else if index == 1 {
166165
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
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, GetChpaterFourFileNum(chapterFourIds[(index-1)+1]), order[index+1]) + preNextFotter
168167
} else {
169168
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
170169
}
171170
} else {
172171
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
172+
tmp = "\n\n" + preNextHeader + fmt.Sprintf("<p><a href=\"https://books.halfrost.com/leetcode/%v/%v/%v/\">⬅️上一页</a></p>\n", chapter, 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, GetChpaterFourFileNum(chapterFourIds[(index-1)+1]), order[index+1]) + preNextFotter
174173
} else {
175174
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
176175
}
177176
}
178177

179178
if chapter == "ChapterFour" && index > 0 {
180-
path = fmt.Sprintf("%v/%v", util.GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
179+
path = fmt.Sprintf("%v/%v", GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
181180
}
182181

183182
exist, err = needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path))
@@ -192,7 +191,7 @@ func addPreNextLabel(order, preOrder []string, chapterFourIds []int, preChapter,
192191
fmt.Println(err)
193192
return
194193
}
195-
util.WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), res)
194+
WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), res)
196195
count++
197196
}
198197
}
@@ -242,7 +241,7 @@ func delPreNextLabel(order []string, chapterFourIds []int, chapter string) {
242241
lineNum = 3
243242
}
244243
if chapter == "ChapterFour" && index > 0 {
245-
path = fmt.Sprintf("%v/%v", util.GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
244+
path = fmt.Sprintf("%v/%v", GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
246245
}
247246

248247
exist, err := needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path))
@@ -263,7 +262,7 @@ func delPreNextLabel(order []string, chapterFourIds []int, chapter string) {
263262
// fmt.Println(err)
264263
// return
265264
// }
266-
// util.WriteFile(fmt.Sprintf("../website/content/ChapterOne/%v.md", v), res)
265+
// WriteFile(fmt.Sprintf("../website/content/ChapterOne/%v.md", v), res)
267266
}
268267

269268
func needAdd(filePath string) (bool, error) {

ctl/models/lcproblems.go renamed to ctl/lcproblems.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package main
22

33
import (
44
"fmt"

ctl/models/mdrow.go renamed to ctl/mdrow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package main
22

33
import (
44
"fmt"

ctl/models/go.mod

Lines changed: 0 additions & 7 deletions
This file was deleted.

ctl/pdf.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"github.com/halfrost/leetcode-go/ctl/util"
1413
"github.com/spf13/cobra"
1514
)
1615

@@ -61,7 +60,7 @@ func generatePDF() {
6160
// 先删除 pre-next
6261
delPreNext()
6362

64-
chapterFourFileOrder, _ := util.LoadChapterFourDir()
63+
chapterFourFileOrder, _ := LoadChapterFourDir()
6564
totalSolutions = len(chapterFourFileOrder)
6665
midVersion = totalSolutions / 100
6766
lastVersion = totalSolutions % 100
@@ -80,18 +79,18 @@ func generatePDF() {
8079
tmp, err = loadChapter(chapterThreeFileOrder, "./pdftemp", "ChapterThree")
8180
pdf = append(pdf, tmp...)
8281
// PDF 第四章
83-
tmp, err = util.LoadFile("./pdftemp/ChapterFour/_index.md")
82+
tmp, err = LoadFile("./pdftemp/ChapterFour/_index.md")
8483
pdf = append(pdf, tmp...)
8584
tmp, err = loadChapter(chapterFourFileOrder, "../website/content", "ChapterFour")
8685
pdf = append(pdf, tmp...)
8786
if err != nil {
8887
fmt.Println(err)
8988
}
9089
// 生成 PDF
91-
util.WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf)
90+
WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf)
9291
// 还原现场
9392
addPreNext()
94-
util.DestoryDir("./pdftemp")
93+
DestoryDir("./pdftemp")
9594
}
9695

9796
func loadChapter(order []string, path, chapter string) ([]byte, error) {
@@ -110,10 +109,10 @@ func loadChapter(order []string, path, chapter string) ([]byte, error) {
110109
if err != nil {
111110
fmt.Println(err)
112111
}
113-
tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v/%v.md", path, chapter, util.GetChpaterFourFileNum(num), v))
112+
tmp, err = LoadFile(fmt.Sprintf("%v/%v/%v/%v.md", path, chapter, GetChpaterFourFileNum(num), v))
114113
}
115114
} else {
116-
tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v.md", path, chapter, v))
115+
tmp, err = LoadFile(fmt.Sprintf("%v/%v/%v.md", path, chapter, v))
117116
}
118117
}
119118
if err != nil {
@@ -147,7 +146,7 @@ func prepare(path string) {
147146
}
148147
// 生成外部链接的 ChapterTwo
149148
buildChapterTwo(false)
150-
util.CopyFile("./pdftemp/ChapterTwo/_index.md", "../website/content/ChapterTwo/_index.md")
149+
CopyFile("./pdftemp/ChapterTwo/_index.md", "../website/content/ChapterTwo/_index.md")
151150

152151
for _, v := range chapterTwoFileOrder {
153152
removeHeader(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), 5)

ctl/render.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"strconv"
1212
"strings"
1313

14-
m "github.com/halfrost/leetcode-go/ctl/models"
15-
"github.com/halfrost/leetcode-go/ctl/util"
1614
"github.com/spf13/cobra"
1715
)
1816

@@ -80,13 +78,13 @@ func newBuildMenu() *cobra.Command {
8078

8179
func buildREADME() {
8280
var (
83-
problems []m.StatStatusPairs
84-
lpa m.LeetCodeProblemAll
85-
info m.UserInfo
81+
problems []StatStatusPairs
82+
lpa LeetCodeProblemAll
83+
info UserInfo
8684
)
8785
// 请求所有题目信息
8886
body := getProblemAllList()
89-
problemsMap, optimizingIds := map[int]m.StatStatusPairs{}, []int{}
87+
problemsMap, optimizingIds := map[int]StatStatusPairs{}, []int{}
9088
err := json.Unmarshal(body, &lpa)
9189
if err != nil {
9290
fmt.Println(err)
@@ -96,30 +94,30 @@ func buildREADME() {
9694

9795
// 拼凑 README 需要渲染的数据
9896
problems = lpa.StatStatusPairs
99-
info = m.ConvertUserInfoModel(lpa)
97+
info = ConvertUserInfoModel(lpa)
10098
for _, v := range problems {
10199
problemsMap[int(v.Stat.FrontendQuestionID)] = v
102100
}
103-
mdrows := m.ConvertMdModelFromSsp(problems)
104-
sort.Sort(m.SortByQuestionID(mdrows))
105-
solutionIds, _, try := util.LoadSolutionsDir()
106-
m.GenerateMdRows(solutionIds, mdrows)
101+
mdrows := ConvertMdModelFromSsp(problems)
102+
sort.Sort(SortByQuestionID(mdrows))
103+
solutionIds, _, try := LoadSolutionsDir()
104+
GenerateMdRows(solutionIds, mdrows)
107105
info.EasyTotal, info.MediumTotal, info.HardTotal, info.OptimizingEasy, info.OptimizingMedium, info.OptimizingHard, optimizingIds = statisticalData(problemsMap, solutionIds)
108-
omdrows := m.ConvertMdModelFromIds(problemsMap, optimizingIds)
109-
sort.Sort(m.SortByQuestionID(omdrows))
106+
omdrows := ConvertMdModelFromIds(problemsMap, optimizingIds)
107+
sort.Sort(SortByQuestionID(omdrows))
110108

111109
// 按照模板渲染 README
112-
res, err := renderReadme("./template/template.markdown", len(solutionIds), try, m.Mdrows{Mdrows: mdrows}, m.Mdrows{Mdrows: omdrows}, info)
110+
res, err := renderReadme("./template/template.markdown", len(solutionIds), try, Mdrows{Mdrows: mdrows}, Mdrows{Mdrows: omdrows}, info)
113111
if err != nil {
114112
fmt.Println(err)
115113
return
116114
}
117-
util.WriteFile("../README.md", res)
115+
WriteFile("../README.md", res)
118116
fmt.Println("write file successful")
119117
//makeReadmeFile(mds)
120118
}
121119

122-
func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, user m.UserInfo) ([]byte, error) {
120+
func renderReadme(filePath string, total, try int, mdrows, omdrows Mdrows, user UserInfo) ([]byte, error) {
123121
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
124122
if err != nil {
125123
return nil, err
@@ -167,8 +165,8 @@ func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, use
167165
// false 渲染的链接是外部 HTTPS 链接,用于生成 PDF
168166
func buildChapterTwo(internal bool) {
169167
var (
170-
gr m.GraphQLResp
171-
questions []m.Question
168+
gr GraphQLResp
169+
questions []Question
172170
count int
173171
)
174172
for index, tag := range chapterTwoSlug {
@@ -180,39 +178,39 @@ func buildChapterTwo(internal bool) {
180178
return
181179
}
182180
questions = gr.Data.TopicTag.Questions
183-
mdrows := m.ConvertMdModelFromQuestions(questions)
184-
sort.Sort(m.SortByQuestionID(mdrows))
185-
solutionIds, _, _ := util.LoadSolutionsDir()
181+
mdrows := ConvertMdModelFromQuestions(questions)
182+
sort.Sort(SortByQuestionID(mdrows))
183+
solutionIds, _, _ := LoadSolutionsDir()
186184
tl, err := loadMetaData(fmt.Sprintf("./meta/%v", chapterTwoFileName[index]))
187185
if err != nil {
188186
fmt.Printf("err = %v\n", err)
189187
}
190-
tls := m.GenerateTagMdRows(solutionIds, tl, mdrows, internal)
188+
tls := GenerateTagMdRows(solutionIds, tl, mdrows, internal)
191189
//fmt.Printf("tls = %v\n", tls)
192190
// 按照模板渲染 README
193-
res, err := renderChapterTwo(fmt.Sprintf("./template/%v.md", chapterTwoFileName[index]), m.TagLists{TagLists: tls})
191+
res, err := renderChapterTwo(fmt.Sprintf("./template/%v.md", chapterTwoFileName[index]), TagLists{TagLists: tls})
194192
if err != nil {
195193
fmt.Println(err)
196194
return
197195
}
198196
if internal {
199-
util.WriteFile(fmt.Sprintf("../website/content/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
197+
WriteFile(fmt.Sprintf("../website/content/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
200198
} else {
201-
util.WriteFile(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
199+
WriteFile(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
202200
}
203201

204202
count++
205203
}
206204
fmt.Printf("write %v files successful", count)
207205
}
208206

209-
func loadMetaData(filePath string) (map[int]m.TagList, error) {
207+
func loadMetaData(filePath string) (map[int]TagList, error) {
210208
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
211209
if err != nil {
212210
return nil, err
213211
}
214212
defer f.Close()
215-
reader, metaMap := bufio.NewReader(f), map[int]m.TagList{}
213+
reader, metaMap := bufio.NewReader(f), map[int]TagList{}
216214

217215
for {
218216
line, _, err := reader.ReadLine()
@@ -225,7 +223,7 @@ func loadMetaData(filePath string) (map[int]m.TagList, error) {
225223
s := strings.Split(string(line), "|")
226224
v, _ := strconv.Atoi(strings.Split(s[1], ".")[0])
227225
// v[0] 是题号,s[4] time, s[5] space, s[6] favorite
228-
metaMap[v] = m.TagList{
226+
metaMap[v] = TagList{
229227
FrontendQuestionID: int32(v),
230228
Acceptance: "",
231229
Difficulty: "",
@@ -236,7 +234,7 @@ func loadMetaData(filePath string) (map[int]m.TagList, error) {
236234
}
237235
}
238236

239-
func renderChapterTwo(filePath string, tls m.TagLists) ([]byte, error) {
237+
func renderChapterTwo(filePath string, tls TagLists) ([]byte, error) {
240238
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
241239
if err != nil {
242240
return nil, err
@@ -272,18 +270,18 @@ func buildBookMenu() {
272270
fmt.Println(err)
273271
return
274272
}
275-
util.WriteFile("../website/content/menu/index.md", res)
273+
WriteFile("../website/content/menu/index.md", res)
276274
fmt.Println("generate Menu successful")
277275
}
278276

279277
// 拷贝 leetcode 目录下的题解 README 文件至第四章对应文件夹中
280278
func copyLackFile() {
281-
solutionIds, soName, _ := util.LoadSolutionsDir()
282-
_, ch4Ids := util.LoadChapterFourDir()
279+
solutionIds, soName, _ := LoadSolutionsDir()
280+
_, ch4Ids := LoadChapterFourDir()
283281

284282
needCopy := []string{}
285283
for i := 0; i < len(solutionIds); i++ {
286-
if util.BinarySearch(ch4Ids, solutionIds[i]) == -1 {
284+
if BinarySearch(ch4Ids, solutionIds[i]) == -1 {
287285
needCopy = append(needCopy, soName[i])
288286
}
289287
}
@@ -295,12 +293,12 @@ func copyLackFile() {
295293
if err != nil {
296294
fmt.Println(err)
297295
}
298-
err = os.MkdirAll(fmt.Sprintf("../website/content/ChapterFour/%v", util.GetChpaterFourFileNum(tmp)), os.ModePerm)
296+
err = os.MkdirAll(fmt.Sprintf("../website/content/ChapterFour/%v", GetChpaterFourFileNum(tmp)), os.ModePerm)
299297
if err != nil {
300298
fmt.Println(err)
301299
}
302-
util.CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/%v.md", util.GetChpaterFourFileNum(tmp), needCopy[i]), fmt.Sprintf("../leetcode/%v/README.md", needCopy[i]))
303-
util.CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/_index.md", util.GetChpaterFourFileNum(tmp)), "./template/collapseSection.md")
300+
CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/%v.md", GetChpaterFourFileNum(tmp), needCopy[i]), fmt.Sprintf("../leetcode/%v/README.md", needCopy[i]))
301+
CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/_index.md", GetChpaterFourFileNum(tmp)), "./template/collapseSection.md")
304302
}
305303
}
306304
} else {

ctl/request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package main
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/mozillazg/request"
76
"io/ioutil"
87
"net/http"
8+
9+
"github.com/mozillazg/request"
910
)
1011

1112
const (

0 commit comments

Comments
 (0)