From 4433aabbd003325bee524c5839b2cf99e8d0df17 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 2 Aug 2024 20:30:32 +0500 Subject: [PATCH] feat: rebranding. --- .gitignore | 3 +- build | 4 - cmd/kyra/main.go | 10 +++ cmd/tht/main.go | 10 --- context.go | 20 ++--- go.mod | 9 +-- go.sum | 26 ++----- handler.go | 94 ++++++++++++----------- install | 3 - log.go | 2 +- mdx/main.go | 24 +++--- mod/{post.tengo => post.xgo} | 6 +- mod/{pre.tengo => pre.xgo} | 10 +-- run-tht | 4 - src/check/{index.htm.tpp => index.htm.kr} | 2 +- src/{index.htm.tpp => index.htm.kr} | 4 +- src/{req.json.tpp => req.json.kr} | 0 tool.go | 29 ++++--- 18 files changed, 116 insertions(+), 144 deletions(-) delete mode 100755 build create mode 100644 cmd/kyra/main.go delete mode 100644 cmd/tht/main.go delete mode 100755 install rename mod/{post.tengo => post.xgo} (64%) rename mod/{pre.tengo => pre.xgo} (72%) delete mode 100755 run-tht rename src/check/{index.htm.tpp => index.htm.kr} (71%) rename src/{index.htm.tpp => index.htm.kr} (88%) rename src/{req.json.tpp => req.json.kr} (100%) diff --git a/.gitignore b/.gitignore index d128777..f7040da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -/tpp -/exe +/kyra Session.vim diff --git a/build b/build deleted file mode 100755 index 4c66820..0000000 --- a/build +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# -go build -o ./exe/ ./cmd/tht - diff --git a/cmd/kyra/main.go b/cmd/kyra/main.go new file mode 100644 index 0000000..74ecb0e --- /dev/null +++ b/cmd/kyra/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "surdeus.su/util/kyra" + "os" +) + +func main() { + kyra.Tool.Run(os.Args[1:]) +} diff --git a/cmd/tht/main.go b/cmd/tht/main.go deleted file mode 100644 index 5c2a29f..0000000 --- a/cmd/tht/main.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import ( - "surdeus.su/util/tht" - "os" -) - -func main() { - tht.Tool.Run(os.Args[1:]) -} diff --git a/context.go b/context.go index 55747c7..1273079 100644 --- a/context.go +++ b/context.go @@ -1,7 +1,7 @@ -package tht +package kyra -import "surdeus.su/core/xgo/xmodules/httpx" -import "surdeus.su/util/tht/mdx" +import httpx "surdeus.su/core/xgo/v2/stdlib/http" +import "surdeus.su/util/kyra/mdx" // Context key type for internal usage. type CKey string @@ -11,11 +11,13 @@ const ( ) type ContextValues struct { - Request *httpx.Request - Global any - Markdown *mdx.Markdown - IsIndex bool + Request *httpx.Request + Global any + Markdown *mdx.Markdown + IsIndex bool IndexSuffix string - Ext string - SourcePath string + PPExt string + SourcePath string + FPath string + FExt string } diff --git a/go.mod b/go.mod index 403e25d..624eb22 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,10 @@ -module surdeus.su/util/tht +module surdeus.su/util/kyra go 1.22.3 require ( - github.com/d5/tengo/v2 v2.17.0 github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 - surdeus.su/core/cli v0.1.2 - surdeus.su/core/xgo v0.8.0 - surdeus.su/util/tpp v0.4.0 + surdeus.su/core/cli v0.5.0 + surdeus.su/core/xgo/v2 v2.18.0 + surdeus.su/util/gopp v0.5.0 ) diff --git a/go.sum b/go.sum index 50fff03..091b605 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,8 @@ -github.com/d5/tengo/v2 v2.17.0 h1:BWUN9NoJzw48jZKiYDXDIF3QrIVZRm1uV1gTzeZ2lqM= -github.com/d5/tengo/v2 v2.17.0/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8= github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 h1:yEt5djSYb4iNtmV9iJGVday+i4e9u6Mrn5iP64HH5QM= github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= -surdeus.su/core/cli v0.1.2 h1:qPzjawqPyZsO4Z5SaA1u141recVE65yioA83Qs7Jecs= -surdeus.su/core/cli v0.1.2/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA= -surdeus.su/core/xgo v0.5.0 h1:/Rk3scfFkoSb0qjHRlkUNOp9sr/fd7wAvCiT4fBRo+U= -surdeus.su/core/xgo v0.5.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= -surdeus.su/core/xgo v0.6.0 h1:r8b2rm7hN35lOScaCuYxMm7bxyESc50UB66J4tGhnk8= -surdeus.su/core/xgo v0.6.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= -surdeus.su/core/xgo v0.6.1 h1:ssF7LrTyANmIVIqO6E2TprueNDup11/NWH8dbzue4VQ= -surdeus.su/core/xgo v0.6.1/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= -surdeus.su/core/xgo v0.7.0 h1:2HoiOTQ0JxqThArC6CRV/BPEuWT9CDlOKH0jyMqhAzU= -surdeus.su/core/xgo v0.7.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= -surdeus.su/core/xgo v0.8.0 h1:/dPBq01ck3VHLfWUgt0Wk1/vdpTiA1FQN2WoLUdylV0= -surdeus.su/core/xgo v0.8.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= -surdeus.su/util/tpp v0.3.2 h1:ebcnEcY+4tgB4a6trs4GBd2CJjrZJaPKh3i5RKQf8/U= -surdeus.su/util/tpp v0.3.2/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw= -surdeus.su/util/tpp v0.3.3 h1:GEyOlt4M1jE9q9HSPAds9X85qEc/FUpD/M90vlROgLw= -surdeus.su/util/tpp v0.3.3/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw= -surdeus.su/util/tpp v0.4.0 h1:GKmduh2AHdaTEZZptCH1yFfAcYiSTX8MvXTzIam8HpM= -surdeus.su/util/tpp v0.4.0/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw= +surdeus.su/core/cli v0.5.0 h1:jYvE0JVDikFT9FhWGV3wIAcMgByziAVxTwsVUwWkeHs= +surdeus.su/core/cli v0.5.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA= +surdeus.su/core/xgo/v2 v2.18.0 h1:Ypz2CIiqkfVD9S4Jes5fES9zkcOYppNRvf4Af5D3JXU= +surdeus.su/core/xgo/v2 v2.18.0/go.mod h1:EBQA07DjGd0bqIGbaNQ2HZHQkilbYacI7fpQen5ivao= +surdeus.su/util/gopp v0.5.0 h1:a1PmsniSR+8pm0TLx+5x2lkGqCiIgpYJOFKzUzKT3ZM= +surdeus.su/util/gopp v0.5.0/go.mod h1:2xkmqz5MjzMlpPPsfaAR5uYB934vp+sVKBRs9qp2IiE= diff --git a/handler.go b/handler.go index 38d7636..99415c7 100644 --- a/handler.go +++ b/handler.go @@ -1,12 +1,12 @@ -package tht +package kyra import ( - //"github.com/d5/tengo/v2" - "surdeus.su/util/tht/mdx" - //"surdeus.su/core/xgo/xmodules/htmlx" - "surdeus.su/core/xgo/xmodules/httpx" - "surdeus.su/core/xgo/xmodules" - "surdeus.su/util/tpp" + "surdeus.su/util/kyra/mdx" + httpx "surdeus.su/core/xgo/v2/stdlib/http" + "surdeus.su/core/xgo/v2/stdlib" + "surdeus.su/util/gopp" + + "bytes" "path/filepath" "net/http" "context" @@ -28,10 +28,10 @@ type Handler struct { sourcePath string // Preprocessor must be set by user // to be able to bring custom features in. - pp *tpp.Preprocessor - // Aditional extension. ".tpp" by default. - // For example "file.html.tpp" will be - // first preprocessed TPP and sent back as simple HTML. + pp *gopp.Preprocessor + // Aditional extension. ".kr" by default. + // For example "file.html.kr" will be + // first preprocessed by gopp and sent back as simple HTML. ext string // The global map accessable from all the @@ -46,7 +46,7 @@ type Handler struct { // specified preprocessor, source path, // preprocessor extension and the global value. func NewHandler( - pp *tpp.Preprocessor, + pp *gopp.Preprocessor, src, ext, index string, global any, ) *Handler { @@ -59,16 +59,16 @@ func NewHandler( return ret } -// Returns the default tpp.Preprocessor suitable for +// Returns the default gopp.Preprocessor suitable for // the most needs. -func DefaultPP(mod string) *tpp.Preprocessor { - t := tpp.NewTengo().SetPreCompile(func( +func DefaultPP(mod string) *gopp.Preprocessor { + t := gopp.NewXGo().SetPreCompile(func( ctx context.Context, - s *tpp.Script, + s *gopp.Script, ) { s.SetImportDir(mod) - modules := xmodules.GetModules() + modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...) modules.Add("markdown", mdx.GetModule()) s.SetImports(modules) s.EnableFileImport(true) @@ -79,8 +79,10 @@ func DefaultPP(mod string) *tpp.Preprocessor { s.Add("__markdown__", vals.Markdown) s.Add("__is_index__", vals.IsIndex) s.Add("__index_suffix__", vals.IndexSuffix) - s.Add("__tpp_ext__", vals.Ext) + s.Add("__pp_ext__", vals.PPExt) s.Add("__source_path__", vals.SourcePath) + s.Add("__fext__", vals.FExt) + s.Add("__fpath__", vals.FPath) }).SetPreCode(func(ctx context.Context) []byte { return []byte(` @@ -90,10 +92,6 @@ func DefaultPP(mod string) *tpp.Preprocessor { return pp.write_raw(__markdown__(args...)) } - markdown_file := func(file_path){ - return pp.write_raw(__markdown(__os__.read_file(file_path))) - } - __http__ := immutable({ request : __http_request__ }) @@ -103,8 +101,10 @@ func DefaultPP(mod string) *tpp.Preprocessor { context.global = __global__ context.is_index = __is_index__ context.index_suffix = __index_suffix__ - context.tpp_ext = __tpp_ext__ + context.pp_ext = __pp_ext__ context.source_path = __source_path__ + context.fext = __fext__ + context.fpath = __fpath__ import("./pre")(context) `) @@ -114,7 +114,7 @@ func DefaultPP(mod string) *tpp.Preprocessor { `) }) - return tpp.New(t) + return gopp.New(t) } func (h *Handler) SetMD(md *mdx.Markdown) *Handler { @@ -164,14 +164,8 @@ func (h *Handler) ServeHTTP( } } - //process := true - fileData, err := io.ReadAll(file) - if err != nil { - http.NotFound(w, r) - return - } - - + fext := filepath.Ext(filePath) + fpath := filePath ctx := context.WithValue( r.Context(), KeyValues, @@ -183,8 +177,10 @@ func (h *Handler) ServeHTTP( Markdown: h.md, IsIndex: isIndex, IndexSuffix: h.indexSuffix, - Ext: h.ext, + PPExt: h.ext, SourcePath: h.sourcePath, + FPath: fpath, + FExt: fext, }, ) @@ -193,22 +189,32 @@ func (h *Handler) ServeHTTP( fileExt := filepath.Ext(filePath) contentType := mime.TypeByExtension(fileExt) w.Header().Set("Content-Type", contentType) - processedData := fileData + + var buf bytes.Buffer + var reader io.Reader if shouldProcess { - _, processedData, err = h.pp.Process( - ctx, - filePathTpp, - fileData, + compiled, err := h.pp.Compile( + ctx, file, ) if err != nil { - http.NotFound(w, r) - log.Printf( - "Error: pp.Process(...): %s\n", - err, - ) + log.Printf("gopp.Compile(...): %s\n", err) + http.Error(w, err.Error(), 500) return } + + err = h.pp.XGo().RunContext( + ctx, compiled, + filePath, &buf, + ) + if err != nil { + log.Printf("gopp.RunContext(...): %s\n", err) + http.Error(w, err.Error(), 500) + return + } + reader = &buf + } else { + reader = file } - w.Write(processedData) + io.Copy(w, reader) } diff --git a/install b/install deleted file mode 100755 index e184488..0000000 --- a/install +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -go install ./cmd/tht/ diff --git a/log.go b/log.go index f347efa..988cf7b 100644 --- a/log.go +++ b/log.go @@ -1,4 +1,4 @@ -package tht +package kyra import "log" import "net/http" diff --git a/mdx/main.go b/mdx/main.go index 6e6734c..231c1fd 100644 --- a/mdx/main.go +++ b/mdx/main.go @@ -2,16 +2,16 @@ package mdx import "github.com/gomarkdown/markdown" import "github.com/gomarkdown/markdown/html" import "github.com/gomarkdown/markdown/parser" -import "github.com/d5/tengo/v2" +import "surdeus.su/core/xgo/v2" import "bytes" -func GetModule() tengo.Importable { - return &tengo.BuiltinModule{Attrs:map[string]tengo.Object{ - "new": &tengo.BuiltinFunction{ +func GetModule() xgo.Importable { + return &xgo.BuiltinModule{Attrs:map[string]xgo.Object{ + "new": &xgo.BuiltinFunction{ Name: "new", Value: func( - args ...tengo.Object, - ) (tengo.Object, error){ + args ...xgo.Object, + ) (xgo.Object, error){ return MakeDefaultMarkdown(), nil }, }, @@ -19,7 +19,7 @@ func GetModule() tengo.Importable { } type Markdown struct { - tengo.ObjectImpl + xgo.ObjectImpl MakeParser func() *parser.Parser MakeRenderer func() *html.Renderer } @@ -55,13 +55,13 @@ func (md *Markdown) CanCall() bool { } func (md *Markdown) Call( - args ...tengo.Object, -) (tengo.Object, error) { + args ...xgo.Object, +) (xgo.Object, error) { var b bytes.Buffer for _, arg := range args { - str, ok := tengo.ToString(arg) + str, ok := xgo.ToString(arg) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "v", Expected: "stringer", Found: arg.TypeName(), @@ -75,7 +75,7 @@ func (md *Markdown) Call( return nil, err } - return tengo.FromInterface(rendered) + return xgo.FromInterface(rendered) } func MakeDefaultParser() *parser.Parser { diff --git a/mod/post.tengo b/mod/post.xgo similarity index 64% rename from mod/post.tengo rename to mod/post.xgo index c4a439e..8691f07 100644 --- a/mod/post.tengo +++ b/mod/post.xgo @@ -1,4 +1,3 @@ -fmt := import("fmt") paths := import("paths") htmExt := func(c){ if c.is_compo { @@ -14,10 +13,7 @@ exts := { } export func(c){ - ext := paths.ext(c.pp.filepath) - ext = paths.ext( - c.pp.filepath[:len(c.pp.filepath) - len(ext)] - ) + ext := c.fext cl := exts[ext] if !is_undefined(cl) { diff --git a/mod/pre.tengo b/mod/pre.xgo similarity index 72% rename from mod/pre.tengo rename to mod/pre.xgo index b550388..ab410fb 100644 --- a/mod/pre.tengo +++ b/mod/pre.xgo @@ -1,6 +1,5 @@ -fmt := import("fmt") paths := import("paths") -html := import("html").new_render() +html := import("html").Renderer() htmExt := func(c){ if c.is_compo { @@ -20,6 +19,7 @@ htmExt := func(c){ )) c.pp.print(``) + c.pp.print(html.div().body(c.global.val)) } exts := { @@ -28,11 +28,7 @@ exts := { } export func(c){ - ext := paths.ext(c.pp.filepath) - ext = paths.ext( - c.pp.filepath[:len(c.pp.filepath) - len(ext)] - ) - + ext := c.fext cl := exts[ext] if cl { cl(c) diff --git a/run-tht b/run-tht deleted file mode 100755 index 646c7c1..0000000 --- a/run-tht +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# -go build -o ./exe/ ./cmd/tht/ && \ - ./exe/tht diff --git a/src/check/index.htm.tpp b/src/check/index.htm.kr similarity index 71% rename from src/check/index.htm.tpp rename to src/check/index.htm.kr index ee4255e..10176f0 100644 --- a/src/check/index.htm.tpp +++ b/src/check/index.htm.kr @@ -1,5 +1,5 @@ {{# - html := import("html").new_render() + html := import("html").Renderer() }} {{ h := html.main().body( diff --git a/src/index.htm.tpp b/src/index.htm.kr similarity index 88% rename from src/index.htm.tpp rename to src/index.htm.kr index 2b3e556..c7b799e 100644 --- a/src/index.htm.tpp +++ b/src/index.htm.kr @@ -2,7 +2,7 @@ context.title = "The example.site main page" fmt := import("fmt") - html := import("html").new_render() + html := import("html").Renderer() }} {{ @@ -37,3 +37,5 @@ } }} +
{{pp.print(context.global.val)}}
+ diff --git a/src/req.json.tpp b/src/req.json.kr similarity index 100% rename from src/req.json.tpp rename to src/req.json.kr diff --git a/tool.go b/tool.go index 3d6490d..aea56c1 100644 --- a/tool.go +++ b/tool.go @@ -1,33 +1,26 @@ -package tht +package kyra import ( - - "github.com/d5/tengo/v2" - "surdeus.su/util/tht/mdx" + "surdeus.su/core/xgo/v2" + "surdeus.su/util/kyra/mdx" "surdeus.su/core/cli/mtool" "net/http" "log" ) - // Simple PHP-like server implementation. -var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { +var Tool = mtool.T("kyra").Func(func(flags *mtool.Flags) { var ( addr, ext, src, mod, index string - noLog bool + noLog bool ) flags.StringVar(&addr, "addr", ":3000", "address to serve at") flags.StringVar(&mod, "mod", "./mod", "path to store Tengo modules") flags.StringVar(&src, "src", "./src", "directory with source files") - flags.StringVar(&ext, "ext", ".tpp", "extension for TPP files") + flags.StringVar(&ext, "ext", ".kr", "extension for Kyra files") flags.StringVar(&index, "index", "index.htm", "index file name") - flags.BoolVar( - &noLog, - "no-log", - false, - "disable logging", - ) + flags.BoolVar(&noLog, "no-log", false, "disable logging") flags.Parse() @@ -35,7 +28,7 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { After: NewHandler( DefaultPP(mod), src, ext, index, - map[string] tengo.Object{}, + map[string]xgo.Object{}, ).SetMD(mdx.MakeDefaultMarkdown()), } if !noLog { @@ -52,4 +45,8 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { if err != nil { log.Printf("Error: srv.ListenAndServe(...): %s\n", err) } -}) +}).Ldesc(` + Simple PHP-like server implementation of the Kyra + suitable for the most of the needs of websites. +`) +