From 61943804508700b7e4960b2092f9cae502d91eb4 Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 23 Aug 2012 13:59:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=88=90html=E7=9A=84go=E6=96=87=E4=BB=B6=EF=BC=8C=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=81=9A=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 build.go diff --git a/build.go b/build.go new file mode 100644 index 00000000..f65254de --- /dev/null +++ b/build.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "github.com/russross/blackfriday" + "io/ioutil" + "os" + "path/filepath" +) + +//定义一个访问者结构体 +type Visitor struct{} + +//定义其碰到目录的时候的行为 +func (v *Visitor) VisitDir(path string, f *os.FileInfo) bool { + fmt.Println(path) + return true +} + +//定义其碰到文件的时候的行为 +func (v *Visitor) VisitFile(path string, f *os.FileInfo) { + fmt.Println(path) + //@todo 如何读取md文件转化为html + input, err := ioutil.ReadAll() + output := blackfriday.MarkdownBasic(input) +} + +func main() { + v := &Visitor{} + errors := make(chan os.Error, 64) //错误消息使用64个缓存,可以随意 + filepath.Walk("./", v, errors) + select { + case err := <-errors: + panic(err) + default: + } +} From c0ce8e5406970bcdf80ce41fd5a2ef07a1ab08ec Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 23 Aug 2012 14:59:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BA=86=EF=BC=8C=E5=8F=AF=E4=BB=A5=E7=94=9F?= =?UTF-8?q?=E6=88=90html=E6=96=87=E4=BB=B6=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.go | 55 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/build.go b/build.go index f65254de..b8522a06 100644 --- a/build.go +++ b/build.go @@ -6,32 +6,51 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" ) //定义一个访问者结构体 type Visitor struct{} -//定义其碰到目录的时候的行为 -func (v *Visitor) VisitDir(path string, f *os.FileInfo) bool { - fmt.Println(path) - return true -} - -//定义其碰到文件的时候的行为 -func (v *Visitor) VisitFile(path string, f *os.FileInfo) { - fmt.Println(path) - //@todo 如何读取md文件转化为html - input, err := ioutil.ReadAll() - output := blackfriday.MarkdownBasic(input) +func (self *Visitor) visit(path string, f os.FileInfo, err error) error { + if f == nil { + return err + } + if f.IsDir() { + return nil + } else if (f.Mode() & os.ModeSymlink) > 0 { + return nil + } else { + if strings.Contains(f.Name(), ".md") { + fmt.Println(f) + file, err := os.Open(f.Name()) + if err != nil { + return err + } + input, _ := ioutil.ReadAll(file) + output := blackfriday.MarkdownCommon(input) + var out *os.File + if out, err = os.Create(f.Name() + ".html"); err != nil { + fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err) + os.Exit(-1) + } + defer out.Close() + if _, err = out.Write(output); err != nil { + fmt.Fprintln(os.Stderr, "Error writing output:", err) + os.Exit(-1) + } + } + } + return nil } func main() { v := &Visitor{} - errors := make(chan os.Error, 64) //错误消息使用64个缓存,可以随意 - filepath.Walk("./", v, errors) - select { - case err := <-errors: - panic(err) - default: + 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) } } From 92b5a86136cd49af324a215fe843df1ba04774b9 Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 23 Aug 2012 15:06:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?readme=E5=A2=9E=E5=8A=A0=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e5a38edd..5213a43a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ ## 格式规范 请参看已有章节的规范, 要注意的是, 每个章节在底部都需要有一个links节, 包含"目录", "上一节", "下一节"的链接 +##如何编译 +目前可以把相应的Markdown编译成html文件,执行`go build build.go`,执行生成的文件,就会在底目录下生成相应的html文件 + ##致谢 首先要感谢golang的QQ群102319854,里面的每一个人都很热心,同时要特别感谢几个人