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 /kyra
/exe
Session.vim 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 httpx "surdeus.su/core/xgo/v2/stdlib/http"
import "surdeus.su/util/tht/mdx" import "surdeus.su/util/kyra/mdx"
// Context key type for internal usage. // Context key type for internal usage.
type CKey string type CKey string
@ -11,11 +11,13 @@ const (
) )
type ContextValues struct { type ContextValues struct {
Request *httpx.Request Request *httpx.Request
Global any Global any
Markdown *mdx.Markdown Markdown *mdx.Markdown
IsIndex bool IsIndex bool
IndexSuffix string IndexSuffix string
Ext string PPExt string
SourcePath 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 go 1.22.3
require ( require (
github.com/d5/tengo/v2 v2.17.0
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
surdeus.su/core/cli v0.1.2 surdeus.su/core/cli v0.5.0
surdeus.su/core/xgo v0.8.0 surdeus.su/core/xgo/v2 v2.18.0
surdeus.su/util/tpp v0.4.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 h1:yEt5djSYb4iNtmV9iJGVday+i4e9u6Mrn5iP64HH5QM=
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= 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.5.0 h1:jYvE0JVDikFT9FhWGV3wIAcMgByziAVxTwsVUwWkeHs=
surdeus.su/core/cli v0.1.2/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA= surdeus.su/core/cli v0.5.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=
surdeus.su/core/xgo v0.5.0 h1:/Rk3scfFkoSb0qjHRlkUNOp9sr/fd7wAvCiT4fBRo+U= surdeus.su/core/xgo/v2 v2.18.0 h1:Ypz2CIiqkfVD9S4Jes5fES9zkcOYppNRvf4Af5D3JXU=
surdeus.su/core/xgo v0.5.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= surdeus.su/core/xgo/v2 v2.18.0/go.mod h1:EBQA07DjGd0bqIGbaNQ2HZHQkilbYacI7fpQen5ivao=
surdeus.su/core/xgo v0.6.0 h1:r8b2rm7hN35lOScaCuYxMm7bxyESc50UB66J4tGhnk8= surdeus.su/util/gopp v0.5.0 h1:a1PmsniSR+8pm0TLx+5x2lkGqCiIgpYJOFKzUzKT3ZM=
surdeus.su/core/xgo v0.6.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= surdeus.su/util/gopp v0.5.0/go.mod h1:2xkmqz5MjzMlpPPsfaAR5uYB934vp+sVKBRs9qp2IiE=
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=

View file

@ -1,12 +1,12 @@
package tht package kyra
import ( import (
//"github.com/d5/tengo/v2" "surdeus.su/util/kyra/mdx"
"surdeus.su/util/tht/mdx" httpx "surdeus.su/core/xgo/v2/stdlib/http"
//"surdeus.su/core/xgo/xmodules/htmlx" "surdeus.su/core/xgo/v2/stdlib"
"surdeus.su/core/xgo/xmodules/httpx" "surdeus.su/util/gopp"
"surdeus.su/core/xgo/xmodules"
"surdeus.su/util/tpp" "bytes"
"path/filepath" "path/filepath"
"net/http" "net/http"
"context" "context"
@ -28,10 +28,10 @@ type Handler struct {
sourcePath string sourcePath string
// Preprocessor must be set by user // Preprocessor must be set by user
// to be able to bring custom features in. // to be able to bring custom features in.
pp *tpp.Preprocessor pp *gopp.Preprocessor
// Aditional extension. ".tpp" by default. // Aditional extension. ".kr" by default.
// For example "file.html.tpp" will be // For example "file.html.kr" will be
// first preprocessed TPP and sent back as simple HTML. // first preprocessed by gopp and sent back as simple HTML.
ext string ext string
// The global map accessable from all the // The global map accessable from all the
@ -46,7 +46,7 @@ type Handler struct {
// specified preprocessor, source path, // specified preprocessor, source path,
// preprocessor extension and the global value. // preprocessor extension and the global value.
func NewHandler( func NewHandler(
pp *tpp.Preprocessor, pp *gopp.Preprocessor,
src, ext, index string, src, ext, index string,
global any, global any,
) *Handler { ) *Handler {
@ -59,16 +59,16 @@ func NewHandler(
return ret return ret
} }
// Returns the default tpp.Preprocessor suitable for // Returns the default gopp.Preprocessor suitable for
// the most needs. // the most needs.
func DefaultPP(mod string) *tpp.Preprocessor { func DefaultPP(mod string) *gopp.Preprocessor {
t := tpp.NewTengo().SetPreCompile(func( t := gopp.NewXGo().SetPreCompile(func(
ctx context.Context, ctx context.Context,
s *tpp.Script, s *gopp.Script,
) { ) {
s.SetImportDir(mod) s.SetImportDir(mod)
modules := xmodules.GetModules() modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
modules.Add("markdown", mdx.GetModule()) modules.Add("markdown", mdx.GetModule())
s.SetImports(modules) s.SetImports(modules)
s.EnableFileImport(true) s.EnableFileImport(true)
@ -79,8 +79,10 @@ func DefaultPP(mod string) *tpp.Preprocessor {
s.Add("__markdown__", vals.Markdown) s.Add("__markdown__", vals.Markdown)
s.Add("__is_index__", vals.IsIndex) s.Add("__is_index__", vals.IsIndex)
s.Add("__index_suffix__", vals.IndexSuffix) 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("__source_path__", vals.SourcePath)
s.Add("__fext__", vals.FExt)
s.Add("__fpath__", vals.FPath)
}).SetPreCode(func(ctx context.Context) []byte { }).SetPreCode(func(ctx context.Context) []byte {
return []byte(` return []byte(`
@ -90,10 +92,6 @@ func DefaultPP(mod string) *tpp.Preprocessor {
return pp.write_raw(__markdown__(args...)) return pp.write_raw(__markdown__(args...))
} }
markdown_file := func(file_path){
return pp.write_raw(__markdown(__os__.read_file(file_path)))
}
__http__ := immutable({ __http__ := immutable({
request : __http_request__ request : __http_request__
}) })
@ -103,8 +101,10 @@ func DefaultPP(mod string) *tpp.Preprocessor {
context.global = __global__ context.global = __global__
context.is_index = __is_index__ context.is_index = __is_index__
context.index_suffix = __index_suffix__ context.index_suffix = __index_suffix__
context.tpp_ext = __tpp_ext__ context.pp_ext = __pp_ext__
context.source_path = __source_path__ context.source_path = __source_path__
context.fext = __fext__
context.fpath = __fpath__
import("./pre")(context) 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 { func (h *Handler) SetMD(md *mdx.Markdown) *Handler {
@ -164,14 +164,8 @@ func (h *Handler) ServeHTTP(
} }
} }
//process := true fext := filepath.Ext(filePath)
fileData, err := io.ReadAll(file) fpath := filePath
if err != nil {
http.NotFound(w, r)
return
}
ctx := context.WithValue( ctx := context.WithValue(
r.Context(), r.Context(),
KeyValues, KeyValues,
@ -183,8 +177,10 @@ func (h *Handler) ServeHTTP(
Markdown: h.md, Markdown: h.md,
IsIndex: isIndex, IsIndex: isIndex,
IndexSuffix: h.indexSuffix, IndexSuffix: h.indexSuffix,
Ext: h.ext, PPExt: h.ext,
SourcePath: h.sourcePath, SourcePath: h.sourcePath,
FPath: fpath,
FExt: fext,
}, },
) )
@ -193,22 +189,32 @@ func (h *Handler) ServeHTTP(
fileExt := filepath.Ext(filePath) fileExt := filepath.Ext(filePath)
contentType := mime.TypeByExtension(fileExt) contentType := mime.TypeByExtension(fileExt)
w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Type", contentType)
processedData := fileData
var buf bytes.Buffer
var reader io.Reader
if shouldProcess { if shouldProcess {
_, processedData, err = h.pp.Process( compiled, err := h.pp.Compile(
ctx, ctx, file,
filePathTpp,
fileData,
) )
if err != nil { if err != nil {
http.NotFound(w, r) log.Printf("gopp.Compile(...): %s\n", err)
log.Printf( http.Error(w, err.Error(), 500)
"Error: pp.Process(...): %s\n",
err,
)
return 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 "log"
import "net/http" import "net/http"

View file

@ -2,16 +2,16 @@ package mdx
import "github.com/gomarkdown/markdown" import "github.com/gomarkdown/markdown"
import "github.com/gomarkdown/markdown/html" import "github.com/gomarkdown/markdown/html"
import "github.com/gomarkdown/markdown/parser" import "github.com/gomarkdown/markdown/parser"
import "github.com/d5/tengo/v2" import "surdeus.su/core/xgo/v2"
import "bytes" import "bytes"
func GetModule() tengo.Importable { func GetModule() xgo.Importable {
return &tengo.BuiltinModule{Attrs:map[string]tengo.Object{ return &xgo.BuiltinModule{Attrs:map[string]xgo.Object{
"new": &tengo.BuiltinFunction{ "new": &xgo.BuiltinFunction{
Name: "new", Name: "new",
Value: func( Value: func(
args ...tengo.Object, args ...xgo.Object,
) (tengo.Object, error){ ) (xgo.Object, error){
return MakeDefaultMarkdown(), nil return MakeDefaultMarkdown(), nil
}, },
}, },
@ -19,7 +19,7 @@ func GetModule() tengo.Importable {
} }
type Markdown struct { type Markdown struct {
tengo.ObjectImpl xgo.ObjectImpl
MakeParser func() *parser.Parser MakeParser func() *parser.Parser
MakeRenderer func() *html.Renderer MakeRenderer func() *html.Renderer
} }
@ -55,13 +55,13 @@ func (md *Markdown) CanCall() bool {
} }
func (md *Markdown) Call( func (md *Markdown) Call(
args ...tengo.Object, args ...xgo.Object,
) (tengo.Object, error) { ) (xgo.Object, error) {
var b bytes.Buffer var b bytes.Buffer
for _, arg := range args { for _, arg := range args {
str, ok := tengo.ToString(arg) str, ok := xgo.ToString(arg)
if !ok { if !ok {
return nil, tengo.ErrInvalidArgumentType{ return nil, xgo.ErrInvalidArgumentType{
Name: "v", Name: "v",
Expected: "stringer", Expected: "stringer",
Found: arg.TypeName(), Found: arg.TypeName(),
@ -75,7 +75,7 @@ func (md *Markdown) Call(
return nil, err return nil, err
} }
return tengo.FromInterface(rendered) return xgo.FromInterface(rendered)
} }
func MakeDefaultParser() *parser.Parser { func MakeDefaultParser() *parser.Parser {

View file

@ -1,4 +1,3 @@
fmt := import("fmt")
paths := import("paths") paths := import("paths")
htmExt := func(c){ htmExt := func(c){
if c.is_compo { if c.is_compo {
@ -14,10 +13,7 @@ exts := {
} }
export func(c){ export func(c){
ext := paths.ext(c.pp.filepath) ext := c.fext
ext = paths.ext(
c.pp.filepath[:len(c.pp.filepath) - len(ext)]
)
cl := exts[ext] cl := exts[ext]
if !is_undefined(cl) { if !is_undefined(cl) {

View file

@ -1,6 +1,5 @@
fmt := import("fmt")
paths := import("paths") paths := import("paths")
html := import("html").new_render() html := import("html").Renderer()
htmExt := func(c){ htmExt := func(c){
if c.is_compo { if c.is_compo {
@ -20,6 +19,7 @@ htmExt := func(c){
)) ))
c.pp.print(`<body>`) c.pp.print(`<body>`)
c.pp.print(html.div().body(c.global.val))
} }
exts := { exts := {
@ -28,11 +28,7 @@ exts := {
} }
export func(c){ export func(c){
ext := paths.ext(c.pp.filepath) ext := c.fext
ext = paths.ext(
c.pp.filepath[:len(c.pp.filepath) - len(ext)]
)
cl := exts[ext] cl := exts[ext]
if cl { if cl {
cl(c) 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( h := html.main().body(

View file

@ -2,7 +2,7 @@
context.title = "The example.site main page" context.title = "The example.site main page"
fmt := import("fmt") 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 ( import (
"surdeus.su/core/xgo/v2"
"github.com/d5/tengo/v2" "surdeus.su/util/kyra/mdx"
"surdeus.su/util/tht/mdx"
"surdeus.su/core/cli/mtool" "surdeus.su/core/cli/mtool"
"net/http" "net/http"
"log" "log"
) )
// Simple PHP-like server implementation. // 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 ( var (
addr, ext, src, mod, index string addr, ext, src, mod, index string
noLog bool noLog bool
) )
flags.StringVar(&addr, "addr", ":3000", "address to serve at") flags.StringVar(&addr, "addr", ":3000", "address to serve at")
flags.StringVar(&mod, "mod", "./mod", "path to store Tengo modules") flags.StringVar(&mod, "mod", "./mod", "path to store Tengo modules")
flags.StringVar(&src, "src", "./src", "directory with source files") 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.StringVar(&index, "index", "index.htm", "index file name")
flags.BoolVar( flags.BoolVar(&noLog, "no-log", false, "disable logging")
&noLog,
"no-log",
false,
"disable logging",
)
flags.Parse() flags.Parse()
@ -35,7 +28,7 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
After: NewHandler( After: NewHandler(
DefaultPP(mod), DefaultPP(mod),
src, ext, index, src, ext, index,
map[string] tengo.Object{}, map[string]xgo.Object{},
).SetMD(mdx.MakeDefaultMarkdown()), ).SetMD(mdx.MakeDefaultMarkdown()),
} }
if !noLog { if !noLog {
@ -52,4 +45,8 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
if err != nil { if err != nil {
log.Printf("Error: srv.ListenAndServe(...): %s\n", err) 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.
`)