hugo/hugolib/handlers.go

92 lines
2.1 KiB
Go
Raw Normal View History

2014-10-17 16:57:48 -04:00
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// 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
2014-10-20 17:42:16 -04:00
import "github.com/spf13/hugo/source"
type Handler interface {
2014-11-01 12:05:37 -04:00
// Read the Files in and register
2014-10-20 17:51:53 -04:00
Read(*source.File, *Site, HandleResults)
2014-11-01 12:05:37 -04:00
// Convert Pages to prepare for templatizing
// Convert Files to their final destination
2014-10-20 20:15:33 -04:00
Convert(interface{}, *Site, HandleResults)
2014-11-01 12:05:37 -04:00
// Extensions to register the handle for
2014-10-20 17:42:16 -04:00
Extensions() []string
}
type HandledResult struct {
page *Page
file *source.File
err error
}
type HandleResults chan<- HandledResult
2014-10-20 17:51:53 -04:00
type ReadFunc func(*source.File, *Site, HandleResults)
2014-10-20 20:15:33 -04:00
type PageConvertFunc func(*Page, *Site, HandleResults)
type FileConvertFunc ReadFunc
2014-10-20 17:42:16 -04:00
type Handle struct {
2014-10-20 20:15:33 -04:00
extensions []string
read ReadFunc
pageConvert PageConvertFunc
fileConvert FileConvertFunc
2014-10-17 16:57:48 -04:00
}
var handlers []Handler
2014-10-20 17:42:16 -04:00
func (h Handle) Extensions() []string {
return h.extensions
}
2014-10-20 17:51:53 -04:00
func (h Handle) Read(f *source.File, s *Site, results HandleResults) {
2014-10-20 20:15:33 -04:00
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)
}
2014-10-20 17:42:16 -04:00
}
2014-10-17 16:57:48 -04:00
func RegisterHandler(h Handler) {
handlers = append(handlers, h)
}
func Handlers() []Handler {
return handlers
}
2014-10-20 17:42:16 -04:00
func FindHandler(ext string) Handler {
2014-10-17 16:57:48 -04:00
for _, h := range Handlers() {
2014-10-20 17:42:16 -04:00
if HandlerMatch(h, ext) {
2014-10-17 16:57:48 -04:00
return h
}
}
return nil
}
2014-10-20 17:42:16 -04:00
func HandlerMatch(h Handler, ext string) bool {
2014-10-17 16:57:48 -04:00
for _, x := range h.Extensions() {
if ext == x {
return true
}
}
return false
}