alpha
This commit is contained in:
54
main.go
54
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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user