mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Ctl add build chapter-two
This commit is contained in:
172
ctl/render.go
172
ctl/render.go
@ -5,21 +5,73 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
m "github.com/halfrost/LeetCode-Go/ctl/models"
|
||||
"github.com/halfrost/LeetCode-Go/ctl/util"
|
||||
"github.com/spf13/cobra"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
chapterTwoList = []string{"Array", "String", "Two Pointers", "Linked List", "Stack", "Tree", "Dynamic Programming", "Backtracking", "Depth First Search", "Breadth First Search",
|
||||
"Binary Search", "Math", "Hash Table", "Sort", "Bit Manipulation", "Union Find", "Sliding Window", "Segment Tree", "Binary Indexed Tree"}
|
||||
chapterTwoFileName = []string{"Array", "String", "Two_Pointers", "Linked_List", "Stack", "Tree", "Dynamic_Programming", "Backtracking", "Depth_First_Search", "Breadth_First_Search",
|
||||
"Binary_Search", "Math", "Hash_Table", "Sort", "Bit_Manipulation", "Union_Find", "Sliding_Window", "Segment_Tree", "Binary_Indexed_Tree"}
|
||||
chapterTwoSlug = []string{"array", "string", "two-pointers", "linked-list", "stack", "tree", "dynamic-programming", "backtracking", "depth-first-search", "breadth-first-search",
|
||||
"binary-search", "math", "hash-table", "sort", "bit-manipulation", "union-find", "sliding-window", "segment-tree", "binary-indexed-tree"}
|
||||
)
|
||||
|
||||
func newBuildCommand() *cobra.Command {
|
||||
mc := &cobra.Command{
|
||||
Use: "build <subcommand>",
|
||||
Short: "Build doc related commands",
|
||||
}
|
||||
//mc.PersistentFlags().StringVar(&logicEndpoint, "endpoint", "localhost:5880", "endpoint of logic service")
|
||||
mc.AddCommand(
|
||||
newBuildREADME(),
|
||||
newBuildChapterTwo(),
|
||||
)
|
||||
|
||||
return mc
|
||||
}
|
||||
|
||||
func newBuildREADME() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "readme",
|
||||
Short: "Build readme.md commands",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
buildREADME()
|
||||
},
|
||||
}
|
||||
// cmd.Flags().StringVar(&alias, "alias", "", "alias")
|
||||
// cmd.Flags().StringVar(&appId, "appid", "", "appid")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newBuildChapterTwo() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "chapter-two",
|
||||
Short: "Build Chapter Two commands",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
buildChapterTwo()
|
||||
},
|
||||
}
|
||||
// cmd.Flags().StringVar(&alias, "alias", "", "alias")
|
||||
// cmd.Flags().StringVar(&appId, "appid", "", "appid")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func buildREADME() {
|
||||
var (
|
||||
problems []m.StatStatusPairs
|
||||
lpa m.LeetCodeProblemAll
|
||||
info m.UserInfo
|
||||
)
|
||||
// 请求所有题目信息
|
||||
body := getProblemAllList(AllProblemURL)
|
||||
body := getProblemAllList()
|
||||
problemsMap, optimizingIds := map[int]m.StatStatusPairs{}, []int{}
|
||||
err := json.Unmarshal(body, &lpa)
|
||||
if err != nil {
|
||||
@ -34,36 +86,24 @@ func main() {
|
||||
for _, v := range problems {
|
||||
problemsMap[int(v.Stat.FrontendQuestionID)] = v
|
||||
}
|
||||
mdrows := m.ConvertMdModel(problems)
|
||||
mdrows := m.ConvertMdModelFromSsp(problems)
|
||||
sort.Sort(m.SortByQuestionID(mdrows))
|
||||
solutionIds, try := loadSolutionsDir()
|
||||
generateMdRows(solutionIds, mdrows)
|
||||
solutionIds, try := util.LoadSolutionsDir()
|
||||
m.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))
|
||||
|
||||
// 按照模板渲染 README
|
||||
res, err := renderReadme("./template.markdown", len(solutionIds), try, m.Mdrows{Mdrows: mdrows}, m.Mdrows{Mdrows: omdrows}, info)
|
||||
res, err := renderReadme("./template/template.markdown", len(solutionIds), try, m.Mdrows{Mdrows: mdrows}, m.Mdrows{Mdrows: omdrows}, info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
writeFile("../README.md", res)
|
||||
util.WriteFile("../README.md", res)
|
||||
//makeReadmeFile(mds)
|
||||
}
|
||||
|
||||
func generateMdRows(solutionIds []int, mdrows []m.Mdrow) {
|
||||
for i := 0; i < len(solutionIds); i++ {
|
||||
id := mdrows[solutionIds[i]-1].FrontendQuestionID
|
||||
if solutionIds[i] == int(id) {
|
||||
//fmt.Printf("id = %v i = %v solutionIds = %v\n", id, i, solutionIds[i])
|
||||
mdrows[id-1].SolutionPath = fmt.Sprintf("[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/%v)", fmt.Sprintf("%04d.%v", id, strings.Replace(mdrows[id-1].QuestionTitle, " ", "-", -1)))
|
||||
} else {
|
||||
fmt.Printf("序号出错了 solutionIds = %v id = %v\n", solutionIds[i], id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, user m.UserInfo) ([]byte, error) {
|
||||
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
|
||||
if err != nil {
|
||||
@ -106,3 +146,97 @@ func renderReadme(filePath string, total, try int, mdrows, omdrows m.Mdrows, use
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func buildChapterTwo() {
|
||||
var (
|
||||
gr m.GraphQLResp
|
||||
questions []m.Question
|
||||
)
|
||||
|
||||
for index, tag := range chapterTwoSlug {
|
||||
body := getTagProblemList(tag)
|
||||
// fmt.Printf("%v\n", string(body))
|
||||
err := json.Unmarshal(body, &gr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
questions = gr.Data.TopicTag.Questions
|
||||
mdrows := m.ConvertMdModelFromQuestions(questions)
|
||||
sort.Sort(m.SortByQuestionID(mdrows))
|
||||
solutionIds, _ := util.LoadSolutionsDir()
|
||||
// generateMdRows(solutionIds, mdrows)
|
||||
|
||||
tl, err := loadMetaData(fmt.Sprintf("./meta/%v", chapterTwoFileName[index]))
|
||||
if err != nil {
|
||||
fmt.Printf("err = %v\n", err)
|
||||
}
|
||||
tls := m.GenerateTagMdRows(solutionIds, tl, mdrows)
|
||||
//fmt.Printf("tls = %v\n", tls)
|
||||
// // 按照模板渲染 README
|
||||
res, err := renderChapterTwo(fmt.Sprintf("./template/%v.md", chapterTwoFileName[index]), m.TagLists{TagLists: tls})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
util.WriteFile(fmt.Sprintf("./%v.md", chapterTwoFileName[index]), res)
|
||||
}
|
||||
}
|
||||
|
||||
func loadMetaData(filePath string) (map[int]m.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{}
|
||||
|
||||
for {
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return metaMap, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
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{
|
||||
FrontendQuestionID: int32(v),
|
||||
Acceptance: "",
|
||||
Difficulty: "",
|
||||
TimeComplexity: s[4],
|
||||
SpaceComplexity: s[5],
|
||||
Favorite: s[6],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func renderChapterTwo(filePath string, tls m.TagLists) ([]byte, error) {
|
||||
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
reader, output := bufio.NewReader(f), []byte{}
|
||||
|
||||
for {
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return output, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if ok, _ := regexp.Match("{{.AvailableTagTable}}", line); ok {
|
||||
reg := regexp.MustCompile("{{.AvailableTagTable}}")
|
||||
newByte := reg.ReplaceAll(line, []byte(tls.AvailableTagTable()))
|
||||
output = append(output, newByte...)
|
||||
output = append(output, []byte("\n")...)
|
||||
} else {
|
||||
output = append(output, line...)
|
||||
output = append(output, []byte("\n")...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user