merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi
About | Files | Commits | Refs | License
feat: add merodi.action() to expose the current command
Author: pi66
Date: 2026-10-10 00:02
commit: d5c094b2
Stat
| A | examples/plugins/hooks.lua | 2 | +2 | -0 |
| M | src/core/core.go | 7 | +3 | -4 |
| A | src/core/lua/action.go | 10 | +10 | -0 |
| A | src/core/lua/lua.go | 1 | +1 | -0 |
| M | src/core/state/state.go | 7 | +5 | -2 |
Diff
diff --git a/examples/plugins/hooks.lua b/examples/plugins/hooks.lua
index 66fcf52..3a855d4 100644
--- a/examples/plugins/hooks.lua
+++ b/examples/plugins/hooks.lua
@@ -1,6 +1,8 @@
-- Hooks let your plugin run code at different stages of the build.
-- Hook functions don't take arguments or return anything. Use merodi.build.* to
+merodi.action() -- Returns the current command as a string: "build", "watch", "clean", or "hook".
+
merodi.hook("before_read", function() end) -- runs before the Markdown file is read
merodi.hook("read_md", function() end) -- runs after the raw Markdown has been read
merodi.hook("html_content", function() end) -- runs after the Markdown is converted to HTML
diff --git a/src/core/core.go b/src/core/core.go
index d3d9123..e52fb3c 100644
--- a/src/core/core.go
+++ b/src/core/core.go
@@ -25,17 +25,16 @@ var Actions = map[parser.Action]Action{
func Run(opt *parser.Option) (err error) {
defer Handle(&err)
...
Show all
Show less
diff --git a/examples/plugins/hooks.lua b/examples/plugins/hooks.lua
index 66fcf52..3a855d4 100644
--- a/examples/plugins/hooks.lua
+++ b/examples/plugins/hooks.lua
@@ -1,6 +1,8 @@
-- Hooks let your plugin run code at different stages of the build.
-- Hook functions don't take arguments or return anything. Use merodi.build.* to
+merodi.action() -- Returns the current command as a string: "build", "watch", "clean", or "hook".
+
merodi.hook("before_read", function() end) -- runs before the Markdown file is read
merodi.hook("read_md", function() end) -- runs after the raw Markdown has been read
merodi.hook("html_content", function() end) -- runs after the Markdown is converted to HTML
diff --git a/src/core/core.go b/src/core/core.go
index d3d9123..e52fb3c 100644
--- a/src/core/core.go
+++ b/src/core/core.go
@@ -25,17 +25,16 @@ var Actions = map[parser.Action]Action{
func Run(opt *parser.Option) (err error) {
defer Handle(&err)
...
diff --git a/examples/plugins/hooks.lua b/examples/plugins/hooks.lua
index 66fcf52..3a855d4 100644
--- a/examples/plugins/hooks.lua
+++ b/examples/plugins/hooks.lua
@@ -1,6 +1,8 @@
-- Hooks let your plugin run code at different stages of the build.
-- Hook functions don't take arguments or return anything. Use merodi.build.* to
+merodi.action() -- Returns the current command as a string: "build", "watch", "clean", or "hook".
+
merodi.hook("before_read", function() end) -- runs before the Markdown file is read
merodi.hook("read_md", function() end) -- runs after the raw Markdown has been read
merodi.hook("html_content", function() end) -- runs after the Markdown is converted to HTML
diff --git a/src/core/core.go b/src/core/core.go
index d3d9123..e52fb3c 100644
--- a/src/core/core.go
+++ b/src/core/core.go
@@ -25,17 +25,16 @@ var Actions = map[parser.Action]Action{
func Run(opt *parser.Option) (err error) {
defer Handle(&err)
state := InitGlobals()
-
- action := Actions[opt.Action]
-
+ state.Action = opt.Action
state.Build.Mode = opt.Mode
+
CheckE(state.GoToProjectDir(opt.ProjectDir))
CheckE(state.LoadConfig())
if opt.Action != parser.Clean {
CheckE(lua.Init(&state))
defer state.Lua.Context.Close()
}
- CheckE(action.Run(&state, opt))
+ CheckE(Actions[opt.Action].Run(&state, opt))
return nil
}
diff --git a/src/core/lua/action.go b/src/core/lua/action.go
new file mode 100644
index 0000000..c291018
--- /dev/null
+++ b/src/core/lua/action.go
@@ -0,0 +1,10 @@
+package lua
+
+import glua "github.com/yuin/gopher-lua"
+
+func (self *Lua) registerAction() {
+ self.Merodi.RawSetString("action", self.Context.NewFunction(func(L *glua.LState) int {
+ L.Push(glua.LString(string(self.State.Action)))
+ return 1
+ }))
+}
diff --git a/src/core/lua/lua.go b/src/core/lua/lua.go
index 99b5d0d..c3847bc 100644
--- a/src/core/lua/lua.go
+++ b/src/core/lua/lua.go
@@ -58,5 +58,6 @@ func (self *Lua) registerMerodi() {
self.registerDisable()
self.registerLog()
self.registerCompile()
+ self.registerAction()
self.State.Lua.Context.SetGlobal("merodi", self.Merodi)
}
diff --git a/src/core/state/state.go b/src/core/state/state.go
index 8c41bcc..dca4bad 100644
--- a/src/core/state/state.go
+++ b/src/core/state/state.go
@@ -4,10 +4,11 @@ import (
"merodi/src/config"
. "merodi/src/config"
"merodi/src/core/build/compile/jinja"
+ "merodi/src/parser"
. "merodi/src/utils/errs"
"github.com/yuin/goldmark"
- "github.com/yuin/goldmark/parser"
+ gparser "github.com/yuin/goldmark/parser"
glua "github.com/yuin/gopher-lua"
)
@@ -20,6 +21,7 @@ type State struct {
Lua Lua
Markdown Markdown
Watch Watcher
+ Action parser.Action
}
type Lua struct {
@@ -36,7 +38,7 @@ type Build struct {
type Markdown struct {
Extensions []goldmark.Extender
- ParsersOption []parser.Option
+ ParsersOption []gparser.Option
Page jinja.Page
}
@@ -53,6 +55,7 @@ func InitGlobals() State {
Watch: Watcher{},
Hooks: make(map[string]*glua.LFunction),
Lua: Lua{},
+ Action: parser.Help,
}
}