This commit is contained in:
Alex Yatskov 2015-10-31 13:08:10 +09:00
parent 5bd898f11f
commit 769b3073af
4 changed files with 47 additions and 35 deletions

View File

@ -24,11 +24,10 @@ package main
import ( import (
"github.com/FooSoft/goldsmith" "github.com/FooSoft/goldsmith"
_ "github.com/FooSoft/goldsmith-plugins/markdown" "github.com/FooSoft/goldsmith-plugins/markdown"
) )
func main() { func main() {
gs := goldsmith.NewGoldsmith("/home/alex/projects/website/content/src") gs := goldsmith.NewGoldsmith("/home/alex/projects/website/content/src")
// gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test") gs.Apply(markdown.NewMarkdown()).Complete("/home/alex/projects/test")
gs.Complete("/home/alex/projects/test")
} }

40
file.go
View File

@ -28,18 +28,18 @@ import (
) )
type file struct { type file struct {
path string relPath, srcPath string
meta map[string]interface{} meta map[string]interface{}
buff *bytes.Buffer buff *bytes.Buffer
err error err error
} }
func (f *file) Path() string { func (f *file) Path() string {
return f.path return f.relPath
} }
func (f *file) SetPath(path string) { func (f *file) SetPath(path string) {
f.path = path f.relPath = path
} }
func (f *file) Property(key, def string) interface{} { func (f *file) Property(key, def string) interface{} {
@ -63,24 +63,26 @@ func (f *file) SetError(err error) {
f.err = err f.err = err
} }
func (f *file) Data() (*bytes.Buffer, error) { func (f *file) Data() *bytes.Buffer {
if f.buff != nil { 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 var buff bytes.Buffer
if _, err := buff.ReadFrom(file); err != nil { if len(f.srcPath) > 0 {
f.SetError(err) file, err := os.Open(f.srcPath)
return nil, err 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 f.buff = &buff
return f.buff, nil return f.buff
} }

View File

@ -24,6 +24,9 @@ package goldsmith
import ( import (
"io/ioutil" "io/ioutil"
"log"
"os"
"path"
"path/filepath" "path/filepath"
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
@ -38,14 +41,14 @@ type goldsmith struct {
files chan File files chan File
} }
func NewGoldsmith(path string) Goldsmith { func NewGoldsmith(src string) Goldsmith {
gs := new(goldsmith) gs := new(goldsmith)
gs.scan(path) gs.scan(src)
return gs return gs
} }
func (gs *goldsmith) scan(path string) error { func (gs *goldsmith) scan(srcDir string) error {
matches, err := doublestar.Glob(filepath.Join(path, "**")) matches, err := doublestar.Glob(filepath.Join(srcDir, "**"))
if err != nil { if err != nil {
return err return err
} }
@ -56,14 +59,15 @@ func (gs *goldsmith) scan(path string) error {
} }
for _, match := range matches { for _, match := range matches {
path, err := filepath.Rel(path, match) relPath, err := filepath.Rel(srcDir, match)
if err != nil { if err != nil {
return err return err
} }
s.output <- gs.NewFile(path) s.output <- &file{relPath: relPath, srcPath: match}
} }
close(s.output)
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
return nil return nil
} }
@ -78,8 +82,8 @@ func (gs *goldsmith) stage() stage {
return s return s
} }
func (gs *goldsmith) NewFile(path string) File { func (gs *goldsmith) NewFile(relPath string) File {
return &file{path: path} return &file{relPath: relPath}
} }
func (gs *goldsmith) Apply(p Processor) Goldsmith { func (gs *goldsmith) Apply(p Processor) Goldsmith {
@ -88,18 +92,25 @@ func (gs *goldsmith) Apply(p Processor) Goldsmith {
return gs return gs
} }
func (gs *goldsmith) Complete(path string) []File { func (gs *goldsmith) Complete(dstDir string) []File {
s := gs.stages[len(gs.stages)-1] s := gs.stages[len(gs.stages)-1]
var files []File var files []File
for file := range s.output { for file := range s.output {
data, err := file.Data() log.Print(file)
if err != nil {
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) file.SetError(err)
continue continue
} }
absPath := filepath.Join(path, file.Path())
if err := ioutil.WriteFile(absPath, data.Bytes(), 0644); err != nil { if err := ioutil.WriteFile(absPath, data.Bytes(), 0644); err != nil {
file.SetError(err) file.SetError(err)
continue continue

View File

@ -38,7 +38,7 @@ type File interface {
Error() error Error() error
SetError(err error) SetError(err error)
Data() (*bytes.Buffer, error) Data() *bytes.Buffer
} }
type Processor interface { type Processor interface {