package main
import ( "fmt" "io/ioutil" "os" "bufio" "strings" )
func main() { dirString := "." if len(os.Args) == 1 { fmt.Errorf("you need give the path to run, default to current directory") } else { dirString = os.Args[1] } fmt.Println("handle directory ", dirString) files, err := ioutil.ReadDir(dirString) checkErr(err) err = os.Mkdir("newPosts", 0644) if os.IsExist(err) == false { checkErr(err) } else { fmt.Printf("skip mkdir\n") } var line string var header []string var newPost string var linenum int for _, f := range files { desc := "" filename := f.Name() newFileName := filename fmt.Println("file:", filename) if strings.HasSuffix(filename, ".md") { fmt.Printf("handle markdown file: %s\n", filename) dateArr := strings.Split(filename, "-") dateStr := "" if len(dateArr) >= 4 { dateStr = "date: " + dateArr[0] + "/" + dateArr[1] + "/" + dateArr[2] + " 12:34:50\n" newFileName = string(filename[11:]) } f, err := os.Open(filename) checkErr(err) defer f.Close() scanner := bufio.NewScanner(f) scanner.Scan() linenum = 1 newPost = "---\n" if strings.Compare(dateStr, "") != 0 { newPost += dateStr } if strings.HasPrefix(scanner.Text(), "---") { for { scanner.Scan() linenum += 1 line = strings.Trim(scanner.Text(), " ") if strings.HasPrefix(line, "--") { break } header = strings.Split(line, ":") if len(header) < 2 { continue } header[1] = strings.TrimLeft(header[1], " ") if strings.Compare(header[0], "title") == 0 || strings.Compare(header[1], "layout") == 0 { newPost += line + "\n" } stmp := handleHeader(header[0], header[1]) if strings.Compare(stmp, "") != 0 { newPost += stmp } else if strings.Compare(header[0], "description") == 0 { desc = header[1] } } } newPost += "---\n" descReplced := false for scanner.Scan() { line = scanner.Text() linenum += 1 if descReplced == false { oldDesc := "{{ page.description }}" oldDesc2 := "{{page.description}}" if strings.Index(line, oldDesc) != -1 { fmt.Println("replace desc 1") line = strings.Replace(line, oldDesc, desc, 1) descReplced = true } else if strings.Index(line, oldDesc2) != -1 { fmt.Println("replace desc 2") line = strings.Replace(line, oldDesc2, desc, 1) descReplced = true } } braceIdx := strings.Index(line, "{{") if braceIdx != -1 { fmt.Println("handle braces at ", linenum) line = strings.Replace(line, "{{", "{% raw %} {{", -1) line = strings.Replace(line, "}}", "}} {% endraw %}", -1) } newPost += line + "\n" } newfile, err := os.Create( "newPosts/" + newFileName) checkErr(err) defer newfile.Close() newfile.Write([]byte(newPost)) } } }
func checkErr(err error) { if err != nil { panic(err) } }
func handleHeader(k, v string) string { var cats []string var res string v = strings.Trim(v, " ") if strings.Compare(k, "category") == 0 { res = "categories:\n" cats = strings.Split(v, " ") for _, cat := range cats { res += ("- " + cat + "\n") } return res } if strings.Compare(k, "tags") == 0 { res = "tags:\n" cats =strings.Split(v, " ") for _, cat := range cats { if strings.Compare(cat, " ") != 0 && strings.Compare(cat, "") != 0 { res += ("- " + cat + "\n") } } return res } return "" }
|