1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00
gohugoio-hugo/hugolib/handlers.go

75 lines
1.6 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-10-20 17:51:53 -04:00
Read(*source.File, *Site, HandleResults)
2014-10-20 17:42:16 -04:00
//Render()
//Convert()
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 17:42:16 -04:00
type Handle struct {
extensions []string
readrun ReadFunc
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) {
h.readrun(f, 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
}