Handlers WIP - Convert now working

This commit is contained in:
spf13 2014-10-20 20:15:33 -04:00
parent 30d4a26ba0
commit dfb8482569
4 changed files with 80 additions and 31 deletions

View file

@ -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":

View file

@ -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() {

View file

@ -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) {

View file

@ -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 {