forked from astaxie/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
65 lines (60 loc) · 1.56 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
package main
import (
"fmt"
"github.com/russross/blackfriday"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
// 定义一个访问者结构体
type Visitor struct{}
func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
//fmt.Println("path: "+path)
if f == nil {
return err
}
if f.IsDir() {
return nil
} else if (f.Mode() & os.ModeSymlink) > 0 {
return nil
} else {
if strings.HasSuffix(f.Name(), ".md") {
//fmt.Println("path:"+path)
//fmt.Printf("file info: %+v\n", f)
file, err := os.Open(path )
if err != nil {
fmt.Println("open: "+path+" failure")
return err
}
input, _ := ioutil.ReadAll(file)
input = regexp.MustCompile("\\[(.*?)\\]\\(<?(.*?)\\.md>?\\)").ReplaceAll(input, []byte("[$1](<$2.html>)"))
output := blackfriday.MarkdownCommon(input)
var out *os.File
if out, err = os.Create(strings.Replace(path, ".md", ".html", -1)); err != nil {
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
os.Exit(-1)
}
defer out.Close()
header := "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\"/>\n</head>\n"
footer := "</html>\n"
out.Write([]byte(header))
if _, err = out.Write(output); err != nil {
fmt.Fprintln(os.Stderr, "Error writing output:", err)
os.Exit(-1)
}
out.Write([]byte(footer))
}
}
return nil
}
func main() {
v := &Visitor{}
err := filepath.Walk("./", func(path string, f os.FileInfo, err error) error {
return v.visit(path, f, err)
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}