Skip to content

Commit

Permalink
Change ctl module level
Browse files Browse the repository at this point in the history
  • Loading branch information
halfrost committed Sep 10, 2022
1 parent e3bb5af commit f0d16fe
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 88 deletions.
19 changes: 9 additions & 10 deletions ctl/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"regexp"
"strings"

"github.com/halfrost/leetcode-go/ctl/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -76,7 +75,7 @@ var (
)

func getChapterFourFileOrder() ([]string, []int) {
solutions, solutionIds := util.LoadChapterFourDir()
solutions, solutionIds := LoadChapterFourDir()
chapterFourFileOrder := []string{"_index"}
chapterFourFileOrder = append(chapterFourFileOrder, solutions...)
fmt.Printf("ChapterFour 中包括 _index 有 %v 个文件, len(id) = %v\n", len(chapterFourFileOrder), len(solutionIds))
Expand Down Expand Up @@ -150,34 +149,34 @@ func addPreNextLabel(order, preOrder []string, chapterFourIds []int, preChapter,
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])
} else {
if chapter == "ChapterFour" {
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
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
} else {
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
}
}
} else if index == len(order)-1 {
if chapter == "ChapterFour" {
// 最后一页不需要“下一页”
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])
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])
} else {
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
}
} else if index == 1 {
if chapter == "ChapterFour" {
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
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
} else {
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
}
} else {
if chapter == "ChapterFour" {
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
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
} else {
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
}
}

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

exist, err = needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, path))
Expand All @@ -192,7 +191,7 @@ func addPreNextLabel(order, preOrder []string, chapterFourIds []int, preChapter,
fmt.Println(err)
return
}
util.WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), res)
WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, path), res)
count++
}
}
Expand Down Expand Up @@ -242,7 +241,7 @@ func delPreNextLabel(order []string, chapterFourIds []int, chapter string) {
lineNum = 3
}
if chapter == "ChapterFour" && index > 0 {
path = fmt.Sprintf("%v/%v", util.GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
path = fmt.Sprintf("%v/%v", GetChpaterFourFileNum(chapterFourIds[(index-1)]), path)
}

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

func needAdd(filePath string) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion ctl/models/lcproblems.go → ctl/lcproblems.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion ctl/models/mdrow.go → ctl/mdrow.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package main

import (
"fmt"
Expand Down
7 changes: 0 additions & 7 deletions ctl/models/go.mod

This file was deleted.

15 changes: 7 additions & 8 deletions ctl/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"strings"

"github.com/halfrost/leetcode-go/ctl/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,7 +60,7 @@ func generatePDF() {
// 先删除 pre-next
delPreNext()

chapterFourFileOrder, _ := util.LoadChapterFourDir()
chapterFourFileOrder, _ := LoadChapterFourDir()
totalSolutions = len(chapterFourFileOrder)
midVersion = totalSolutions / 100
lastVersion = totalSolutions % 100
Expand All @@ -80,18 +79,18 @@ func generatePDF() {
tmp, err = loadChapter(chapterThreeFileOrder, "./pdftemp", "ChapterThree")
pdf = append(pdf, tmp...)
// PDF 第四章
tmp, err = util.LoadFile("./pdftemp/ChapterFour/_index.md")
tmp, err = LoadFile("./pdftemp/ChapterFour/_index.md")
pdf = append(pdf, tmp...)
tmp, err = loadChapter(chapterFourFileOrder, "../website/content", "ChapterFour")
pdf = append(pdf, tmp...)
if err != nil {
fmt.Println(err)
}
// 生成 PDF
util.WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf)
WriteFile(fmt.Sprintf("../PDF v%v.%v.%v.md", majorVersion, midVersion, lastVersion), pdf)
// 还原现场
addPreNext()
util.DestoryDir("./pdftemp")
DestoryDir("./pdftemp")
}

func loadChapter(order []string, path, chapter string) ([]byte, error) {
Expand All @@ -110,10 +109,10 @@ func loadChapter(order []string, path, chapter string) ([]byte, error) {
if err != nil {
fmt.Println(err)
}
tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v/%v.md", path, chapter, util.GetChpaterFourFileNum(num), v))
tmp, err = LoadFile(fmt.Sprintf("%v/%v/%v/%v.md", path, chapter, GetChpaterFourFileNum(num), v))
}
} else {
tmp, err = util.LoadFile(fmt.Sprintf("%v/%v/%v.md", path, chapter, v))
tmp, err = LoadFile(fmt.Sprintf("%v/%v/%v.md", path, chapter, v))
}
}
if err != nil {
Expand Down Expand Up @@ -147,7 +146,7 @@ func prepare(path string) {
}
// 生成外部链接的 ChapterTwo
buildChapterTwo(false)
util.CopyFile("./pdftemp/ChapterTwo/_index.md", "../website/content/ChapterTwo/_index.md")
CopyFile("./pdftemp/ChapterTwo/_index.md", "../website/content/ChapterTwo/_index.md")

for _, v := range chapterTwoFileOrder {
removeHeader(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", v), 5)
Expand Down
70 changes: 34 additions & 36 deletions ctl/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"strconv"
"strings"

m "github.com/halfrost/leetcode-go/ctl/models"
"github.com/halfrost/leetcode-go/ctl/util"
"github.com/spf13/cobra"
)

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

func buildREADME() {
var (
problems []m.StatStatusPairs
lpa m.LeetCodeProblemAll
info m.UserInfo
problems []StatStatusPairs
lpa LeetCodeProblemAll
info UserInfo
)
// 请求所有题目信息
body := getProblemAllList()
problemsMap, optimizingIds := map[int]m.StatStatusPairs{}, []int{}
problemsMap, optimizingIds := map[int]StatStatusPairs{}, []int{}
err := json.Unmarshal(body, &lpa)
if err != nil {
fmt.Println(err)
Expand All @@ -96,30 +94,30 @@ func buildREADME() {

// 拼凑 README 需要渲染的数据
problems = lpa.StatStatusPairs
info = m.ConvertUserInfoModel(lpa)
info = ConvertUserInfoModel(lpa)
for _, v := range problems {
problemsMap[int(v.Stat.FrontendQuestionID)] = v
}
mdrows := m.ConvertMdModelFromSsp(problems)
sort.Sort(m.SortByQuestionID(mdrows))
solutionIds, _, try := util.LoadSolutionsDir()
m.GenerateMdRows(solutionIds, mdrows)
mdrows := ConvertMdModelFromSsp(problems)
sort.Sort(SortByQuestionID(mdrows))
solutionIds, _, try := LoadSolutionsDir()
GenerateMdRows(solutionIds, mdrows)
info.EasyTotal, info.MediumTotal, info.HardTotal, info.OptimizingEasy, info.OptimizingMedium, info.OptimizingHard, optimizingIds = statisticalData(problemsMap, solutionIds)
omdrows := m.ConvertMdModelFromIds(problemsMap, optimizingIds)
sort.Sort(m.SortByQuestionID(omdrows))
omdrows := ConvertMdModelFromIds(problemsMap, optimizingIds)
sort.Sort(SortByQuestionID(omdrows))

// 按照模板渲染 README
res, err := renderReadme("./template/template.markdown", len(solutionIds), try, m.Mdrows{Mdrows: mdrows}, m.Mdrows{Mdrows: omdrows}, info)
res, err := renderReadme("./template/template.markdown", len(solutionIds), try, Mdrows{Mdrows: mdrows}, Mdrows{Mdrows: omdrows}, info)
if err != nil {
fmt.Println(err)
return
}
util.WriteFile("../README.md", res)
WriteFile("../README.md", res)
fmt.Println("write file successful")
//makeReadmeFile(mds)
}

func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, user m.UserInfo) ([]byte, error) {
func renderReadme(filePath string, total, try int, mdrows, omdrows Mdrows, user UserInfo) ([]byte, error) {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return nil, err
Expand Down Expand Up @@ -167,8 +165,8 @@ func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, use
// false 渲染的链接是外部 HTTPS 链接,用于生成 PDF
func buildChapterTwo(internal bool) {
var (
gr m.GraphQLResp
questions []m.Question
gr GraphQLResp
questions []Question
count int
)
for index, tag := range chapterTwoSlug {
Expand All @@ -180,39 +178,39 @@ func buildChapterTwo(internal bool) {
return
}
questions = gr.Data.TopicTag.Questions
mdrows := m.ConvertMdModelFromQuestions(questions)
sort.Sort(m.SortByQuestionID(mdrows))
solutionIds, _, _ := util.LoadSolutionsDir()
mdrows := ConvertMdModelFromQuestions(questions)
sort.Sort(SortByQuestionID(mdrows))
solutionIds, _, _ := LoadSolutionsDir()
tl, err := loadMetaData(fmt.Sprintf("./meta/%v", chapterTwoFileName[index]))
if err != nil {
fmt.Printf("err = %v\n", err)
}
tls := m.GenerateTagMdRows(solutionIds, tl, mdrows, internal)
tls := GenerateTagMdRows(solutionIds, tl, mdrows, internal)
//fmt.Printf("tls = %v\n", tls)
// 按照模板渲染 README
res, err := renderChapterTwo(fmt.Sprintf("./template/%v.md", chapterTwoFileName[index]), m.TagLists{TagLists: tls})
res, err := renderChapterTwo(fmt.Sprintf("./template/%v.md", chapterTwoFileName[index]), TagLists{TagLists: tls})
if err != nil {
fmt.Println(err)
return
}
if internal {
util.WriteFile(fmt.Sprintf("../website/content/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
WriteFile(fmt.Sprintf("../website/content/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
} else {
util.WriteFile(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
WriteFile(fmt.Sprintf("./pdftemp/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
}

count++
}
fmt.Printf("write %v files successful", count)
}

func loadMetaData(filePath string) (map[int]m.TagList, error) {
func loadMetaData(filePath string) (map[int]TagList, error) {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
defer f.Close()
reader, metaMap := bufio.NewReader(f), map[int]m.TagList{}
reader, metaMap := bufio.NewReader(f), map[int]TagList{}

for {
line, _, err := reader.ReadLine()
Expand All @@ -225,7 +223,7 @@ func loadMetaData(filePath string) (map[int]m.TagList, error) {
s := strings.Split(string(line), "|")
v, _ := strconv.Atoi(strings.Split(s[1], ".")[0])
// v[0] 是题号,s[4] time, s[5] space, s[6] favorite
metaMap[v] = m.TagList{
metaMap[v] = TagList{
FrontendQuestionID: int32(v),
Acceptance: "",
Difficulty: "",
Expand All @@ -236,7 +234,7 @@ func loadMetaData(filePath string) (map[int]m.TagList, error) {
}
}

func renderChapterTwo(filePath string, tls m.TagLists) ([]byte, error) {
func renderChapterTwo(filePath string, tls TagLists) ([]byte, error) {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return nil, err
Expand Down Expand Up @@ -272,18 +270,18 @@ func buildBookMenu() {
fmt.Println(err)
return
}
util.WriteFile("../website/content/menu/index.md", res)
WriteFile("../website/content/menu/index.md", res)
fmt.Println("generate Menu successful")
}

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

needCopy := []string{}
for i := 0; i < len(solutionIds); i++ {
if util.BinarySearch(ch4Ids, solutionIds[i]) == -1 {
if BinarySearch(ch4Ids, solutionIds[i]) == -1 {
needCopy = append(needCopy, soName[i])
}
}
Expand All @@ -295,12 +293,12 @@ func copyLackFile() {
if err != nil {
fmt.Println(err)
}
err = os.MkdirAll(fmt.Sprintf("../website/content/ChapterFour/%v", util.GetChpaterFourFileNum(tmp)), os.ModePerm)
err = os.MkdirAll(fmt.Sprintf("../website/content/ChapterFour/%v", GetChpaterFourFileNum(tmp)), os.ModePerm)
if err != nil {
fmt.Println(err)
}
util.CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/%v.md", util.GetChpaterFourFileNum(tmp), needCopy[i]), fmt.Sprintf("../leetcode/%v/README.md", needCopy[i]))
util.CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/_index.md", util.GetChpaterFourFileNum(tmp)), "./template/collapseSection.md")
CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/%v.md", GetChpaterFourFileNum(tmp), needCopy[i]), fmt.Sprintf("../leetcode/%v/README.md", needCopy[i]))
CopyFile(fmt.Sprintf("../website/content/ChapterFour/%v/_index.md", GetChpaterFourFileNum(tmp)), "./template/collapseSection.md")
}
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion ctl/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"bytes"
"fmt"
"github.com/mozillazg/request"
"io/ioutil"
"net/http"

"github.com/mozillazg/request"
)

const (
Expand Down
Loading

0 comments on commit f0d16fe

Please sign in to comment.