287 lines
6.4 KiB
Go
287 lines
6.4 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"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 {
|
|
Site struct {
|
|
Name string `toml:"name"`
|
|
Version string `toml:"version"`
|
|
Authors []string `toml:"authors"`
|
|
} `toml:"site"`
|
|
Fes struct {
|
|
Version string `toml:"version"`
|
|
CUSTOM_CSS string `toml:"CUSTOM_CSS,omitempty"`
|
|
} `toml:"fes"`
|
|
}
|
|
|
|
var port = flag.Int("p", 3000, "set the server port")
|
|
|
|
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()
|
|
|
|
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)
|
|
L.Pop(1)
|
|
tbl, ok := val.(*lua.LTable)
|
|
if !ok {
|
|
t := L.NewTable()
|
|
t.RawSetString("value", val)
|
|
tbl = t
|
|
}
|
|
if modName == "builtin" {
|
|
tbl.ForEach(func(key, value lua.LValue) {
|
|
mod.RawSet(key, value)
|
|
})
|
|
} else {
|
|
mod.RawSetString(modName, tbl)
|
|
}
|
|
}
|
|
|
|
if cfg != nil {
|
|
configTable := L.NewTable()
|
|
siteTable := L.NewTable()
|
|
siteTable.RawSetString("version", lua.LString(cfg.Site.Version))
|
|
siteTable.RawSetString("name", lua.LString(cfg.Site.Name))
|
|
authorsTable := L.NewTable()
|
|
for i, author := range cfg.Site.Authors {
|
|
authorsTable.RawSetInt(i+1, lua.LString(author))
|
|
}
|
|
siteTable.RawSetString("authors", authorsTable)
|
|
configTable.RawSetString("site", siteTable)
|
|
fesTable := L.NewTable()
|
|
fesTable.RawSetString("version", lua.LString(cfg.Fes.Version))
|
|
configTable.RawSetString("fes", fesTable)
|
|
mod.RawSetString("config", configTable)
|
|
}
|
|
|
|
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
|
|
})
|
|
|
|
if err := L.DoFile(entry); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
top := L.GetTop()
|
|
if top == 0 {
|
|
fmt.Println("warning: no return value from Lua file")
|
|
return "", nil
|
|
}
|
|
|
|
resultVal := L.Get(-1)
|
|
L.SetGlobal("__fes_result", resultVal)
|
|
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
|
L.GetGlobal("__fes_result")
|
|
if s := L.ToString(-1); s != "" {
|
|
return s, nil
|
|
}
|
|
return "", nil
|
|
}
|
|
if s := L.ToString(-1); s != "" {
|
|
return s, nil
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func getName() string {
|
|
out, err := exec.Command("git", "config", "user.name").Output()
|
|
if err == nil {
|
|
s := strings.TrimSpace(string(out))
|
|
if s != "" {
|
|
return s
|
|
}
|
|
}
|
|
u, err := user.Current()
|
|
if err == nil && u.Username != "" {
|
|
return u.Username
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func newProject(dir string) error {
|
|
if err := os.MkdirAll(filepath.Join(dir, "www"), 0755); err != nil {
|
|
return err
|
|
}
|
|
indexLua := filepath.Join(dir, "www", "index.lua")
|
|
if _, err := os.Stat(indexLua); os.IsNotExist(err) {
|
|
content := `local fes = require("fes")
|
|
local site = fes.site_builder()
|
|
|
|
site:h1("Hello, World!")
|
|
|
|
return site
|
|
`
|
|
if err := os.WriteFile(indexLua, []byte(content), 0644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
indexFes := filepath.Join(dir, "Fes.toml")
|
|
if _, err := os.Stat(indexFes); os.IsNotExist(err) {
|
|
content := fmt.Sprintf(`[site]
|
|
|
|
name = "%s"
|
|
version = "0.0.1"
|
|
authors = ["%s"]
|
|
|
|
[fes]
|
|
version = "%s"
|
|
CUSTOM_CSS =
|
|
`, dir, getName(), version)
|
|
if err := os.WriteFile(indexFes, []byte(content), 0644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fmt.Println("Created new project at", dir)
|
|
return nil
|
|
}
|
|
|
|
func fixMalformedToml(content string) string {
|
|
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
|
|
return re.ReplaceAllStringFunc(content, func(match string) string {
|
|
parts := strings.Split(strings.TrimSpace(match), "=")
|
|
if len(parts) == 2 && strings.TrimSpace(parts[1]) == "" {
|
|
key := strings.TrimSpace(parts[0])
|
|
return key + " = \"\""
|
|
}
|
|
return match
|
|
})
|
|
}
|
|
|
|
func startServer(dir string) error {
|
|
doc, err := os.ReadFile(filepath.Join(dir, "Fes.toml"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
docStr := fixMalformedToml(string(doc))
|
|
|
|
var cfg MyConfig
|
|
err = toml.Unmarshal([]byte(docStr), &cfg)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse Fes.toml: %w", err)
|
|
}
|
|
|
|
wwwDir := filepath.Join(dir, "www")
|
|
|
|
entries, err := os.ReadDir(wwwDir)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read www directory: %w", err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if len(os.Args) < 3 {
|
|
fmt.Println("Usage: fes <command> <project_dir>")
|
|
fmt.Println("Commands: new, run")
|
|
os.Exit(1)
|
|
}
|
|
|
|
cmd := os.Args[1]
|
|
dir := os.Args[2]
|
|
|
|
switch cmd {
|
|
case "new":
|
|
if err := newProject(dir); err != nil {
|
|
panic(err)
|
|
}
|
|
case "run":
|
|
if err := startServer(dir); err != nil {
|
|
panic(err)
|
|
}
|
|
default:
|
|
fmt.Println("Unknown command:", cmd)
|
|
os.Exit(1)
|
|
}
|
|
}
|