This commit is contained in:
2025-11-20 16:57:46 -05:00
parent 442e5c7f12
commit ca435c01a8
5 changed files with 601 additions and 20 deletions

55
main.go
View File

@@ -21,10 +21,8 @@ import (
//go:embed core/builtin.lua
var builtinLua string
//go:embed core/markdown.lua
var markdownLua string
//go:embed core/std.lua
var stdLua string
@@ -44,7 +42,6 @@ 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)
@@ -64,7 +61,6 @@ func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) {
L.PreloadModule("fes", func(L *lua.LState) int {
mod := L.NewTable()
// Load core modules from embedded files
coreModules := map[string]string{
"builtin": builtinLua,
"markdown": markdownLua,
@@ -92,7 +88,7 @@ func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) {
mod.RawSetString(modName, tbl)
}
}
// Pass config to Lua
if cfg != nil {
configTable := L.NewTable()
siteTable := L.NewTable()
@@ -109,7 +105,7 @@ 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)
@@ -198,10 +194,8 @@ CUSTOM_CSS =
}
func fixMalformedToml(content string) string {
// Fix lines like "CUSTOM_CSS =" (with no value) to "CUSTOM_CSS = \"\""
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
return re.ReplaceAllStringFunc(content, func(match string) string {
// Extract the key name
parts := strings.Split(strings.TrimSpace(match), "=")
if len(parts) == 2 && strings.TrimSpace(parts[1]) == "" {
key := strings.TrimSpace(parts[0])
@@ -217,7 +211,6 @@ func startServer(dir string) error {
return err
}
// Fix malformed TOML before parsing
docStr := fixMalformedToml(string(doc))
var cfg MyConfig
@@ -226,15 +219,43 @@ func startServer(dir string) error {
return fmt.Errorf("failed to parse Fes.toml: %w", err)
}
luaPath := filepath.Join(dir, "www", "index.lua")
data, err := loadLua(dir, luaPath, &cfg)
wwwDir := filepath.Join(dir, "www")
entries, err := os.ReadDir(wwwDir)
if err != nil {
return err
return fmt.Errorf("failed to read www directory: %w", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(data))
})
fmt.Printf("App running at:\n - Local: http://localhost:%d/\n", *port)
routes := make(map[string]string)
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lua") {
baseName := strings.TrimSuffix(entry.Name(), ".lua")
luaPath := filepath.Join(wwwDir, entry.Name())
if baseName == "index" {
routes["/"] = luaPath
routes["/index"] = luaPath
} else {
routes["/"+baseName] = luaPath
}
}
}
for route, luaPath := range routes {
func(rt string, lp string) {
http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) {
data, err := loadLua(dir, lp, &cfg)
if err != nil {
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
return
}
w.Write([]byte(data))
})
}(route, luaPath)
}
fmt.Printf("Server is running on http://localhost:%d\n", *port)
return http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}
@@ -242,7 +263,7 @@ func main() {
flag.Parse()
if len(os.Args) < 3 {
fmt.Println("Usage: fes <command> <project_dir>")
fmt.Println("Commands: new, serve")
fmt.Println("Commands: new, run")
os.Exit(1)
}