feat: rebranding.

This commit is contained in:
Andrey Parhomenko 2024-08-02 20:30:32 +05:00
parent bdbce0100e
commit 4433aabbd0
18 changed files with 116 additions and 144 deletions

3
.gitignore vendored
View file

@ -1,3 +1,2 @@
/tpp
/exe
/kyra
Session.vim

4
build
View file

@ -1,4 +0,0 @@
#!/bin/sh
#
go build -o ./exe/ ./cmd/tht

10
cmd/kyra/main.go Normal file
View file

@ -0,0 +1,10 @@
package main
import (
"surdeus.su/util/kyra"
"os"
)
func main() {
kyra.Tool.Run(os.Args[1:])
}

View file

@ -1,10 +0,0 @@
package main
import (
"surdeus.su/util/tht"
"os"
)
func main() {
tht.Tool.Run(os.Args[1:])
}

View file

@ -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
}

9
go.mod
View file

@ -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
)

26
go.sum
View file

@ -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=

View file

@ -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)
}

View file

@ -1,3 +0,0 @@
#!/bin/sh
go install ./cmd/tht/

2
log.go
View file

@ -1,4 +1,4 @@
package tht
package kyra
import "log"
import "net/http"

View file

@ -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 {

View file

@ -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) {

View file

@ -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(`<body>`)
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)

View file

@ -1,4 +0,0 @@
#!/bin/sh
#
go build -o ./exe/ ./cmd/tht/ && \
./exe/tht

View file

@ -1,5 +1,5 @@
{{#
html := import("html").new_render()
html := import("html").Renderer()
}}
{{
h := html.main().body(

View file

@ -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 @@
}
}}
<div id="bool">{{pp.print(context.global.val)}}</div>

29
tool.go
View file

@ -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.
`)