-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.go
More file actions
62 lines (49 loc) · 1.3 KB
/
plugins.go
File metadata and controls
62 lines (49 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package webx
import "path/filepath"
var plugins = []*Plugin{}
var pluginPaths = map[string][]string{}
type Plugin struct {
name string
config map[string]string
compile []func(config *Config)
router []func(app App)
pages map[string][]byte
assets map[string][]byte
}
func NewPlugin(name string, defConfig ...map[string]string) *Plugin {
if len(defConfig) == 0 {
defConfig = append(defConfig, map[string]string{})
}
plugin := &Plugin{
name: name,
config: defConfig[0],
compile: []func(config *Config){},
router: []func(app App){},
pages: map[string][]byte{},
assets: map[string][]byte{},
}
plugins = append(plugins, plugin)
return plugin
}
func (plugin *Plugin) Compile(cb func(config *Config)) {
plugin.compile = append(plugin.compile, cb)
}
func (plugin *Plugin) Router(cb func(app App)) {
plugin.router = append(plugin.router, cb)
}
func (plugin *Plugin) Page(name string, buf []byte, path ...string) {
plugin.pages[name] = buf
if len(path) != 0 {
if p, err := filepath.Abs(path[0]); err == nil {
pluginPaths[p] = []string{name, plugin.name}
}
}
}
func (plugin *Plugin) Asset(name string, buf []byte, path ...string) {
plugin.assets[name] = buf
if len(path) != 0 {
if p, err := filepath.Abs(path[0]); err == nil {
pluginPaths[p] = []string{name, plugin.name}
}
}
}