shithub: hugo

Download patch

ref: 93f3d604c6403c9e018a55590cfb30bc06f670dd
parent: 8bd0ffba439c1df2e98113dffbee5203fd004e6f
author: spf13 <[email protected]>
date: Mon Oct 20 13:42:16 EDT 2014

Handlers WIP

--- /dev/null
+++ b/hugolib/handler_file.go
@@ -1,0 +1,23 @@
+// Copyright © 2014 Steve Francia <[email protected]>.
+//
+// Licensed under the Simple Public License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://opensource.org/licenses/Simple-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import "github.com/spf13/hugo/source"
+
+var Filer interface {
+	Read(*source.File)
+	Render()
+	Convert()
+	Extensions() []string
+}
--- /dev/null
+++ b/hugolib/handler_page.go
@@ -1,0 +1,43 @@
+// Copyright © 2014 Steve Francia <[email protected]>.
+//
+// Licensed under the Simple Public License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://opensource.org/licenses/Simple-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import "github.com/spf13/hugo/source"
+
+var Pager interface {
+	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)
+}
--- /dev/null
+++ b/hugolib/handlers.go
@@ -1,0 +1,74 @@
+// Copyright © 2014 Steve Francia <[email protected]>.
+//
+// Licensed under the Simple Public License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://opensource.org/licenses/Simple-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+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)
+}
+
+func Handlers() []Handler {
+	return handlers
+}
+
+func FindHandler(ext string) Handler {
+	for _, h := range Handlers() {
+		if HandlerMatch(h, ext) {
+			return h
+		}
+	}
+	return nil
+}
+
+func HandlerMatch(h Handler, ext string) bool {
+	for _, x := range h.Extensions() {
+		if ext == x {
+			return true
+		}
+	}
+	return false
+}
--- a/hugolib/handlers/file.go
+++ /dev/null
@@ -1,20 +1,0 @@
-// Copyright © 2014 Steve Francia <[email protected]>.
-//
-// Licensed under the Simple Public License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// http://opensource.org/licenses/Simple-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package hugolib
-
-var Filer interface {
-	Read()
-	Render()
-	Convert()
-}
--- a/hugolib/handlers/handlers.go
+++ /dev/null
@@ -1,49 +1,0 @@
-// Copyright © 2014 Steve Francia <[email protected]>.
-//
-// Licensed under the Simple Public License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// http://opensource.org/licenses/Simple-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package hugolib
-
-var Handler interface {
-	Read()
-	Render()
-	Convert()
-	Extensions()
-}
-
-var handlers []Handler
-
-func RegisterHandler(h Handler) {
-	handlers = append(handlers, h)
-}
-
-func Handlers() []Handler {
-	return handlers
-}
-
-func Handler(ext string) Handler {
-	for _, h := range Handlers() {
-		if h.Match(ext) {
-			return h
-		}
-	}
-	return nil
-}
-
-func (h Handler) Match(ext string) bool {
-	for _, x := range h.Extensions() {
-		if ext == x {
-			return true
-		}
-	}
-	return false
-}
--- a/hugolib/handlers/page.go
+++ /dev/null
@@ -1,20 +1,0 @@
-// Copyright © 2014 Steve Francia <[email protected]>.
-//
-// Licensed under the Simple Public License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// http://opensource.org/licenses/Simple-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package hugolib
-
-var Pager interface {
-	Read()
-	Render()
-	Convert()
-}
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -54,19 +54,11 @@
 	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
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -331,7 +331,7 @@
 
 	files := s.Source.Files()
 
-	results := make(chan pageResult)
+	results := make(chan HandledResult)
 	filechan := make(chan *source.File)
 
 	procs := getGoMaxProcs()
@@ -361,7 +361,7 @@
 
 	readErrs := <-errs
 
-	results = make(chan pageResult)
+	results = make(chan HandledResult)
 	pagechan := make(chan *Page)
 
 	wg = &sync.WaitGroup{}
@@ -397,33 +397,23 @@
 	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 @@
 
 		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 @@
 	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 {