-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
103 lines (93 loc) · 2.29 KB
/
Copy pathmodule.go
File metadata and controls
103 lines (93 loc) · 2.29 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
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
package ui
import (
"fmt"
"github.com/lightningsdk/core"
"github.com/lightningsdk/ui/essentials"
"github.com/lightningsdk/ui/essentials/layout"
"github.com/lightningsdk/ui/parser"
"github.com/lightningsdk/ui/renderer"
"github.com/lightningsdk/ui/standard"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"reflect"
"regexp"
)
const ModuleName = "github.com/lightningsdk/ui"
type Module struct {
core.DefaultModule
*Config
renderer.Service
}
type Config struct {
Framework string `yaml:"framework" default:"bootstrap"`
}
func NewModule(app *core.App) core.Module {
initParsers()
templates := getTemplates()
return &Module{
Service: renderer.New(templates),
}
}
func initParsers() {
// these will parse from html to specific components
for k, v := range map[string]any{
"html": standard.HTML{},
"sections": layout.Sections{},
"template": essentials.Template{},
"div": essentials.Div{},
} {
// TODO: handle this error
_ = parser.AddType(k, reflect.TypeOf(v))
}
}
func getTemplates() map[string]renderer.Component {
c := map[string]renderer.Component{}
r, _ := regexp.Compile(".*ya?ml$")
for _, p := range []string{"./content/module_defaults", "./content/templates"} {
_ = filepath.WalkDir(p,
func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !r.Match([]byte(path)) {
return nil
}
template := parser.RenderParser{}
data, err := os.ReadFile(path)
if err != nil {
fmt.Println(err.Error())
}
name, err := filepath.Rel(p, path)
if err != nil {
fmt.Println(err.Error())
}
err = yaml.Unmarshal(data, &template)
if err != nil {
fmt.Println(err.Error())
}
c[name] = template.Renderer
return nil
})
}
// TODO: return the error if it fails
return c
}
func (m *Module) GetCommands() map[string]core.Command {
return map[string]core.Command{
"build": {
Function: build,
Help: "builds all the tempaltes, css and js files",
},
}
}
func (m *Module) GetEmptyConfig() any {
return &Config{}
}
func (m *Module) SetConfig(cfg any) {
m.Config = cfg.(*Config)
}
func GetRenderer(app *core.App) renderer.Service {
// TODO: if this is nil, it needs to report an error that the UI plugin needs to be added to the config
return app.Modules[ModuleName].(*Module).Service
}