From dfb848256994289f5571fae8343578cf455c6431 Mon Sep 17 00:00:00 2001 From: spf13 Date: Mon, 20 Oct 2014 20:15:33 -0400 Subject: [PATCH] Handlers WIP - Convert now working --- helpers/general.go | 9 ++++++ hugolib/handler_page.go | 11 ++++++- hugolib/handlers.go | 20 +++++++++--- hugolib/site.go | 71 ++++++++++++++++++++++++++--------------- 4 files changed, 80 insertions(+), 31 deletions(-) diff --git a/helpers/general.go b/helpers/general.go index 89e3857b0..f68908d9a 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -36,6 +36,15 @@ func FindAvailablePort() (*net.TCPAddr, error) { return nil, err } +func InStringArray(arr []string, el string) bool { + for _, v := range arr { + if v == el { + return true + } + } + return false +} + func GuessType(in string) string { switch strings.ToLower(in) { case "md", "markdown", "mdown": diff --git a/hugolib/handler_page.go b/hugolib/handler_page.go index 7f96ba3c8..dd30dc4d5 100644 --- a/hugolib/handler_page.go +++ b/hugolib/handler_page.go @@ -24,7 +24,7 @@ var Pager interface { var markdown = Handle{ extensions: []string{"mdown", "markdown", "md"}, - readrun: func(f *source.File, s *Site, results HandleResults) { + read: func(f *source.File, s *Site, results HandleResults) { page, err := NewPage(f.Path()) if err != nil { results <- HandledResult{file: f, err: err} @@ -39,6 +39,15 @@ var markdown = Handle{ results <- HandledResult{file: f, page: page, err: err} }, + pageConvert: func(p *Page, s *Site, results HandleResults) { + p.ProcessShortcodes(s.Tmpl) + err := p.Convert() + if err != nil { + results <- HandledResult{err: err} + } + + results <- HandledResult{err: err} + }, } func init() { diff --git a/hugolib/handlers.go b/hugolib/handlers.go index f406744b8..d00a7497c 100644 --- a/hugolib/handlers.go +++ b/hugolib/handlers.go @@ -18,7 +18,7 @@ import "github.com/spf13/hugo/source" type Handler interface { Read(*source.File, *Site, HandleResults) //Render() - //Convert() + Convert(interface{}, *Site, HandleResults) Extensions() []string } @@ -31,10 +31,14 @@ type HandledResult struct { type HandleResults chan<- HandledResult type ReadFunc func(*source.File, *Site, HandleResults) +type PageConvertFunc func(*Page, *Site, HandleResults) +type FileConvertFunc ReadFunc type Handle struct { - extensions []string - readrun ReadFunc + extensions []string + read ReadFunc + pageConvert PageConvertFunc + fileConvert FileConvertFunc } var handlers []Handler @@ -44,7 +48,15 @@ func (h Handle) Extensions() []string { } func (h Handle) Read(f *source.File, s *Site, results HandleResults) { - h.readrun(f, s, results) + h.read(f, s, results) +} + +func (h Handle) Convert(i interface{}, s *Site, results HandleResults) { + if h.pageConvert != nil { + h.pageConvert(i.(*Page), s, results) + } else { + h.fileConvert(i.(*source.File), s, results) + } } func RegisterHandler(h Handler) { diff --git a/hugolib/site.go b/hugolib/site.go index ab464f7a1..5a700764d 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -59,6 +59,7 @@ var DefaultTimer *nitro.B // 5. The entire collection of files is written to disk. type Site struct { Pages Pages + Files []*source.File Tmpl Template Taxonomies TaxonomyList Source source.Input @@ -82,6 +83,7 @@ type SiteInfo struct { Indexes *TaxonomyList // legacy, should be identical to Taxonomies Sections Taxonomy Pages *Pages + Files []*source.File Recent *Pages // legacy, should be identical to Pages Menus *Menus Title string @@ -363,9 +365,16 @@ func (s *Site) CreatePages() error { results = make(chan HandledResult) pagechan := make(chan *Page) + filechan = make(chan *source.File) wg = &sync.WaitGroup{} + for i := 0; i < procs*4; i++ { + wg.Add(1) + go fileConverter(s, filechan, results, wg) + } + + wg = &sync.WaitGroup{} for i := 0; i < procs*4; i++ { wg.Add(1) go pageConverter(s, pagechan, results, wg) @@ -377,7 +386,13 @@ func (s *Site) CreatePages() error { pagechan <- p } + for _, f := range s.Files { + fmt.Println(f) + filechan <- f + } + close(pagechan) + close(filechan) wg.Wait() @@ -412,15 +427,22 @@ func sourceReader(s *Site, files <-chan *source.File, results chan<- HandledResu 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 - page.ProcessShortcodes(s.Tmpl) - - err := page.Convert() - if err != nil { - results <- HandledResult{err: err} - continue + h := FindHandler(page.File.Extension()) + if h != nil { + h.Convert(page, s, results) + } + } +} + +func fileConverter(s *Site, files <-chan *source.File, results HandleResults, wg *sync.WaitGroup) { + defer wg.Done() + for file := range files { + fmt.Println(file.Path()) + //Handling short codes prior to Conversion to HTML + h := FindHandler(file.Extension()) + if h != nil { + h.Convert(file, s, results) } - results <- HandledResult{err: err} } } @@ -447,18 +469,24 @@ func readCollator(s *Site, results <-chan HandledResult, errs chan<- error) { continue } - if r.page.ShouldBuild() { - s.Pages = append(s.Pages, r.page) - } + // !page == file + if r.page == nil { + s.Files = append(s.Files, r.file) + } else { + if r.page.ShouldBuild() { + s.Pages = append(s.Pages, r.page) + } - if r.page.IsDraft() { - s.draftCount += 1 - } + if r.page.IsDraft() { + s.draftCount += 1 + } - if r.page.IsFuture() { - s.futureCount += 1 + if r.page.IsFuture() { + s.futureCount += 1 + } } } + s.Pages.Sort() if len(errMsgs) == 0 { errs <- nil @@ -623,7 +651,7 @@ func (s *Site) assembleSections() { func (s *Site) possibleTaxonomies() (taxonomies []string) { for _, p := range s.Pages { for k := range p.Params { - if !inStringArray(taxonomies, k) { + if !helpers.InStringArray(taxonomies, k) { taxonomies = append(taxonomies, k) } } @@ -631,15 +659,6 @@ func (s *Site) possibleTaxonomies() (taxonomies []string) { return } -func inStringArray(arr []string, el string) bool { - for _, v := range arr { - if v == el { - return true - } - } - return false -} - // Render shell pages that simply have a redirect in the header func (s *Site) RenderAliases() error { for _, p := range s.Pages {