From 769b3073af9d2a340b630e2b6d4bbaf464374227 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 31 Oct 2015 13:08:10 +0900 Subject: [PATCH] WIP --- cmd/main.go | 5 ++--- file.go | 40 +++++++++++++++++++++------------------- goldsmith.go | 35 +++++++++++++++++++++++------------ types.go | 2 +- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 6ff1287..5aed606 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -24,11 +24,10 @@ package main import ( "github.com/FooSoft/goldsmith" - _ "github.com/FooSoft/goldsmith-plugins/markdown" + "github.com/FooSoft/goldsmith-plugins/markdown" ) func main() { gs := goldsmith.NewGoldsmith("/home/alex/projects/website/content/src") - // gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test") - gs.Complete("/home/alex/projects/test") + gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test") } diff --git a/file.go b/file.go index b79b660..233a0bd 100644 --- a/file.go +++ b/file.go @@ -28,18 +28,18 @@ import ( ) type file struct { - path string - meta map[string]interface{} - buff *bytes.Buffer - err error + relPath, srcPath string + meta map[string]interface{} + buff *bytes.Buffer + err error } func (f *file) Path() string { - return f.path + return f.relPath } func (f *file) SetPath(path string) { - f.path = path + f.relPath = path } func (f *file) Property(key, def string) interface{} { @@ -63,24 +63,26 @@ func (f *file) SetError(err error) { f.err = err } -func (f *file) Data() (*bytes.Buffer, error) { +func (f *file) Data() *bytes.Buffer { if f.buff != nil { - return f.buff, nil + return f.buff } - file, err := os.Open(f.path) - if err != nil { - f.SetError(err) - return nil, err - } - defer file.Close() - var buff bytes.Buffer - if _, err := buff.ReadFrom(file); err != nil { - f.SetError(err) - return nil, err + if len(f.srcPath) > 0 { + file, err := os.Open(f.srcPath) + if err != nil { + f.SetError(err) + return nil + } + defer file.Close() + + if _, err := buff.ReadFrom(file); err != nil { + f.SetError(err) + return nil + } } f.buff = &buff - return f.buff, nil + return f.buff } diff --git a/goldsmith.go b/goldsmith.go index bec8851..d948c26 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -24,6 +24,9 @@ package goldsmith import ( "io/ioutil" + "log" + "os" + "path" "path/filepath" "github.com/bmatcuk/doublestar" @@ -38,14 +41,14 @@ type goldsmith struct { files chan File } -func NewGoldsmith(path string) Goldsmith { +func NewGoldsmith(src string) Goldsmith { gs := new(goldsmith) - gs.scan(path) + gs.scan(src) return gs } -func (gs *goldsmith) scan(path string) error { - matches, err := doublestar.Glob(filepath.Join(path, "**")) +func (gs *goldsmith) scan(srcDir string) error { + matches, err := doublestar.Glob(filepath.Join(srcDir, "**")) if err != nil { return err } @@ -56,14 +59,15 @@ func (gs *goldsmith) scan(path string) error { } for _, match := range matches { - path, err := filepath.Rel(path, match) + relPath, err := filepath.Rel(srcDir, match) if err != nil { return err } - s.output <- gs.NewFile(path) + s.output <- &file{relPath: relPath, srcPath: match} } + close(s.output) gs.stages = append(gs.stages, s) return nil } @@ -78,8 +82,8 @@ func (gs *goldsmith) stage() stage { return s } -func (gs *goldsmith) NewFile(path string) File { - return &file{path: path} +func (gs *goldsmith) NewFile(relPath string) File { + return &file{relPath: relPath} } func (gs *goldsmith) Apply(p Processor) Goldsmith { @@ -88,18 +92,25 @@ func (gs *goldsmith) Apply(p Processor) Goldsmith { return gs } -func (gs *goldsmith) Complete(path string) []File { +func (gs *goldsmith) Complete(dstDir string) []File { s := gs.stages[len(gs.stages)-1] var files []File for file := range s.output { - data, err := file.Data() - if err != nil { + log.Print(file) + + data := file.Data() + if data == nil { + continue + } + + absPath := filepath.Join(dstDir, file.Path()) + + if err := os.MkdirAll(path.Dir(absPath), 0755); err != nil { file.SetError(err) continue } - absPath := filepath.Join(path, file.Path()) if err := ioutil.WriteFile(absPath, data.Bytes(), 0644); err != nil { file.SetError(err) continue diff --git a/types.go b/types.go index 14268d7..b84f7d9 100644 --- a/types.go +++ b/types.go @@ -38,7 +38,7 @@ type File interface { Error() error SetError(err error) - Data() (*bytes.Buffer, error) + Data() *bytes.Buffer } type Processor interface {