\n", cmd)
- flag.Usage()
- os.Exit(1)
- }
- dir = args[1]
- }
+ cmd := os.Args[1]
+ dir := os.Args[2]
switch cmd {
case "new":
if err := new.Project(dir); err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
- os.Exit(1)
- }
- case "doc":
- if err := doc.Open(); err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
- os.Exit(1)
+ panic(err)
}
case "run":
if err := server.Start(dir); err != nil {
- if errors.Is(err, os.ErrNotExist) {
- fmt.Fprintf(os.Stderr, "%s does not exist\n", dir)
- fmt.Fprintf(os.Stderr, "Try: fes new %s\n", dir)
- os.Exit(1)
- } else {
- fmt.Fprintln(os.Stderr, "Error:", err)
- os.Exit(1)
- }
+ panic(err)
}
default:
- fmt.Fprintf(os.Stderr, "Unknown command: %s\n", cmd)
- flag.Usage()
+ fmt.Println("Unknown command:", cmd)
os.Exit(1)
}
}
diff --git a/src/doc/doc.go b/src/doc/doc.go
deleted file mode 100644
index a48d46f..0000000
--- a/src/doc/doc.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package doc
-
-import (
- "fmt"
- "os"
- "path/filepath"
-
- "github.com/pkg/browser"
-)
-
-func Open() error {
- fmt.Println("Opening documentation in browser")
-
- tmpFile := filepath.Join(os.TempDir(), "doc.html")
- content := `
-This feature is not implemented yet. It will be once the doc site
-is up and running, for now read through the core/ files and examples.
-
`
-
- if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil {
- return err
- }
-
- return browser.OpenFile(tmpFile)
-}
diff --git a/src/new/new.go b/src/new/new.go
index 71ad79e..2fa1b0f 100644
--- a/src/new/new.go
+++ b/src/new/new.go
@@ -48,21 +48,16 @@ func Project(dir string) error {
if err := os.Chdir(dir); err != nil {
return err
}
-
- name := getName()
-
write("www/index.lua", `local fes = require("fes")
local site = fes.fes()
--- site.copyright = fes.util.copyright("https://example.com", "%s")
-
site:h1("Hello, World!")
-return site`, name)
+return site`)
write("Fes.toml", `[app]
name = "%s"
version = "0.0.1"
-authors = ["%s"]`, dir, name)
+authors = ["%s"]`, dir, getName())
return nil
}
diff --git a/src/server/server.go b/src/server/server.go
index 396fe9f..b17d174 100644
--- a/src/server/server.go
+++ b/src/server/server.go
@@ -12,7 +12,6 @@ import (
"strings"
"time"
- "fes/src/config"
"github.com/fatih/color"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
@@ -20,13 +19,16 @@ import (
"github.com/pelletier/go-toml/v2"
lua "github.com/yuin/gopher-lua"
"html/template"
+ "fes/src/config"
)
+/* structure of all data passed to page via fes.bus */
type reqData struct {
path string
params map[string]string
}
+/* loads directories to routes */
func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base string, isStatic bool) error {
for _, entry := range entries {
path := filepath.Join(dir, entry.Name())
@@ -70,6 +72,7 @@ func basePath(base string) string {
return base
}
+/* fixes empty TOML keys (e.g., key=) */
func fixMalformedToml(content string) string {
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
return re.ReplaceAllStringFunc(content, func(match string) string {
@@ -82,6 +85,7 @@ func fixMalformedToml(content string) string {
})
}
+/* converts markdown to html */
func markdownToHTML(mdText string) string {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
@@ -92,6 +96,7 @@ func markdownToHTML(mdText string) string {
return string(markdown.Render(doc, renderer))
}
+/* load Lua include modules used in core/ into their table */
func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
app := L.NewTable()
ents, err := os.ReadDir(includeDir)
@@ -104,12 +109,6 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
}
base := strings.TrimSuffix(e.Name(), ".lua")
path := filepath.Join(includeDir, e.Name())
- if _, err := os.Stat(path); err != nil {
- tbl := L.NewTable()
- tbl.RawSetString("error", lua.LString(fmt.Sprintf("file not found: %s", path)))
- app.RawSetString(base, tbl)
- continue
- }
if err := L.DoFile(path); err != nil {
tbl := L.NewTable()
tbl.RawSetString("error", lua.LString(err.Error()))
@@ -119,7 +118,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
val := L.Get(-1)
L.Pop(1)
tbl, ok := val.(*lua.LTable)
- if !ok || tbl == nil {
+ if !ok {
tbl = L.NewTable()
}
app.RawSetString(base, tbl)
@@ -127,6 +126,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
return app
}
+/* load and execute lua scripts with core and user modules */
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
L := lua.NewState()
defer L.Close()
@@ -197,7 +197,8 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
}
}
- mod.RawSetString("app", loadIncludeModules(L, filepath.Join(".", "include")))
+ includeDir := filepath.Join(luaDir, "include")
+ mod.RawSetString("app", loadIncludeModules(L, includeDir))
if cfg != nil {
site := L.NewTable()
@@ -252,6 +253,7 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
return "", nil
}
+/* generate html index page for archive directories */
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
info, err := os.Stat(fsPath)
if err != nil {
@@ -342,6 +344,7 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
return b.String(), nil
}
+/* start http server with features */
func Start(dir string) error {
if err := os.Chdir(dir); err != nil {
return fmt.Errorf("failed to change directory to %s: %w", dir, err)