|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/fairlyblank/md2min" |
| 12 | +) |
| 13 | + |
| 14 | +// 定义一个访问者结构体 |
| 15 | +type Visitor struct{} |
| 16 | + |
| 17 | +func (self *Visitor) md2html(arg map[string]string) error { |
| 18 | + from := arg["from"] |
| 19 | + to := arg["to"] |
| 20 | + err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error { |
| 21 | + if f == nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + if f.IsDir() { |
| 25 | + return nil |
| 26 | + } |
| 27 | + if (f.Mode() & os.ModeSymlink) > 0 { |
| 28 | + return nil |
| 29 | + } |
| 30 | + if !strings.HasSuffix(f.Name(), ".md") { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + file, err := os.Open(path) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + input_byte, _ := ioutil.ReadAll(file) |
| 40 | + input := string(input_byte) |
| 41 | + input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)") |
| 42 | + |
| 43 | + if f.Name() == "README.md" { |
| 44 | + input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "") |
| 45 | + } |
| 46 | + |
| 47 | + // 以#开头的行,在#后增加空格 |
| 48 | + // 以#开头的行, 删除多余的空格 |
| 49 | + input = FixHeader(input) |
| 50 | + |
| 51 | + // 删除页面链接 |
| 52 | + input = RemoveFooterLink(input) |
| 53 | + |
| 54 | + // remove image suffix |
| 55 | + input = RemoveImageLinkSuffix(input) |
| 56 | + |
| 57 | + var out *os.File |
| 58 | + filename := strings.Replace(f.Name(), ".md", ".html", -1) |
| 59 | + fmt.Println(to + "/" + filename) |
| 60 | + if out, err = os.Create(to + "/" + filename); err != nil { |
| 61 | + fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err) |
| 62 | + os.Exit(-1) |
| 63 | + } |
| 64 | + defer out.Close() |
| 65 | + md := md2min.New("none") |
| 66 | + err = md.Parse([]byte(input), out) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintln(os.Stderr, "Parsing Error", err) |
| 69 | + os.Exit(-1) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | + }) |
| 74 | + return err |
| 75 | +} |
| 76 | + |
| 77 | +func FixHeader(input string) string { |
| 78 | + re_header := regexp.MustCompile(`(?m)^#.+$`) |
| 79 | + re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`) |
| 80 | + fixer := func(header string) string { |
| 81 | + s := re_sub.FindStringSubmatch(header) |
| 82 | + return s[1] + " " + s[2] |
| 83 | + } |
| 84 | + return re_header.ReplaceAllStringFunc(input, fixer) |
| 85 | +} |
| 86 | + |
| 87 | +func RemoveFooterLink(input string) string { |
| 88 | + re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`) |
| 89 | + return re_footer.ReplaceAllString(input, "") |
| 90 | +} |
| 91 | + |
| 92 | +func RemoveImageLinkSuffix(input string) string { |
| 93 | + re_footer := regexp.MustCompile(`png\?raw=true`) |
| 94 | + return re_footer.ReplaceAllString(input, "png") |
| 95 | +} |
| 96 | + |
| 97 | +func main() { |
| 98 | + tmp := os.Getenv("TMP") |
| 99 | + if tmp == "" { |
| 100 | + tmp = "." |
| 101 | + } |
| 102 | + |
| 103 | + workdir := os.Getenv("WORKDIR") |
| 104 | + if workdir == "" { |
| 105 | + workdir = "." |
| 106 | + } |
| 107 | + |
| 108 | + arg := map[string]string{ |
| 109 | + "from": workdir, |
| 110 | + "to": tmp, |
| 111 | + } |
| 112 | + |
| 113 | + v := &Visitor{} |
| 114 | + err := v.md2html(arg) |
| 115 | + if err != nil { |
| 116 | + fmt.Printf("filepath.Walk() returned %v\n", err) |
| 117 | + } |
| 118 | +} |
0 commit comments