Skip to content

Commit

Permalink
Add pre-next
Browse files Browse the repository at this point in the history
  • Loading branch information
halfrost committed Jan 16, 2021
1 parent 1a51946 commit e1a090b
Show file tree
Hide file tree
Showing 35 changed files with 1,993 additions and 15 deletions.
1,173 changes: 1,173 additions & 0 deletions README_old.md

Large diffs are not rendered by default.

248 changes: 246 additions & 2 deletions ctl/label.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
package main

import (
"bufio"
// "encoding/json"
"fmt"
// m "github.com/halfrost/LeetCode-Go/ctl/models"
// "github.com/halfrost/LeetCode-Go/ctl/util"
"github.com/halfrost/LeetCode-Go/ctl/util"
"github.com/spf13/cobra"
"io"
"os"
"regexp"
// "sort"
// "strconv"
"errors"
"io/ioutil"
"strings"
)

var (
chapterOneFileOrder = []string{"_index", "Data_Structure", "Algorithm"}
chapterTwoFileOrder = []string{"_index", "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"}
chapterThreeFileOrder = []string{"_index", "Segment_Tree", "UnionFind", "LRUCache", "LFUCache"}
preNextHeader = "----------------------------------------------\n<div style=\"display: flex;justify-content: space-between;align-items: center;\">\n"
preNextFotter = "</div>"
delLine = "----------------------------------------------\n"
delHeader = "<div style=\"display: flex;justify-content: space-between;align-items: center;\">"
delLabel = "<[a-zA-Z]+.*?>([\\s\\S]*?)</[a-zA-Z]*?>"
delFooter = "</div>"

//ErrNoFilename is thrown when the path the the file to tail was not given
ErrNoFilename = errors.New("You must provide the path to a file in the \"-file\" flag.")

//ErrInvalidLineCount is thrown when the user provided 0 (zero) as the value for number of lines to tail
ErrInvalidLineCount = errors.New("You cannot tail zero lines.")
)

func getChapterFourFileOrder() []string {
solutions := util.LoadChapterFourDir()
chapterFourFileOrder := []string{"_index"}
chapterFourFileOrder = append(chapterFourFileOrder, solutions...)
fmt.Printf("ChapterFour 中包括 _index 有 %v 个文件\n", len(chapterFourFileOrder))
return chapterFourFileOrder
}

func newLabelCommand() *cobra.Command {
mc := &cobra.Command{
Use: "label <subcommand>",
Expand All @@ -22,7 +63,7 @@ func newAddPreNext() *cobra.Command {
Use: "add-pre-next",
Short: "Add pre-next label",
Run: func(cmd *cobra.Command, args []string) {

addPreNext()
},
}
// cmd.Flags().StringVar(&alias, "alias", "", "alias")
Expand All @@ -32,12 +73,215 @@ func newAddPreNext() *cobra.Command {

func newDeletePreNext() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-pre-next",
Use: "del-pre-next",
Short: "Delete pre-next label",
Run: func(cmd *cobra.Command, args []string) {
delPreNext()
},
}
// cmd.Flags().StringVar(&alias, "alias", "", "alias")
// cmd.Flags().StringVar(&appId, "appid", "", "appid")
return cmd
}

func addPreNext() {
// Chpater one add pre-next
addPreNextLabel(chapterOneFileOrder, []string{}, "", "ChapterOne", "ChapterTwo")
// Chpater two add pre-next
addPreNextLabel(chapterTwoFileOrder, chapterOneFileOrder, "ChapterOne", "ChapterTwo", "ChapterThree")
// Chpater three add pre-next
addPreNextLabel(chapterThreeFileOrder, chapterTwoFileOrder, "ChapterTwo", "ChapterThree", "ChapterFour")
// Chpater four add pre-next
//fmt.Printf("%v\n", getChapterFourFileOrder())
addPreNextLabel(getChapterFourFileOrder(), chapterThreeFileOrder, "ChapterThree", "ChapterFour", "")
}

func addPreNextLabel(order, preOrder []string, preChapter, chapter, nextChapter string) {
var (
exist bool
err error
res []byte
count int
)
for index, v := range order {
tmp := ""
if index == 0 {
if chapter == "ChapterOne" {
// 第一页不需要“上一章”
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 {
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/\">⬅️上一页</a></p>\n", chapter, 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 {
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 {
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
}
exist, err = needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, v))
if err != nil {
fmt.Println(err)
return
}
// 当前没有上一页和下一页,才添加
if !exist && err == nil {
res, err = eofAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, v), tmp)
if err != nil {
fmt.Println(err)
return
}
util.WriteFile(fmt.Sprintf("../website/content/%v/%v.md", chapter, v), res)
count++
}
}
fmt.Printf("添加了 %v 个文件的 pre-next\n", count)
}

func eofAdd(filePath string, labelString string) ([]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 {
output = append(output, []byte(labelString)...)
output = append(output, []byte("\n")...)
return output, nil
}
return nil, err
}
output = append(output, line...)
output = append(output, []byte("\n")...)
}
}

func delPreNext() {
// Chpater one del pre-next
delPreNextLabel(chapterOneFileOrder, "ChapterOne")
// Chpater two del pre-next
delPreNextLabel(chapterTwoFileOrder, "ChapterTwo")
// Chpater three del pre-next
delPreNextLabel(chapterThreeFileOrder, "ChapterThree")
// Chpater four del pre-next
delPreNextLabel(getChapterFourFileOrder(), "ChapterFour")
}

func delPreNextLabel(order []string, chapter string) {
count := 0
for index, v := range order {
lineNum := 5
if index == 0 && chapter == "ChapterOne" || index == len(order)-1 && chapter == "ChapterFour" {
lineNum = 3
}
exist, err := needAdd(fmt.Sprintf("../website/content/%v/%v.md", chapter, v))
if err != nil {
fmt.Println(err)
return
}
// 存在才删除
if exist && err == nil {
removeLine(fmt.Sprintf("../website/content/%v/%v.md", chapter, v), lineNum+1)
count++
}
}
fmt.Printf("删除了 %v 个文件的 pre-next\n", count)
// 另外一种删除方法
// res, err := eofDel(fmt.Sprintf("../website/content/ChapterOne/%v.md", v))
// if err != nil {
// fmt.Println(err)
// return
// }
// util.WriteFile(fmt.Sprintf("../website/content/ChapterOne/%v.md", v), res)
}

func needAdd(filePath string) (bool, error) {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return false, err
}
defer f.Close()
reader, output := bufio.NewReader(f), []byte{}
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
return false, nil
}
return false, err
}
if ok, _ := regexp.Match(delHeader, line); ok {
return true, nil
} else if ok, _ := regexp.Match(delLabel, line); ok {
return true, nil
} else {
output = append(output, line...)
output = append(output, []byte("\n")...)
}
}
}

func eofDel(filePath string) ([]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(delLine, line); ok {
reg := regexp.MustCompile(delLine)
newByte := reg.ReplaceAll(line, []byte(" "))
output = append(output, newByte...)
output = append(output, []byte("\n")...)
} else if ok, _ := regexp.Match(delHeader, line); ok {
reg := regexp.MustCompile(delHeader)
newByte := reg.ReplaceAll(line, []byte(" "))
output = append(output, newByte...)
output = append(output, []byte("\n")...)
} else if ok, _ := regexp.Match(delLabel, line); ok {
reg := regexp.MustCompile(delLabel)
newByte := reg.ReplaceAll(line, []byte(" "))
output = append(output, newByte...)
output = append(output, []byte("\n")...)
} else if ok, _ := regexp.Match(delFooter, line); ok {
reg := regexp.MustCompile(delFooter)
newByte := reg.ReplaceAll(line, []byte(" "))
output = append(output, newByte...)
output = append(output, []byte("\n")...)
} else {
output = append(output, line...)
output = append(output, []byte("\n")...)
}
}
}

func removeLine(path string, lineNumber int) {
file, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
info, _ := os.Stat(path)
mode := info.Mode()
array := strings.Split(string(file), "\n")
array = array[:len(array)-lineNumber-1]
ioutil.WriteFile(path, []byte(strings.Join(array, "\n")), mode)
//fmt.Println("remove line successful")
}
5 changes: 4 additions & 1 deletion ctl/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func newBuildCommand() *cobra.Command {
newBuildREADME(),
newBuildChapterTwo(),
)

return mc
}

Expand Down Expand Up @@ -101,6 +100,7 @@ func buildREADME() {
return
}
util.WriteFile("../README.md", res)
fmt.Println("write file successful")
//makeReadmeFile(mds)
}

Expand Down Expand Up @@ -151,6 +151,7 @@ func buildChapterTwo() {
var (
gr m.GraphQLResp
questions []m.Question
count int
)
for index, tag := range chapterTwoSlug {
body := getTagProblemList(tag)
Expand All @@ -177,7 +178,9 @@ func buildChapterTwo() {
return
}
util.WriteFile(fmt.Sprintf("../website/content/ChapterTwo/%v.md", chapterTwoFileName[index]), res)
count++
}
fmt.Println("write %v files successful", count)
}

func loadMetaData(filePath string) (map[int]m.TagList, error) {
Expand Down
30 changes: 29 additions & 1 deletion ctl/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ func LoadSolutionsDir() ([]int, int) {
return solutionIds, len(files) - len(solutionIds)
}

// LoadChapterFourDir define
func LoadChapterFourDir() []string {
files, err := ioutil.ReadDir("../website/content/ChapterFour/")
if err != nil {
fmt.Println(err)
}
solutions, solutionIds, solutionsMap := []string{}, []int{}, map[int]string{}
for _, f := range files {
if f.Name()[4] == '.' {
tmp, err := strconv.Atoi(f.Name()[:4])
if err != nil {
fmt.Println(err)
}
solutionIds = append(solutionIds, tmp)
// len(f.Name())-3 = 文件名去掉 .md 后缀
solutionsMap[tmp] = f.Name()[:len(f.Name())-3]
}
}
sort.Ints(solutionIds)
fmt.Printf("读取了第四章的 %v 道题的题解\n", len(solutionIds))
for _, v := range solutionIds {
if name, ok := solutionsMap[v]; ok {
solutions = append(solutions, name)
}
}
return solutions
}

// WriteFile define
func WriteFile(fileName string, content []byte) {
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0777)
Expand All @@ -41,7 +69,7 @@ func WriteFile(fileName string, content []byte) {
if err != nil {
fmt.Println(err)
}
fmt.Println("write file successful")
//fmt.Println("write file successful")
}

// BinarySearch define
Expand Down
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/halfrost/LeetCode-Go

go 1.14

require (
github.com/BurntSushi/toml v0.3.1
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/graphql-go/graphql v0.7.9
github.com/keegancsmith/rpc v1.3.0 // indirect
github.com/mozillazg/request v0.8.0
github.com/nsf/gocode v0.0.0-20190302080247-5bee97b48836 // indirect
github.com/spf13/cobra v1.1.1
github.com/stamblerre/gocode v1.0.0 // indirect
golang.org/x/mod v0.4.0 // indirect
golang.org/x/tools v0.0.0-20201211185031-d93e913c1a58 // indirect
)
Loading

0 comments on commit e1a090b

Please sign in to comment.