alpha p9
This commit is contained in:
|
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 1.3 MiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* try to get git user, if not system user */
|
||||||
func getName() string {
|
func getName() string {
|
||||||
out, err := exec.Command("git", "config", "user.name").Output()
|
out, err := exec.Command("git", "config", "user.name").Output()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -21,43 +22,10 @@ func getName() string {
|
|||||||
if err == nil && u.Username != "" {
|
if err == nil && u.Username != "" {
|
||||||
return u.Username
|
return u.Username
|
||||||
}
|
}
|
||||||
return ""
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
// func Project(dir string) error {
|
/* helper function for writing files */
|
||||||
// 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 := fmt.Sprintf(`local fes = require("fes")
|
|
||||||
// local site = fes.fes()
|
|
||||||
//
|
|
||||||
// site.title = "%s"
|
|
||||||
//
|
|
||||||
// site:h1("Hello, World!")
|
|
||||||
//
|
|
||||||
// return site
|
|
||||||
// `, dir)
|
|
||||||
// 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(`[app]
|
|
||||||
//
|
|
||||||
// name = "%s"
|
|
||||||
// version = "0.0.1"
|
|
||||||
// authors = ["%s"]`, dir, getName())
|
|
||||||
// if err := os.WriteFile(indexFes, []byte(content), 0644); err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// fmt.Println("Created new project at", dir)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
func write(path string, format string, args ...interface{}) error {
|
func write(path string, format string, args ...interface{}) error {
|
||||||
dir := filepath.Dir(path)
|
dir := filepath.Dir(path)
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
@@ -72,6 +40,7 @@ func write(path string, format string, args ...interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* creates a hello world project */
|
||||||
func Project(dir string) error {
|
func Project(dir string) error {
|
||||||
if err := os.Mkdir(dir, 0755); err != nil {
|
if err := os.Mkdir(dir, 0755); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -22,11 +22,13 @@ import (
|
|||||||
"fes/src/config"
|
"fes/src/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* structure of all data passed to page via fes.bus */
|
||||||
type reqData struct {
|
type reqData struct {
|
||||||
path string
|
path string
|
||||||
params map[string]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 {
|
func handleDir(entries []os.DirEntry, dir string, routes map[string]string, base string, isStatic bool) error {
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
path := filepath.Join(dir, entry.Name())
|
path := filepath.Join(dir, entry.Name())
|
||||||
@@ -70,6 +72,7 @@ func basePath(base string) string {
|
|||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fixes empty TOML keys (e.g., key=) */
|
||||||
func fixMalformedToml(content string) string {
|
func fixMalformedToml(content string) string {
|
||||||
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
|
re := regexp.MustCompile(`(?m)^(\s*\w+\s*=\s*)$`)
|
||||||
return re.ReplaceAllStringFunc(content, func(match string) string {
|
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 {
|
func markdownToHTML(mdText string) string {
|
||||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
||||||
p := parser.NewWithExtensions(extensions)
|
p := parser.NewWithExtensions(extensions)
|
||||||
@@ -92,6 +96,7 @@ func markdownToHTML(mdText string) string {
|
|||||||
return string(markdown.Render(doc, renderer))
|
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 {
|
func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
||||||
app := L.NewTable()
|
app := L.NewTable()
|
||||||
ents, err := os.ReadDir(includeDir)
|
ents, err := os.ReadDir(includeDir)
|
||||||
@@ -121,6 +126,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|||||||
return app
|
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) {
|
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
@@ -247,6 +253,7 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* generate html index page for archive directories */
|
||||||
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
||||||
info, err := os.Stat(fsPath)
|
info, err := os.Stat(fsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -296,10 +303,7 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
|||||||
return strings.ToLower(list[i].name) < strings.ToLower(list[j].name)
|
return strings.ToLower(list[i].name) < strings.ToLower(list[j].name)
|
||||||
})
|
})
|
||||||
|
|
||||||
urlPath = strings.TrimPrefix(urlPath, "/archive")
|
urlPath = basePath(strings.TrimPrefix(urlPath, "/archive"))
|
||||||
if urlPath == "" {
|
|
||||||
urlPath = "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
b.WriteString("<html>\n<head><title>Index of ")
|
b.WriteString("<html>\n<head><title>Index of ")
|
||||||
@@ -340,6 +344,7 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
|||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* start http server with features */
|
||||||
func Start(dir string) error {
|
func Start(dir string) error {
|
||||||
if err := os.Chdir(dir); err != nil {
|
if err := os.Chdir(dir); err != nil {
|
||||||
return fmt.Errorf("failed to change directory to %s: %w", dir, err)
|
return fmt.Errorf("failed to change directory to %s: %w", dir, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user