diff --git a/core/builtin.lua b/core/builtin.lua index 02d71e4..b324680 100644 --- a/core/builtin.lua +++ b/core/builtin.lua @@ -121,39 +121,62 @@ function M:custom(str) return self end +function M:g(str) + self:custom(str) + return self +end + function M:h1(str) str = str or "" - table.insert(self.parts, "

" .. str .. "

") + self:custom("

" .. str .. "

") return self end function M:h2(str) str = str or "" - table.insert(self.parts, "

" .. str .. "

") + self:custom("

" .. str .. "

") return self end function M:h3(str) str = str or "" - table.insert(self.parts, "

" .. str .. "

") + self:custom("

" .. str .. "

") return self end function M:h4(str) str = str or "" - table.insert(self.parts, "

" .. str .. "

") + self:custom("

" .. str .. "

") return self end function M:h5(str) str = str or "" - table.insert(self.parts, "
" .. str .. "
") + self:custom("
" .. str .. "
") return self end function M:h6(str) str = str or "" - table.insert(self.parts, "
" .. str .. "
") + self:custom("
" .. str .. "
") + return self +end + +function M:p(str) + str = str or "" + table.insert(self.parts, "

" .. str .. "

") + return self +end + +function M:note(content) + content = content or "" + self:custom('
' .. content .. '
') + return self +end + +function M:a(link, str) + str = str or "" + table.insert(self.parts, "" .. str .. "") return self end diff --git a/core/markdown.lua b/core/markdown.lua new file mode 100644 index 0000000..07da252 --- /dev/null +++ b/core/markdown.lua @@ -0,0 +1,19 @@ +local M = {} + +-- Markdown to HTML conversion function +-- Uses the Go backend markdown parser +function M.to_html(markdown_text) + markdown_text = markdown_text or "" + + -- Get the fes module + local fes_mod = package.loaded.fes + if fes_mod and fes_mod.markdown_to_html then + return fes_mod.markdown_to_html(markdown_text) + end + + -- Fallback: return error message if Go function not available + return "

Error: markdown_to_html function not available

" +end + +return M + diff --git a/core/std.lua b/core/std.lua index e70f604..ed7e403 100644 --- a/core/std.lua +++ b/core/std.lua @@ -16,4 +16,4 @@ function M.site_version() return "" end -return M +return M \ No newline at end of file diff --git a/go.mod b/go.mod index 5630c52..64f6518 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module fes go 1.25.4 require ( - github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 // indirect + github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a // indirect github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/yuin/gopher-lua v1.1.1 // indirect diff --git a/go.sum b/go.sum index dc06d4a..42cadc2 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= +github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a h1:l7A0loSszR5zHd/qK53ZIHMO8b3bBSmENnQ6eKnUT0A= +github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/gomarkdown/mdtohtml v0.0.0-20240124153210-d773061d1585/go.mod h1:6grYm5/uY15CwgBBqwA3+o/cAzaxssckznJ0B35ouBY= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= diff --git a/main.go b/main.go index 87dac4d..936c79e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "flag" "fmt" "net/http" @@ -11,10 +12,22 @@ import ( "regexp" "strings" + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" "github.com/pelletier/go-toml/v2" lua "github.com/yuin/gopher-lua" ) +//go:embed core/builtin.lua +var builtinLua string + +//go:embed core/markdown.lua +var markdownLua string + +//go:embed core/std.lua +var stdLua string + const version = "1.0.0" type MyConfig struct { @@ -31,22 +44,36 @@ type MyConfig struct { var port = flag.Int("p", 3000, "set the server port") +// markdownToHTML converts markdown text to HTML +func markdownToHTML(mdText string) string { + extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock + p := parser.NewWithExtensions(extensions) + doc := p.Parse([]byte(mdText)) + + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + return string(markdown.Render(doc, renderer)) +} + func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) { L := lua.NewState() defer L.Close() L.PreloadModule("fes", func(L *lua.LState) int { mod := L.NewTable() - wd, _ := os.Getwd() - corePath := filepath.Join(wd, "core") - files, _ := os.ReadDir(corePath) - for _, f := range files { - if f.IsDir() || filepath.Ext(f.Name()) != ".lua" { - continue - } - modName := f.Name()[:len(f.Name())-len(".lua")] - if err := L.DoFile(filepath.Join(corePath, f.Name())); err != nil { - fmt.Println("error loading", f.Name(), ":", err) + + // Load core modules from embedded files + coreModules := map[string]string{ + "builtin": builtinLua, + "markdown": markdownLua, + "std": stdLua, + } + + for modName, luaCode := range coreModules { + if err := L.DoString(luaCode); err != nil { + fmt.Println("error loading", modName, ":", err) continue } val := L.Get(-1) @@ -82,6 +109,13 @@ func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) { configTable.RawSetString("fes", fesTable) mod.RawSetString("config", configTable) } + // Register markdown_to_html function + mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int { + mdText := L.ToString(1) + html := markdownToHTML(mdText) + L.Push(lua.LString(html)) + return 1 + })) L.Push(mod) return 1 }) diff --git a/test/Fes.toml b/sam-fan-page/Fes.toml similarity index 80% rename from test/Fes.toml rename to sam-fan-page/Fes.toml index 3698755..870b092 100644 --- a/test/Fes.toml +++ b/sam-fan-page/Fes.toml @@ -1,6 +1,6 @@ [site] -name = "test" +name = "sam-fan-page" version = "0.0.1" authors = ["vx-clutch"] diff --git a/sam-fan-page/www/index.lua b/sam-fan-page/www/index.lua new file mode 100644 index 0000000..ac3a5ba --- /dev/null +++ b/sam-fan-page/www/index.lua @@ -0,0 +1,6 @@ +local fes = require("fes") +local site = fes.site_builder() + +site:h1("Hello, World!") + +return site diff --git a/test/www/index.lua b/test/www/index.lua deleted file mode 100644 index 5f01205..0000000 --- a/test/www/index.lua +++ /dev/null @@ -1,8 +0,0 @@ -local fes = require("fes") -local site = fes.site_builder() - -site:h1("Hello, Sam!") -site:h2(fes.std.fes_version()) -site:h2(fes.std.site_version()) - -return site