Handlers WIP

This commit is contained in:
spf13 2014-10-20 17:42:16 -04:00
parent 8bd0ffba43
commit 93f3d604c6
5 changed files with 74 additions and 41 deletions

View file

@ -13,8 +13,11 @@
package hugolib package hugolib
import "github.com/spf13/hugo/source"
var Filer interface { var Filer interface {
Read() Read(*source.File)
Render() Render()
Convert() Convert()
Extensions() []string
} }

View file

@ -13,8 +13,31 @@
package hugolib package hugolib
import "github.com/spf13/hugo/source"
var Pager interface { var Pager interface {
Read() Read(*source.File)
Render() Render()
Convert() 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)
} }

View file

@ -13,15 +13,40 @@
package hugolib package hugolib
var Handler interface { import "github.com/spf13/hugo/source"
Read()
Render() type Handler interface {
Convert() Read(*source.File, HandleResults)
Extensions() //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 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) { func RegisterHandler(h Handler) {
handlers = append(handlers, h) handlers = append(handlers, h)
} }
@ -30,16 +55,16 @@ func Handlers() []Handler {
return handlers return handlers
} }
func Handler(ext string) Handler { func FindHandler(ext string) Handler {
for _, h := range Handlers() { for _, h := range Handlers() {
if h.Match(ext) { if HandlerMatch(h, ext) {
return h return h
} }
} }
return nil return nil
} }
func (h Handler) Match(ext string) bool { func HandlerMatch(h Handler, ext string) bool {
for _, x := range h.Extensions() { for _, x := range h.Extensions() {
if ext == x { if ext == x {
return true return true

View file

@ -54,20 +54,12 @@ type Page struct {
frontmatter []byte frontmatter []byte
rawContent []byte rawContent []byte
plain string // TODO should be []byte plain string // TODO should be []byte
//sourceFrontmatter []byte
//sourceContent []byte
PageMeta PageMeta
//SourceFile source.File
Source Source
Position Position
Node Node
//Destination source.File
} }
//type File struct {
//Name, FileName, Extension, Dir, UniqueId string
//}
type Source struct { type Source struct {
Frontmatter []byte Frontmatter []byte
Content []byte Content []byte

View file

@ -331,7 +331,7 @@ func (s *Site) CreatePages() error {
files := s.Source.Files() files := s.Source.Files()
results := make(chan pageResult) results := make(chan HandledResult)
filechan := make(chan *source.File) filechan := make(chan *source.File)
procs := getGoMaxProcs() procs := getGoMaxProcs()
@ -361,7 +361,7 @@ func (s *Site) CreatePages() error {
readErrs := <-errs readErrs := <-errs
results = make(chan pageResult) results = make(chan HandledResult)
pagechan := make(chan *Page) pagechan := make(chan *Page)
wg = &sync.WaitGroup{} wg = &sync.WaitGroup{}
@ -397,33 +397,23 @@ func (s *Site) CreatePages() error {
return fmt.Errorf("%s\n%s", readErrs, renderErrs) 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() defer wg.Done()
for file := range files { for file := range files {
// TODO: Switch here on extension h := FindHandler(file.Extension())
h := handlers.Handler(file.Extension())
if h != nil { if h != nil {
h.Read(file, results)
} else { } else {
jww.ERROR.Println("Unsupported File Type", file.Path()) jww.ERROR.Println("Unsupported File Type", file.Path())
} }
page, err := NewPage(file.Path()) // TODO: Figure out Site stuff
if err != nil { //page.Site = &s.Info
results <- pageResult{nil, err} //page.Tmpl = s.Tmpl
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}
} }
} }
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() defer wg.Done()
for page := range pages { for page := range pages {
//Handling short codes prior to Conversion to HTML //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() err := page.Convert()
if err != nil { if err != nil {
results <- pageResult{nil, err} results <- HandledResult{err: err}
continue 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{} errMsgs := []string{}
for r := range results { for r := range results {
if r.err != nil { 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")) 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{} errMsgs := []string{}
for r := range results { for r := range results {
if r.err != nil { if r.err != nil {