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