This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththeme.go
137 lines (114 loc) · 3 KB
/
theme.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package app
import (
"errors"
"html/template"
"os"
"path/filepath"
"reflect"
"strings"
)
type ConfigureQorThemeInterface interface {
ConfigureQorTheme(ThemeInterface)
}
type ThemeInterface interface {
Initialize(ThemeInterface)
GetName() string
GetPath() string
GetTemplatesPath() string
CopyFiles(ThemeInterface) error
Build(ThemeInterface) error
GetApplication() *Application
SetApplication(*Application)
UsePlugin(PluginInterface)
GetPlugins() []PluginInterface
GetPlugin(name string) PluginInterface
FuncMap() template.FuncMap
}
type Theme struct {
Name string
Path string
TemplatesPath string
Application *Application
Plugins []PluginInterface
}
func (theme *Theme) Initialize(t ThemeInterface) {
if t.GetName() == "" {
theme.Name = reflect.ValueOf(t).Elem().Type().Name()
}
if t.GetTemplatesPath() == "" {
theme.TemplatesPath = filepath.Join(reflect.ValueOf(t).Elem().Type().PkgPath(), "templates")
}
}
func (theme *Theme) GetName() string {
return theme.Name
}
func (theme *Theme) GetPath() string {
return theme.Path
}
func (theme *Theme) GetTemplatesPath() string {
if theme.TemplatesPath != "" {
if pth, ok := isExistingDir(filepath.Join(root, "vendor", theme.TemplatesPath)); ok {
return pth
}
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
if pth, ok := isExistingDir(filepath.Join(gopath, "src", theme.TemplatesPath)); ok {
return pth
}
}
}
return theme.TemplatesPath
}
// Patch model, functions (golang, java, swift)
func (*Theme) CopyFiles(theme ThemeInterface) error {
return copyFiles(theme.GetTemplatesPath(), theme.GetPath(), theme.FuncMap(), theme)
}
func (*Theme) Build(theme ThemeInterface) error {
return errors.New("Build not implemented for Theme " + theme.GetName())
}
// Theme Application
func (theme *Theme) GetApplication() *Application {
return theme.Application
}
func (theme *Theme) SetApplication(app *Application) {
theme.Application = app
}
// Theme Plugins
func (theme *Theme) UsePlugin(plugin PluginInterface) {
plugin.SetTheme(theme)
plugin.Initialize(plugin)
if configor, ok := plugin.(ConfigureQorPluginInterface); ok {
configor.ConfigureQorPlugin(plugin)
}
theme.Plugins = append(theme.Plugins, plugin)
}
func (theme *Theme) GetPlugins() []PluginInterface {
return theme.Plugins
}
func (theme *Theme) GetPlugin(name string) PluginInterface {
for _, plugin := range theme.Plugins {
if name == plugin.GetName() {
return plugin
}
}
return nil
}
// FuncMap
func (theme *Theme) FuncMap() template.FuncMap {
funcMap := theme.GetApplication().FuncMap()
funcMap["has_plugin"] = func(name string) bool {
for _, plugin := range theme.GetPlugins() {
if plugin.GetName() == name {
return true
}
}
return false
}
funcMap["package_path"] = func() string {
var pkgPath, _ = filepath.Abs(root)
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
pkgPath = strings.TrimPrefix(pkgPath, filepath.Join(gopath, "src")+"/")
}
return pkgPath
}
return funcMap
}