alpha p10
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fes/src/config"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
@@ -19,16 +20,13 @@ 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())
|
||||
@@ -72,7 +70,6 @@ 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 {
|
||||
@@ -85,7 +82,6 @@ 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)
|
||||
@@ -96,7 +92,6 @@ 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)
|
||||
@@ -109,6 +104,12 @@ 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()))
|
||||
@@ -118,7 +119,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
||||
val := L.Get(-1)
|
||||
L.Pop(1)
|
||||
tbl, ok := val.(*lua.LTable)
|
||||
if !ok {
|
||||
if !ok || tbl == nil {
|
||||
tbl = L.NewTable()
|
||||
}
|
||||
app.RawSetString(base, tbl)
|
||||
@@ -126,7 +127,6 @@ 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,8 +197,7 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
|
||||
}
|
||||
}
|
||||
|
||||
includeDir := filepath.Join(luaDir, "include")
|
||||
mod.RawSetString("app", loadIncludeModules(L, includeDir))
|
||||
mod.RawSetString("app", loadIncludeModules(L, filepath.Join(".", "include")))
|
||||
|
||||
if cfg != nil {
|
||||
site := L.NewTable()
|
||||
@@ -253,7 +252,6 @@ 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 {
|
||||
@@ -344,7 +342,6 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user