pre refactor

This commit is contained in:
2025-11-24 15:48:46 -05:00
parent 84b4487d92
commit e2d31aa734
7 changed files with 71 additions and 6 deletions

1
TODO
View File

@@ -2,3 +2,4 @@ Make this static ( cannot find core.std everywhere )
Add hotreloading Add hotreloading
Add an interval element Add an interval element
Compare bin version to Fes.toml Compare bin version to Fes.toml
Add ways to interact with left and right

View File

@@ -3,7 +3,7 @@ local std = require("core.std")
local M = {} local M = {}
M.__index = M M.__index = M
function M.site_builder(header, footer) function M.fes(header, footer)
local config = {} local config = {}
local site_config = {} local site_config = {}
@@ -119,6 +119,7 @@ a:hover{text-decoration:underline}
.muted{color:#9aa6b1} .muted{color:#9aa6b1}
.lead{font-size:15px;margin-top:8px} .lead{font-size:15px;margin-top:8px}
.callout{display:block;margin:10px 0}
.small{font-size:13px;color:#9aa6b1;margin-top:6px} .small{font-size:13px;color:#9aa6b1;margin-top:6px}
.highlight{font-weight:700;color:#cde7ff} .highlight{font-weight:700;color:#cde7ff}

19
core/console.lua Normal file
View File

@@ -0,0 +1,19 @@
local M = {}
function M.log(fmt, ...)
print(string.format("[%12f] ", os.clock()) .. string.format(fmt, ...))
end
function M.info(fmt, ...)
print("INFO: " .. string.format(fmt, ...))
end
function M.warn(fmt, ...)
print("WARN: " .. string.format(fmt, ...))
end
function M.error(fmt, ...)
print("ERROR: " .. string.format(fmt, ...))
end
return M

View File

@@ -32,6 +32,10 @@ function M.muted(str)
return '<div class="muted">' .. str .. '</div>' return '<div class="muted">' .. str .. '</div>'
end end
function M.callout(str)
return '<div class="callout">' .. str .. '</div>'
end
function M.h1(str) function M.h1(str)
return "<h1>" .. (str or "") .. "</h1>" return "<h1>" .. (str or "") .. "</h1>"
end end
@@ -210,4 +214,8 @@ function M.copyright()
return "&#169;" return "&#169;"
end end
function M.highlight(str)
return '<span class="highlight">' .. str .. "</span>"
end
return M return M

26
main.go
View File

@@ -11,6 +11,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/html"
@@ -21,11 +22,16 @@ import (
//go:embed core/builtin.lua //go:embed core/builtin.lua
var builtinLua string var builtinLua string
//go:embed core/markdown.lua //go:embed core/markdown.lua
var markdownLua string var markdownLua string
//go:embed core/std.lua //go:embed core/std.lua
var stdLua string var stdLua string
//go:embed core/console.lua
var consoleLua string
const version = "1.0.0" const version = "1.0.0"
type MyConfig struct { type MyConfig struct {
@@ -58,15 +64,24 @@ func loadLua(luaDir string, entry string, cfg *MyConfig) (string, error) {
L := lua.NewState() L := lua.NewState()
defer L.Close() defer L.Close()
L.PreloadModule("core.std", func(L *lua.LState) int {
if err := L.DoString(stdLua); err != nil {
panic(err)
}
L.Push(L.Get(-1))
return 1
})
L.PreloadModule("fes", func(L *lua.LState) int { L.PreloadModule("fes", func(L *lua.LState) int {
mod := L.NewTable() mod := L.NewTable()
coreModules := map[string]string{ coreModules := map[string]string{
"builtin": builtinLua, "builtin": builtinLua,
"markdown": markdownLua, "markdown": markdownLua,
"std": stdLua, "std": stdLua,
"console": consoleLua,
} }
for modName, luaCode := range coreModules { for modName, luaCode := range coreModules {
if err := L.DoString(luaCode); err != nil { if err := L.DoString(luaCode); err != nil {
fmt.Println("error loading", modName, ":", err) fmt.Println("error loading", modName, ":", err)
@@ -220,7 +235,7 @@ func startServer(dir string) error {
} }
wwwDir := filepath.Join(dir, "www") wwwDir := filepath.Join(dir, "www")
entries, err := os.ReadDir(wwwDir) entries, err := os.ReadDir(wwwDir)
if err != nil { if err != nil {
return fmt.Errorf("failed to read www directory: %w", err) return fmt.Errorf("failed to read www directory: %w", err)
@@ -231,7 +246,7 @@ func startServer(dir string) error {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lua") { if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lua") {
baseName := strings.TrimSuffix(entry.Name(), ".lua") baseName := strings.TrimSuffix(entry.Name(), ".lua")
luaPath := filepath.Join(wwwDir, entry.Name()) luaPath := filepath.Join(wwwDir, entry.Name())
if baseName == "index" { if baseName == "index" {
routes["/"] = luaPath routes["/"] = luaPath
routes["/index"] = luaPath routes["/index"] = luaPath
@@ -244,6 +259,7 @@ func startServer(dir string) error {
for route, luaPath := range routes { for route, luaPath := range routes {
func(rt string, lp string) { func(rt string, lp string) {
http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(rt, func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[%s] LOAD /%s\n", time.Now().Format(time.RFC1123), lp)
data, err := loadLua(dir, lp, &cfg) data, err := loadLua(dir, lp, &cfg)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
@@ -255,7 +271,7 @@ func startServer(dir string) error {
} }
fmt.Printf("Server is running on http://localhost:%d\n", *port) fmt.Printf("Server is running on http://localhost:%d\n", *port)
return http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) return http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
} }

9
test/Fes.toml Normal file
View File

@@ -0,0 +1,9 @@
[site]
name = "test"
version = "0.0.1"
authors = ["vx-clutch"]
[fes]
version = "1.0.0"
CUSTOM_CSS =

11
test/www/index.lua Normal file
View File

@@ -0,0 +1,11 @@
local fes = require("fes")
local site = fes()
site:h1("<h1>")
site:h2("<h2>")
site:h3("<h3>")
site:h4("<h4>")
site:h5("<h5>")
site:h6("<h6>")
return site