update at 2020-11-02 17:23:06 by ehlxr

master
ehlxr 2020-11-02 17:23:06 +08:00
parent 61ff5e9e8e
commit 3c8fe515d7
2 changed files with 66 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
) )
func main() { func main() {
@ -61,6 +62,14 @@ func main() {
active: false, active: false,
} }
fmt.Println(user) 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 { type User struct {

View File

@ -14,8 +14,8 @@ import (
func main() { func main() {
prompt := promptui.SelectWithAdd{ prompt := promptui.SelectWithAdd{
// Label: "What's your source file path", Label: "What's your source file path",
// Items: []string{"/Users/ehlxr/ehlxr/blog/Hexo/source/resume/index.md"}, Items: []string{"/Users/ehlxr/WorkSpaces/blog/Hexo/source/_posts"},
AddLabel: "Input your source file path", AddLabel: "Input your source file path",
} }
@ -27,15 +27,15 @@ func main() {
files := getFiles(source) files := getFiles(source)
for _, file := range files { for _, file := range files {
// has, err := hasSlug(file) has, err := hasSlug(file)
// if err != nil { if err != nil {
// fmt.Printf("hasSlug file: %s failed: %v\n", file, err) //fmt.Printf("hasSlug file: %s failed: %v\n", file, err)
// continue continue
// } }
// if has { if has {
// println("file have content slug already") //println("file have content slug already")
// continue continue
// } }
// //
// bytes, err := handleText(file) // bytes, err := handleText(file)
// if err != nil { // if err != nil {
@ -53,10 +53,20 @@ func main() {
// fmt.Printf("deal file: %s done!!!\n", file) // 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 { if err != nil {
fmt.Printf("genDesc file: %s failed: %v\n", file, err) 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)) bytes, err := genNew(file, string(desc))
if err != nil { if err != nil {
@ -87,7 +97,7 @@ func hasSlug(file string) (bool, error) {
return false, err return false, err
} }
if strings.Index(string(fd), "slug:") > -1 { if strings.Index(string(fd), "index_img:") > -1 {
return true, nil return true, nil
} }
@ -168,6 +178,39 @@ func genDesc(path string) ([]byte, error) {
return output, nil 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) { func genNew(path string, des string) ([]byte, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644) file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil { if err != nil {
@ -190,7 +233,7 @@ func genNew(path string, des string) ([]byte, error) {
if identifier == 2 && line == "---" { if identifier == 2 && line == "---" {
identifier++ identifier++
output = append(output, fmt.Sprintf("description: \"%s\"", des)...) output = append(output, fmt.Sprintf("index_img: \"%s\"", des)...)
output = append(output, []byte("\n")...) output = append(output, []byte("\n")...)
} }