merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi
About | Files | Commits | Refs | License
refactor: move shared state out of lua.Lua into state.State
Author: pi66
Date: 2026-10-04 23:52
commit: 261d8e10
- Moved the shared `Build`, `Watch`, `Hooks`, `Jinja`, `Extensions/ParsersOption`, and Lua context/abort state from `lua.Lua` into `state.State`. - Updated the build and watch actions to work directly with `state.State` instead of going through Lua. - Moved `RunHook` to `state.State` as a method. - Removed the `core.Core` type alias and now use `*state.State` directly. - Fixed `Handle()` to pass `*error` to its callback, allowing the callback to modify the returned error.
Stat
| M | src/core/build/{main.go => build.go} | 19 | +10 | -9 |
| M | src/core/build/compile/{main.go => compile.go} | 16 | +6 | -10 |
| M | src/core/build/compile/document.go | 2 | +1 | -1 |
| M | src/core/build/compile/helper.go | 16 | +8 | -8 |
| M | src/core/build/compile/jinja/{main.go => jinja.go} | 0 | +0 | -0 |
| M | src/core/build/compile/markdown/{main.go => markdown.go} | 18 | +7 | -11 |
| M | src/core/{main.go => core.go} | 19 | +9 | -10 |
| M | src/core/lua/build.go | 21 | +7 | -14 |
| M | src/core/lua/config.go | 20 | +10 | -10 |
| M | src/core/lua/enable.go | 12 | +6 | -6 |
| M | src/core/lua/hook.go | 31 | +2 | -29 |
| M | src/core/lua/jinja.go | 8 | +4 | -4 |
| A | src/core/lua/lua.go | 62 | +62 | -0 |
| D | src/core/lua/main.go | 70 | +0 | -70 |
| M | src/core/lua/watch.go | 6 | +1 | -5 |
| M | src/core/{ => state}/helper.go | 6 | +3 | -3 |
| D | src/core/state/main.go | 14 | +0 | -14 |
| A | src/core/state/state.go | 85 | +85 | -0 |
| M | src/core/watch/main.go | 13 | +8 | -5 |
| M | src/utils/errs/{main.go => errs.go} | 4 | +2 | -2 |
Diff
diff --git a/src/core/build/main.go b/src/core/build/build.go
similarity index 72%
rename from src/core/build/main.go
rename to src/core/build/build.go
index 70bef37..f961d6d 100644
--- a/src/core/build/main.go
+++ b/src/core/build/build.go
@@ -2,22 +2,19 @@ package build
import (
"io/fs"
- "merodi/src/config"
"merodi/src/core/build/compile"
- . "merodi/src/core/state"
+ "merodi/src/core/state"
. "merodi/src/utils/errs"
)
type Build struct {
- state *State
...
Show all
Show less
diff --git a/src/core/build/main.go b/src/core/build/build.go
similarity index 72%
rename from src/core/build/main.go
rename to src/core/build/build.go
index 70bef37..f961d6d 100644
--- a/src/core/build/main.go
+++ b/src/core/build/build.go
@@ -2,22 +2,19 @@ package build
import (
"io/fs"
- "merodi/src/config"
"merodi/src/core/build/compile"
- . "merodi/src/core/state"
+ "merodi/src/core/state"
. "merodi/src/utils/errs"
)
type Build struct {
- state *State
...
diff --git a/src/core/build/main.go b/src/core/build/build.go
similarity index 72%
rename from src/core/build/main.go
rename to src/core/build/build.go
index 70bef37..f961d6d 100644
--- a/src/core/build/main.go
+++ b/src/core/build/build.go
@@ -2,22 +2,19 @@ package build
import (
"io/fs"
- "merodi/src/config"
"merodi/src/core/build/compile"
- . "merodi/src/core/state"
+ "merodi/src/core/state"
. "merodi/src/utils/errs"
)
type Build struct {
- state *State
- mode config.Mode
+ state *state.State
compile *compile.Compile
}
func (self *Build) InitCompile() {
- self.state.Lua.Build.Mode = self.mode
self.compile = &compile.Compile{State: self.state}
- CheckE(self.compile.Init(self.mode))
+ CheckE(self.compile.Init())
}
func (self *Build) BuildFile(path string) (err error) {
@@ -30,7 +27,12 @@ func (self *Build) BuildFile(path string) (err error) {
}
func (self *Build) Visit(path string, entry fs.DirEntry, err error) (rerr error) {
- defer Handle(&rerr)
+ defer Handle(&rerr, func (err *error) {
+ if *err == ErrAborted {
+ *err = self.state.RunHook("on_abort")
+ }
+ })
+
CheckE(err)
if !self.isSource(entry) {
return
@@ -39,11 +41,10 @@ func (self *Build) Visit(path string, entry fs.DirEntry, err error) (rerr error)
return
}
-func (self *Build) Run(state *State, args *[]string) (err error) {
+func (self *Build) Run(state *state.State, args *[]string) (err error) {
defer Handle(&err)
self.state = state
- self.mode = state.BuildMode
self.InitCompile()
CheckE(self.walkAndBuild())
diff --git a/src/core/build/compile/main.go b/src/core/build/compile/compile.go
similarity index 72%
rename from src/core/build/compile/main.go
rename to src/core/build/compile/compile.go
index f854690..0ac7811 100644
--- a/src/core/build/compile/main.go
+++ b/src/core/build/compile/compile.go
@@ -1,26 +1,22 @@
package compile
import (
- "merodi/src/config"
. "merodi/src/core/build/compile/jinja"
. "merodi/src/core/build/compile/markdown"
- L "merodi/src/core/lua"
"merodi/src/core/state"
. "merodi/src/utils/errs"
)
type Compile struct {
- State *state.State
- Lua *L.Lua
- Jinja JinjaEngine
- MD Markdown
+ State *state.State
+ Jinja JinjaEngine
+ MD Markdown
+ Build state.Build
}
-func (self *Compile) Init(mode config.Mode) (err error) {
+func (self *Compile) Init() (err error) {
defer Handle(&err)
- self.Lua = self.State.Lua
-
self.Jinja = JinjaEngine{Title: self.State.Config.Project.Description}
CheckE(self.Jinja.Init(self.State.Config.Tree.Templates))
@@ -40,5 +36,5 @@ func (self *Compile) Run(mdfile string) (doc string, err error) {
self.convertToDoc()
self.hook("before_write")
- return self.Lua.Build.Content, nil
+ return self.State.Build.Content, nil
}
diff --git a/src/core/build/compile/document.go b/src/core/build/compile/document.go
index 4752844..a387b34 100644
--- a/src/core/build/compile/document.go
+++ b/src/core/build/compile/document.go
@@ -10,7 +10,7 @@ import (
)
func (self *Compile) destRoot() string {
- if self.State.BuildMode == config.Release {
+ if self.State.Build.Mode == config.Release {
return self.State.Config.Tree.ReleaseDest
}
return self.State.Config.Tree.DraftDest
diff --git a/src/core/build/compile/helper.go b/src/core/build/compile/helper.go
index 12a0f13..1a73da3 100644
--- a/src/core/build/compile/helper.go
+++ b/src/core/build/compile/helper.go
@@ -6,39 +6,39 @@ import (
)
func (self *Compile) hook(name string) {
- CheckE(self.Lua.RunHook(name))
+ CheckE(self.State.RunHook(name))
}
func (self *Compile) resolvePaths(mdfile string) {
- build := self.Lua.Build
+ build := self.State.Build
mdPath, docPath, err := self.ResolveDocPaths(mdfile)
CheckE(err)
build.MDPath, build.DocPath = mdPath, docPath
}
func (self *Compile) readMarkdown() {
- build := self.Lua.Build
+ build := self.State.Build
self.hook("before_read")
build.Content = CheckV(self.MD.ReadMD(build.MDPath))
self.hook("read_md")
}
func (self *Compile) convertToDoc() {
- build := self.Lua.Build
- self.MD.Page = self.Jinja.Page
+ build := self.State.Build
+ self.MD.Page = *self.Jinja.Page
build.Content = CheckV(self.MD.MdToDoc(build.Content))
self.hook("doc_content")
}
func (self *Compile) applyJinja() {
- build := self.Lua.Build
- self.Jinja.Data = self.State.Lua.Jinja
+ build := self.State.Build
+ self.Jinja.Data = self.State.Jinja
CheckE(self.Jinja.JinjaHandler(&build.Content))
self.hook("apply_doc_jinja")
}
func (self *Compile) reset() {
- build := self.Lua.Build
+ build := self.State.Build
build.Content = ""
build.DocPath = ""
build.MDPath = ""
diff --git a/src/core/build/compile/jinja/main.go b/src/core/build/compile/jinja/jinja.go
similarity index 100%
rename from src/core/build/compile/jinja/main.go
rename to src/core/build/compile/jinja/jinja.go
diff --git a/src/core/build/compile/markdown/main.go b/src/core/build/compile/markdown/markdown.go
similarity index 84%
rename from src/core/build/compile/markdown/main.go
rename to src/core/build/compile/markdown/markdown.go
index c1a696b..3275bdf 100644
--- a/src/core/build/compile/markdown/main.go
+++ b/src/core/build/compile/markdown/markdown.go
@@ -3,12 +3,10 @@ package markdown
import (
"bytes"
"merodi/src/core/build/compile/jinja"
- . "merodi/src/core/state"
+ "merodi/src/core/state"
. "merodi/src/utils/errs"
"os"
- L "merodi/src/core/lua"
-
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
@@ -17,15 +15,13 @@ import (
)
type Markdown struct {
- Extensions []goldmark.Extender
- ParsersOption []parser.Option
- Build L.Build
- Page *jinja.Page
+ *state.Markdown
+ state.State
}
-func (self *Markdown) Init(state *State) {
- self.Extensions = state.Lua.Extensions
- self.ParsersOption = state.Lua.ParsersOption
+func (self *Markdown) Init(state *state.State) {
+ self.Extensions = state.Markdown.Extensions
+ self.ParsersOption = state.Markdown.ParsersOption
}
func (self *Markdown) ReadMD(mdfile string) (content string, err error) {
@@ -52,7 +48,7 @@ func (self *Markdown) MdToDoc(mdcontent string) (doc string, err error) {
CheckE(gm.Renderer().Render(&raw_doc, mdBytes, node))
body := raw_doc.String()
- if self.Page == nil || !self.Page.Set {
+ if !self.Page.Set {
return body, nil
}
diff --git a/src/core/main.go b/src/core/core.go
similarity index 54%
rename from src/core/main.go
rename to src/core/core.go
index be6c438..70a4c62 100644
--- a/src/core/main.go
+++ b/src/core/core.go
@@ -3,15 +3,14 @@ package core
import (
"merodi/src/core/build"
"merodi/src/core/lua"
- "merodi/src/core/state"
+ . "merodi/src/core/state"
"merodi/src/core/watch"
"merodi/src/utils"
. "merodi/src/utils/errs"
)
-type Core state.State
type Action interface {
- Run(state *state.State, args *[]string) error
+ Run(state *State, args *[]string) error
}
var Actions = map[string]Action{
@@ -21,19 +20,19 @@ var Actions = map[string]Action{
func Run(act string, args *[]string) (err error) {
defer Handle(&err)
- self := Core{Lua: &lua.Lua{}}
+ state := &State{}
+
action, ok := Actions[act]
if !ok {
utils.PrintHelp()
return
}
- self.BuildMode = build.SetBuildMode(args)
- CheckE(self.GoToProjectDir(args))
- CheckE(self.LoadConfig())
- CheckE(self.Lua.InitLua(&self.Config))
- defer self.Lua.Context.Close()
+ state.Build.Mode = build.SetBuildMode(args)
+ CheckE(state.GoToProjectDir(args))
+ CheckE(state.LoadConfig())
+ CheckE(lua.Init(state))
+ CheckE(action.Run(state, args))
- CheckE(action.Run((*state.State)(&self), args))
return nil
}
diff --git a/src/core/lua/build.go b/src/core/lua/build.go
index 9afa3f4..e7341ce 100644
--- a/src/core/lua/build.go
+++ b/src/core/lua/build.go
@@ -5,31 +5,24 @@ import (
"merodi/src/config"
)
-type Build struct {
- MDPath string
- DocPath string
- Content string
- Mode config.Mode
-}
-
func (self *Lua) registerBuild() {
dataTable := self.Context.NewTable()
dataTable.RawSetString("mode", self.Context.NewFunction(func(L *glua.LState) int {
- L.Push(glua.LString(config.ModeToString(self.Build.Mode)))
+ L.Push(glua.LString(config.ModeToString(self.State.Build.Mode)))
return 1
}))
dataTable.RawSetString("content", self.makeGetSetTable(
- func() string { return self.Build.Content },
- func(v string) { self.Build.Content = v },
+ func() string { return self.State.Build.Content },
+ func(v string) { self.State.Build.Content = v },
))
dataTable.RawSetString("md_path", self.makeGetSetTable(
- func() string { return self.Build.MDPath },
- func(v string) { self.Build.MDPath = v },
+ func() string { return self.State.Build.MDPath },
+ func(v string) { self.State.Build.MDPath = v },
))
dataTable.RawSetString("document_path", self.makeGetSetTable(
- func() string { return self.Build.DocPath },
- func(v string) { self.Build.DocPath = v },
+ func() string { return self.State.Build.DocPath },
+ func(v string) { self.State.Build.DocPath = v },
))
self.Merodi.RawSetString("build", dataTable)
diff --git a/src/core/lua/config.go b/src/core/lua/config.go
index 4402970..5cc2e74 100644
--- a/src/core/lua/config.go
+++ b/src/core/lua/config.go
@@ -5,24 +5,24 @@ import (
)
func (self *Lua) registerConfigTree() *lua.LTable {
- tree := self.Context.NewTable()
- tree.RawSetString("markdown", lua.LString(self.Config.Tree.Markdown))
- tree.RawSetString("templates", lua.LString(self.Config.Tree.Templates))
- tree.RawSetString("draft_dest", lua.LString(self.Config.Tree.DraftDest))
- tree.RawSetString("release_dest", lua.LString(self.Config.Tree.ReleaseDest))
+ tree := self.State.Lua.Context.NewTable()
+ tree.RawSetString("markdown", lua.LString(self.State.Config.Tree.Markdown))
+ tree.RawSetString("templates", lua.LString(self.State.Config.Tree.Templates))
+ tree.RawSetString("draft_dest", lua.LString(self.State.Config.Tree.DraftDest))
+ tree.RawSetString("release_dest", lua.LString(self.State.Config.Tree.ReleaseDest))
return tree
}
func (self *Lua) registerConfigProject() *lua.LTable {
- projectTable := self.Context.NewTable()
- projectTable.RawSetString("name", lua.LString(self.Config.Project.Name))
- projectTable.RawSetString("version", lua.LString(self.Config.Project.Version))
- projectTable.RawSetString("description", lua.LString(self.Config.Project.Description))
+ projectTable := self.State.Lua.Context.NewTable()
+ projectTable.RawSetString("name", lua.LString(self.State.Config.Project.Name))
+ projectTable.RawSetString("version", lua.LString(self.State.Config.Project.Version))
+ projectTable.RawSetString("description", lua.LString(self.State.Config.Project.Description))
return projectTable
}
func (self *Lua) registerConfig() {
- configTable := self.Context.NewTable()
+ configTable := self.State.Lua.Context.NewTable()
configTable.RawSetString("tree", self.registerConfigTree())
configTable.RawSetString("project", self.registerConfigProject())
diff --git a/src/core/lua/enable.go b/src/core/lua/enable.go
index f737f17..d967fb1 100644
--- a/src/core/lua/enable.go
+++ b/src/core/lua/enable.go
@@ -57,18 +57,18 @@ func (self *Lua) registerEnable() {
fn := self.Context.NewFunction(func(L *glua.LState) int {
if ext, ok := extensionsMap[name]; ok {
- self.Extensions = append(self.Extensions, ext)
+ self.State.Markdown.Extensions = append(self.State.Markdown.Extensions, ext)
return 0
}
if option, ok := parsersMap[name]; ok {
- self.ParsersOption = append(self.ParsersOption, option)
+ self.State.Markdown.ParsersOption = append(self.State.Markdown.ParsersOption, option)
return 0
}
if p, ok := inlineParsersMap[name]; ok {
- self.ParsersOption = append(
- self.ParsersOption,
+ self.State.Markdown.ParsersOption = append(
+ self.State.Markdown.ParsersOption,
parser.WithInlineParsers(
util.Prioritized(p, 100),
),
@@ -77,8 +77,8 @@ func (self *Lua) registerEnable() {
}
if p, ok := blockParsersMap[name]; ok {
- self.ParsersOption = append(
- self.ParsersOption,
+ self.State.Markdown.ParsersOption = append(
+ self.State.Markdown.ParsersOption,
parser.WithBlockParsers(
util.Prioritized(p, 100),
),
diff --git a/src/core/lua/hook.go b/src/core/lua/hook.go
index e0b919b..7c5e648 100644
--- a/src/core/lua/hook.go
+++ b/src/core/lua/hook.go
@@ -10,13 +10,13 @@ import (
func (self *Lua) registerHook(L *lua.LState) int {
stage := L.ToString(1)
fn := L.ToFunction(2)
- self.Hooks[stage] = fn
+ self.State.Hooks[stage] = fn
return 0
}
func (self *Lua) luaAbort(L *lua.LState) int {
log.Warn(L.ToString(1))
- self.Aborted = true
+ self.State.Lua.Aborted = true
panic(ErrAborted)
}
@@ -26,30 +26,3 @@ func (self *Lua) registerHooks() {
"abort": self.luaAbort,
})
}
-
-func (self *Lua) RunHook(stage string, args ...string) (err error) {
- defer Handle(&err)
- fn, ok := self.Hooks[stage]
- if !ok {
- return
- }
-
- self.Aborted = false
-
- luaArgs := make([]lua.LValue, len(args))
- for i, arg := range args {
- luaArgs[i] = lua.LString(arg)
- }
-
- cerr := self.Context.CallByParam(lua.P{
- Fn: fn,
- NRet: 0,
- Protect: true,
- }, luaArgs...)
-
- if self.Aborted {
- CheckE(ErrAborted)
- }
- CheckE(cerr)
- return
-}
diff --git a/src/core/lua/jinja.go b/src/core/lua/jinja.go
index 60a334a..097661b 100644
--- a/src/core/lua/jinja.go
+++ b/src/core/lua/jinja.go
@@ -5,8 +5,8 @@ import (
)
func (self *Lua) registerJinja() {
- jinja := self.Context.NewTable()
- self.Context.SetFuncs(jinja, map[string]lua.LGFunction{
+ jinja := self.State.Lua.Context.NewTable()
+ self.State.Lua.Context.SetFuncs(jinja, map[string]lua.LGFunction{
"set": self.luaJinjaSet,
"get": self.luaJinjaGet,
})
@@ -16,12 +16,12 @@ func (self *Lua) registerJinja() {
func (self *Lua) luaJinjaSet(L *lua.LState) int {
key := L.ToString(1)
value := gluaToGo(L.Get(2))
- self.Jinja[key] = value
+ self.State.Jinja[key] = value
return 0
}
func (self *Lua) luaJinjaGet(L *lua.LState) int {
key := L.ToString(1)
- L.Push(goToGlua(self.Jinja[key]))
+ L.Push(goToGlua(self.State.Jinja[key]))
return 1
}
diff --git a/src/core/lua/lua.go b/src/core/lua/lua.go
new file mode 100644
index 0000000..8361deb
--- /dev/null
+++ b/src/core/lua/lua.go
@@ -0,0 +1,62 @@
+package lua
+
+import (
+ "merodi/src/core/state"
+ . "merodi/src/utils/errs"
+ "path/filepath"
+
+ glua "github.com/yuin/gopher-lua"
+)
+
+type Lua struct {
+ State *state.State
+ Context *glua.LState
+ Merodi *glua.LTable
+}
+
+func Init(st *state.State) (err error) {
+ defer Handle(&err)
+
+ st.Lua.Context = glua.NewState()
+
+ self := Lua{
+ State: st,
+ Context: st.Lua.Context,
+ }
+ defer self.Context.Close()
+
+ self.State.Lua.Aborted = false
+
+ self.registerMerodi()
+
+ path := filepath.Join(self.State.Config.Tree.Plugins, "main.lua")
+ CheckE(self.loadFile(path))
+ return
+}
+
+func (self *Lua) loadFile(path string) (err error) {
+ defer Handle(&err)
+ fn, lerr := self.State.Lua.Context.LoadFile(path)
+ if lerr != nil {
+ CheckE(describeLuaError(path, lerr))
+ }
+
+ self.State.Lua.Context.Push(fn)
+ if perr := self.State.Lua.Context.PCall(0, glua.MultRet, nil); perr != nil {
+ CheckE(describeLuaError(path, perr))
+ }
+ return
+}
+
+func (self *Lua) registerMerodi() {
+ self.Merodi = self.State.Lua.Context.NewTable()
+ self.registerHooks()
+ self.registerConfig()
+ self.registerJinja()
+ self.registerBuild()
+ self.registerWatch()
+ self.registerEnable()
+ self.registerDisable()
+ self.registerLog()
+ self.State.Lua.Context.SetGlobal("merodi", self.Merodi)
+}
diff --git a/src/core/lua/main.go b/src/core/lua/main.go
deleted file mode 100644
index dfd99c9..0000000
--- a/src/core/lua/main.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package lua
-
-import (
- "merodi/src/config"
- . "merodi/src/utils/errs"
- "path/filepath"
-
- "github.com/yuin/goldmark"
- "github.com/yuin/goldmark/parser"
- glua "github.com/yuin/gopher-lua"
-)
-
-type Lua struct {
- Context *glua.LState
- Config *config.Data
-
- Hooks map[string]*glua.LFunction
- Jinja map[string]any
- Build *Build
- Watch *Watcher
-
- Extensions []goldmark.Extender
- ParsersOption []parser.Option
-
- Merodi *glua.LTable
- Aborted bool
-}
-
-func (self *Lua) InitLua(cfg *config.Data) (err error) {
- defer Handle(&err)
- self.Config = cfg
- self.Hooks = make(map[string]*glua.LFunction)
- self.Jinja = make(map[string]any)
- self.Context = glua.NewState()
- self.Watch = &Watcher{}
- self.Build = &Build{}
- self.Aborted = false
-
- self.registerMerodi()
-
- path := filepath.Join(cfg.Tree.Plugins, "main.lua")
- CheckE(self.loadFile(path))
- return
-}
-
-func (self *Lua) loadFile(path string) (err error) {
- defer Handle(&err)
- fn, lerr := self.Context.LoadFile(path)
- if lerr != nil {
- CheckE(describeLuaError(path, lerr))
- }
-
- self.Context.Push(fn)
- if perr := self.Context.PCall(0, glua.MultRet, nil); perr != nil {
- CheckE(describeLuaError(path, perr))
- }
- return
-}
-func (self *Lua) registerMerodi() {
- self.Merodi = self.Context.NewTable()
- self.registerHooks()
- self.registerConfig()
- self.registerJinja()
- self.registerBuild()
- self.registerWatch()
- self.registerEnable()
- self.registerDisable()
- self.registerLog()
- self.Context.SetGlobal("merodi", self.Merodi)
-}
diff --git a/src/core/lua/watch.go b/src/core/lua/watch.go
index 1c22966..4e98c30 100644
--- a/src/core/lua/watch.go
+++ b/src/core/lua/watch.go
@@ -4,14 +4,10 @@ import (
glua "github.com/yuin/gopher-lua"
)
-type Watcher struct {
- Add []string
-}
-
func (self *Lua) registerWatch() {
watchTable := self.Context.NewTable()
watchTable.RawSetString("add", self.Context.NewFunction(func(l *glua.LState) int {
- self.Watch.Add = append(self.Watch.Add, l.ToString(1))
+ self.State.Watch.Add = append(self.State.Watch.Add, l.ToString(1))
return 0
}))
self.Merodi.RawSetString("watch", watchTable)
diff --git a/src/core/helper.go b/src/core/state/helper.go
similarity index 73%
rename from src/core/helper.go
rename to src/core/state/helper.go
index b558c57..9f32753 100644
--- a/src/core/helper.go
+++ b/src/core/state/helper.go
@@ -1,4 +1,4 @@
-package core
+package state
import (
"merodi/src/config"
@@ -7,7 +7,7 @@ import (
"os"
)
-func (self *Core) GoToProjectDir(args *[]string) (err error) {
+func (self *State) GoToProjectDir(args *[]string) (err error) {
defer Handle(&err)
dir := CheckV(project.SetProjectDir(args))
self.ProjectDir = dir
@@ -15,7 +15,7 @@ func (self *Core) GoToProjectDir(args *[]string) (err error) {
return
}
-func (self *Core) LoadConfig() (err error) {
+func (self *State) LoadConfig() (err error) {
defer Handle(&err)
var cfg config.Config
cfg.ProjectDir = self.ProjectDir
diff --git a/src/core/state/main.go b/src/core/state/main.go
deleted file mode 100644
index 2eb82e6..0000000
--- a/src/core/state/main.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package state
-
-import (
- "merodi/src/config"
- . "merodi/src/config"
- "merodi/src/core/lua"
-)
-
-type State struct {
- ProjectDir string
- Config Data
- BuildMode config.Mode
- Lua *lua.Lua
-}
diff --git a/src/core/state/state.go b/src/core/state/state.go
new file mode 100644
index 0000000..8a76531
--- /dev/null
+++ b/src/core/state/state.go
@@ -0,0 +1,85 @@
+package state
+
+import (
+ "merodi/src/config"
+ . "merodi/src/config"
+ "merodi/src/core/build/compile/jinja"
+ . "merodi/src/utils/errs"
+
+ "github.com/yuin/goldmark"
+ "github.com/yuin/goldmark/parser"
+ glua "github.com/yuin/gopher-lua"
+)
+
+type State struct {
+ ProjectDir string
+ Config Data
+ Build Build
+ Jinja map[string]any
+ Hooks map[string]*glua.LFunction
+ Lua Lua
+ Markdown Markdown
+ Watch Watcher
+}
+
+type Lua struct {
+ Context *glua.LState
+ Aborted bool
+}
+
+
+type Build struct {
+ MDPath string
+ DocPath string
+ Content string
+ Mode config.Mode
+}
+
+type Markdown struct {
+ Extensions []goldmark.Extender
+ ParsersOption []parser.Option
+ Page jinja.Page
+}
+
+type Watcher struct {
+ Add []string
+}
+
+func InitGlobals() State {
+ return State{
+ ProjectDir: "",
+ Config: Data{},
+ Jinja: make(map[string]any),
+ Build: Build{},
+ Watch: Watcher{},
+ Hooks: make(map[string]*glua.LFunction),
+ Lua: Lua{},
+ }
+}
+
+func (s *State) RunHook(stage string, args ...string) (err error) {
+ defer Handle(&err)
+ fn, ok := s.Hooks[stage]
+ if !ok {
+ return
+ }
+
+ s.Lua.Aborted = false
+
+ luaArgs := make([]glua.LValue, len(args))
+ for i, arg := range args {
+ luaArgs[i] = glua.LString(arg)
+ }
+
+ cerr := s.Lua.Context.CallByParam(glua.P{
+ Fn: fn,
+ NRet: 0,
+ Protect: true,
+ }, luaArgs...)
+
+ if s.Lua.Aborted {
+ CheckE(ErrAborted)
+ }
+ CheckE(cerr)
+ return
+}
diff --git a/src/core/watch/main.go b/src/core/watch/main.go
index 1abd867..7bbd4cd 100644
--- a/src/core/watch/main.go
+++ b/src/core/watch/main.go
@@ -10,18 +10,21 @@ import (
"github.com/fsnotify/fsnotify"
)
-type Watcher struct{}
+type Watcher struct{
+ State *state.State
+}
func (self *Watcher) Run(state *state.State, args *[]string) (err error) {
defer Handle(&err)
+ self.State = state
watcher := CheckV(fsnotify.NewWatcher())
defer watcher.Close()
- if len(state.Lua.Watch.Add) == 0 {
+ if len(state.Watch.Add) == 0 {
CheckE(watcher.Add(state.Config.Tree.Markdown))
} else {
- for _, file := range state.Lua.Watch.Add {
+ for _, file := range self.State.Watch.Add {
_, statErr := os.Stat(file)
if os.IsNotExist(statErr) {
CheckE(statErr)
@@ -30,7 +33,7 @@ func (self *Watcher) Run(state *state.State, args *[]string) (err error) {
}
}
- CheckE(state.Lua.RunHook("on_start_watching"))
+ CheckE(self.State.RunHook("on_start_watching"))
lastEvents := make(map[string]time.Time)
const debounce = 100 * time.Millisecond
@@ -48,7 +51,7 @@ func (self *Watcher) Run(state *state.State, args *[]string) (err error) {
}
lastEvents[event.Name] = now
- CheckE(state.Lua.RunHook("on_file_changed", event.Op.String(), event.Name))
+ CheckE(state.RunHook("on_file_changed", event.Op.String(), event.Name))
case werr, ok := <-watcher.Errors:
if !ok {
diff --git a/src/utils/errs/main.go b/src/utils/errs/errs.go
similarity index 85%
rename from src/utils/errs/main.go
rename to src/utils/errs/errs.go
index dc4bdc3..7c15063 100644
--- a/src/utils/errs/main.go
+++ b/src/utils/errs/errs.go
@@ -6,7 +6,7 @@ import (
var ErrAborted = errors.New("build aborted")
-func Handle(err *error, fn ...func(error)) {
+func Handle(err *error, fn ...func(*error)) {
if r := recover(); r != nil {
e, ok := r.(error)
if !ok {
@@ -14,7 +14,7 @@ func Handle(err *error, fn ...func(error)) {
}
*err = e
if len(fn) > 0 && fn[0] != nil {
- fn[0](e)
+ fn[0](err)
}
}
}