From 93f3d604c6403c9e018a55590cfb30bc06f670dd Mon Sep 17 00:00:00 2001 From: spf13 Date: Mon, 20 Oct 2014 17:42:16 -0400 Subject: [PATCH] Handlers WIP --- hugolib/{handlers/file.go => handler_file.go} | 5 ++- hugolib/{handlers/page.go => handler_page.go} | 25 ++++++++++- hugolib/{handlers => }/handlers.go | 41 +++++++++++++++---- hugolib/page.go | 8 ---- hugolib/site.go | 36 ++++++---------- 5 files changed, 74 insertions(+), 41 deletions(-) rename hugolib/{handlers/file.go => handler_file.go} (89%) rename hugolib/{handlers/page.go => handler_page.go} (56%) rename hugolib/{handlers => }/handlers.go (59%) diff --git a/hugolib/handlers/file.go b/hugolib/handler_file.go similarity index 89% rename from hugolib/handlers/file.go rename to hugolib/handler_file.go index d69ce063d..32df80661 100644 --- a/hugolib/handlers/file.go +++ b/hugolib/handler_file.go @@ -13,8 +13,11 @@ package hugolib +import "github.com/spf13/hugo/source" + var Filer interface { - Read() + Read(*source.File) Render() Convert() + Extensions() []string } diff --git a/hugolib/handlers/page.go b/hugolib/handler_page.go similarity index 56% rename from hugolib/handlers/page.go rename to hugolib/handler_page.go index 6d0545b4e..47fa85cdd 100644 --- a/hugolib/handlers/page.go +++ b/hugolib/handler_page.go @@ -13,8 +13,31 @@ package hugolib +import "github.com/spf13/hugo/source" + var Pager interface { - Read() + Read(*source.File) Render() Convert() + Extensions() []string +} + +var markdown = Handle{ + extensions: []string{"mdown", "markdown", "md"}, + readrun: func(f *source.File, results HandleResults) { + page, err := NewPage(f.Path()) + if err != nil { + results <- HandledResult{file: f, err: err} + } + + if err := page.ReadFrom(f.Contents); err != nil { + results <- HandledResult{file: f, err: err} + } + + results <- HandledResult{file: f, page: page, err: err} + }, +} + +func init() { + RegisterHandler(markdown) } diff --git a/hugolib/handlers/handlers.go b/hugolib/handlers.go similarity index 59% rename from hugolib/handlers/handlers.go rename to hugolib/handlers.go index c6c24a959..2aaf8cc48 100644 --- a/hugolib/handlers/handlers.go +++ b/hugolib/handlers.go @@ -13,15 +13,40 @@ package hugolib -var Handler interface { - Read() - Render() - Convert() - Extensions() +import "github.com/spf13/hugo/source" + +type Handler interface { + Read(*source.File, HandleResults) + //Render() + //Convert() + Extensions() []string +} + +type HandledResult struct { + page *Page + file *source.File + err error +} + +type HandleResults chan<- HandledResult + +type ReadFunc func(*source.File, HandleResults) + +type Handle struct { + extensions []string + readrun ReadFunc } var handlers []Handler +func (h Handle) Extensions() []string { + return h.extensions +} + +func (h Handle) Read(s *source.File, results HandleResults) { + h.readrun(s, results) +} + func RegisterHandler(h Handler) { handlers = append(handlers, h) } @@ -30,16 +55,16 @@ func Handlers() []Handler { return handlers } -func Handler(ext string) Handler { +func FindHandler(ext string) Handler { for _, h := range Handlers() { - if h.Match(ext) { + if HandlerMatch(h, ext) { return h } } return nil } -func (h Handler) Match(ext string) bool { +func HandlerMatch(h Handler, ext string) bool { for _, x := range h.Extensions() { if ext == x { return true diff --git a/hugolib/page.go b/hugolib/page.go index f5b5a2cdb..5ca1ffb52 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -54,20 +54,12 @@ type Page struct { frontmatter []byte rawContent []byte plain string // TODO should be []byte - //sourceFrontmatter []byte - //sourceContent []byte PageMeta - //SourceFile source.File Source Position Node - //Destination source.File } -//type File struct { -//Name, FileName, Extension, Dir, UniqueId string -//} - type Source struct { Frontmatter []byte Content []byte diff --git a/hugolib/site.go b/hugolib/site.go index 9c96ff7c9..2db711543 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -331,7 +331,7 @@ func (s *Site) CreatePages() error { files := s.Source.Files() - results := make(chan pageResult) + results := make(chan HandledResult) filechan := make(chan *source.File) procs := getGoMaxProcs() @@ -361,7 +361,7 @@ func (s *Site) CreatePages() error { readErrs := <-errs - results = make(chan pageResult) + results = make(chan HandledResult) pagechan := make(chan *Page) wg = &sync.WaitGroup{} @@ -397,33 +397,23 @@ func (s *Site) CreatePages() error { return fmt.Errorf("%s\n%s", readErrs, renderErrs) } -func sourceReader(s *Site, files <-chan *source.File, results chan<- pageResult, wg *sync.WaitGroup) { +func sourceReader(s *Site, files <-chan *source.File, results chan<- HandledResult, wg *sync.WaitGroup) { defer wg.Done() for file := range files { - // TODO: Switch here on extension - h := handlers.Handler(file.Extension()) + h := FindHandler(file.Extension()) if h != nil { - + h.Read(file, results) } else { jww.ERROR.Println("Unsupported File Type", file.Path()) } - page, err := NewPage(file.Path()) - if err != nil { - results <- pageResult{nil, err} - continue - } - page.Site = &s.Info - page.Tmpl = s.Tmpl - if err := page.ReadFrom(file.Contents); err != nil { - results <- pageResult{nil, err} - continue - } - results <- pageResult{page, nil} + // TODO: Figure out Site stuff + //page.Site = &s.Info + //page.Tmpl = s.Tmpl } } -func pageConverter(s *Site, pages <-chan *Page, results chan<- pageResult, wg *sync.WaitGroup) { +func pageConverter(s *Site, pages <-chan *Page, results HandleResults, wg *sync.WaitGroup) { defer wg.Done() for page := range pages { //Handling short codes prior to Conversion to HTML @@ -431,14 +421,14 @@ func pageConverter(s *Site, pages <-chan *Page, results chan<- pageResult, wg *s err := page.Convert() if err != nil { - results <- pageResult{nil, err} + results <- HandledResult{err: err} continue } - results <- pageResult{page, nil} + results <- HandledResult{err: err} } } -func converterCollator(s *Site, results <-chan pageResult, errs chan<- error) { +func converterCollator(s *Site, results <-chan HandledResult, errs chan<- error) { errMsgs := []string{} for r := range results { if r.err != nil { @@ -453,7 +443,7 @@ func converterCollator(s *Site, results <-chan pageResult, errs chan<- error) { errs <- fmt.Errorf("Errors rendering pages: %s", strings.Join(errMsgs, "\n")) } -func readCollator(s *Site, results <-chan pageResult, errs chan<- error) { +func readCollator(s *Site, results <-chan HandledResult, errs chan<- error) { errMsgs := []string{} for r := range results { if r.err != nil {