From 3c8fe515d7e14ade9b49ea48d1104afa07cb584f Mon Sep 17 00:00:00 2001 From: ehlxr Date: Mon, 2 Nov 2020 17:23:06 +0800 Subject: [PATCH] update at 2020-11-02 17:23:06 by ehlxr --- main.go | 9 +++++ utils/blogmigrate/main.go | 71 +++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 722cc8c..61c1357 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "strings" ) func main() { @@ -61,6 +62,14 @@ func main() { active: false, } fmt.Println(user) + + fmt.Println("Hello World!") + + a := "https://cdn.jsdelivr.net/gh/0vo/oss/images/kafka-logo.png 400 %}\n" + a1 := strings.Split(a, " ")[0] + + fmt.Println(a1) //输出为:www.waylau.com/golang-strings-split-get-url/ + fmt.Println(strings.Index(a, "https://cdn.jsdelivr.net/gh/0vo/oss/images")) //输出为:www.waylau.com } type User struct { diff --git a/utils/blogmigrate/main.go b/utils/blogmigrate/main.go index bb3bfc5..79534ca 100644 --- a/utils/blogmigrate/main.go +++ b/utils/blogmigrate/main.go @@ -14,8 +14,8 @@ import ( func main() { prompt := promptui.SelectWithAdd{ - // Label: "What's your source file path", - // Items: []string{"/Users/ehlxr/ehlxr/blog/Hexo/source/resume/index.md"}, + Label: "What's your source file path", + Items: []string{"/Users/ehlxr/WorkSpaces/blog/Hexo/source/_posts"}, AddLabel: "Input your source file path", } @@ -27,15 +27,15 @@ func main() { files := getFiles(source) for _, file := range files { - // has, err := hasSlug(file) - // if err != nil { - // fmt.Printf("hasSlug file: %s failed: %v\n", file, err) - // continue - // } - // if has { - // println("file have content slug already") - // continue - // } + has, err := hasSlug(file) + if err != nil { + //fmt.Printf("hasSlug file: %s failed: %v\n", file, err) + continue + } + if has { + //println("file have content slug already") + continue + } // // bytes, err := handleText(file) // if err != nil { @@ -53,10 +53,20 @@ func main() { // fmt.Printf("deal file: %s done!!!\n", file) // } - desc, err := genDesc(file) + //desc, err := genDesc(file) + //if err != nil { + // fmt.Printf("genDesc file: %s failed: %v\n", file, err) + //} + + desc, err := genIndexImg(file) if err != nil { fmt.Printf("genDesc file: %s failed: %v\n", file, err) + continue } + if len(desc) <= 0 { + continue + } + println(file, "=", string(desc)) bytes, err := genNew(file, string(desc)) if err != nil { @@ -87,7 +97,7 @@ func hasSlug(file string) (bool, error) { return false, err } - if strings.Index(string(fd), "slug:") > -1 { + if strings.Index(string(fd), "index_img:") > -1 { return true, nil } @@ -168,6 +178,39 @@ func genDesc(path string) ([]byte, error) { return output, nil } +func genIndexImg(path string) ([]byte, error) { + file, err := os.OpenFile(path, os.O_RDONLY, 0644) + if err != nil { + log.Printf("cannot open text file: %s, err: [%v]", path, err) + return nil, err + } + defer file.Close() + + // 按行读取文件 + scanner := bufio.NewScanner(file) + output := make([]byte, 0) + for scanner.Scan() { + line := scanner.Text() + // line := scanner.Bytes() + + if strings.Index(line, "https://cdn.jsdelivr.net/gh/0vo/oss/images") > -1 { + newByte := strings.Split(line, "https")[1] + newByte = strings.Split(newByte, " ")[0] + newByte = strings.Split(newByte, ")")[0] + output = append(output, "https"...) + output = append(output, newByte...) + break + } + } + + if err := scanner.Err(); err != nil { + log.Printf("cannot scanner text file: %s, err: [%v]", path, err) + return nil, err + } + + return output, nil +} + func genNew(path string, des string) ([]byte, error) { file, err := os.OpenFile(path, os.O_RDONLY, 0644) if err != nil { @@ -190,7 +233,7 @@ func genNew(path string, des string) ([]byte, error) { if identifier == 2 && line == "---" { identifier++ - output = append(output, fmt.Sprintf("description: \"%s\"", des)...) + output = append(output, fmt.Sprintf("index_img: \"%s\"", des)...) output = append(output, []byte("\n")...) }