diff --git a/.gitignore b/.gitignore index 783eacd..d128777 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /tpp /exe +Session.vim diff --git a/cat.webp b/cat.webp deleted file mode 100644 index 6b225c7..0000000 Binary files a/cat.webp and /dev/null differ diff --git a/go.mod b/go.mod index c317a32..2221189 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module surdeus.su/util/tpp go 1.21.7 require ( - github.com/d5/tengo/v2 v2.16.1 // indirect - vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 // indirect + github.com/d5/tengo/v2 v2.17.0 + github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 + surdeus.su/core/cli v0.0.2 ) diff --git a/go.sum b/go.sum index f57549c..cac8de3 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ -github.com/d5/tengo/v2 v2.16.1 h1:/N6dqiGu9toqANInZEOQMM8I06icdZnmb+81DG/lZdw= -github.com/d5/tengo/v2 v2.16.1/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8= -vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 h1:acA77oEg7hDuUchyBvw7scG9gayVLFAC9/CGuqyfLSA= -vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0/go.mod h1:rYu/sFWE3vUkDSSJCZt+K1aVaso0MYcZ+tmxQd4whdQ= +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.0.2 h1:RdHHk3/Fhwxz9PjaE+vTlCuF9KmhrmNUb5y4oqulrYI= +surdeus.su/core/cli v0.0.2/go.mod h1:UKwCmcSX+x7XX9aF3gOaaAaJcJA3gtUmL4vdnM43+fM= diff --git a/httpx/handler.go b/httpx/handler.go index 8d88835..c0b0161 100644 --- a/httpx/handler.go +++ b/httpx/handler.go @@ -2,6 +2,7 @@ package httpx import ( //"github.com/d5/tengo/v2" + "surdeus.su/util/tpp/mdx" "surdeus.su/util/tpp" "path/filepath" "net/http" @@ -34,6 +35,7 @@ type Handler struct { // request so you can keep the states // between requests. global any + md *mdx.Markdown } // Returns the new Handler with @@ -65,8 +67,12 @@ func DefaultPP(mod string) *tpp.Preprocessor { s.Add("__http_request__", ctx.Value(KeyRequest)) s.Add("__global__", ctx.Value(KeyGlobal)) + s.Add("__markdown__", ctx.Value(KeyMarkdown)) }).SetPreCode(func(ctx context.Context) []byte { return []byte(` + markdown := func(...args) { + pp.write_raw(__markdown__(args...)) + } http := immutable({ request : __http_request__ }) @@ -84,6 +90,11 @@ func DefaultPP(mod string) *tpp.Preprocessor { return tpp.New(t) } +func (h *Handler) SetMD(md *mdx.Markdown) *Handler { + h.md = md + return h +} + func (h *Handler) ServeHTTP( w http.ResponseWriter, r *http.Request, @@ -132,6 +143,12 @@ func (h *Handler) ServeHTTP( h.global, ) + ctx = context.WithValue( + ctx, + KeyMarkdown, + h.md, + ) + // Setting before the code to let it change own // content type. contentType := mime.TypeByExtension(urlExt) diff --git a/httpx/tool.go b/httpx/tool.go index 89afdd0..5600f1b 100644 --- a/httpx/tool.go +++ b/httpx/tool.go @@ -1,9 +1,11 @@ package httpx import ( + "github.com/d5/tengo/v2" //"github.com/d5/tengo/v2/stdlib" - "vultras.su/core/cli/mtool" + "surdeus.su/util/tpp/mdx" + "surdeus.su/core/cli/mtool" "net/http" "log" //"context" @@ -15,6 +17,7 @@ type CKey string const ( KeyRequest CKey = "http-request" KeyGlobal = "global" + KeyMarkdown = "markdown" ) // Simple PHP-like server implementation. @@ -37,8 +40,10 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { DefaultPP(mod), src, ext, map[string] tengo.Object{}, - ), + ).SetMD(mdx.MakeDefaultMarkdown()), } + + log.Printf("Listening on %q\n", addr) err := srv.ListenAndServe() if err != nil { log.Printf("Error: srv.ListenAndServe(...): %s\n", err) diff --git a/mdx/main.go b/mdx/main.go new file mode 100644 index 0000000..d5241da --- /dev/null +++ b/mdx/main.go @@ -0,0 +1,80 @@ +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 "bytes" + +type Markdown struct { + tengo.ObjectImpl + MakeParser func() *parser.Parser + MakeRenderer func() *html.Renderer +} + +func MakeDefaultMarkdown() *Markdown { + return &Markdown{ + MakeParser: MakeDefaultParser, + MakeRenderer: MakeDefaultRenderer, + } +} + +func (md *Markdown) TypeName() string { + return "*Markdown" +} + +func (md *Markdown) String() string { + if md == nil { + return "" + } + return "&Markdown{...}" +} + +func (md *Markdown) Render( + data []byte, +) ([]byte, error) { + doc := md.MakeParser().Parse(data) + rendered := markdown.Render(doc, md.MakeRenderer()) + return rendered, nil +} + +func (md *Markdown) CanCall() bool { + return md != nil +} + +func (md *Markdown) Call( + args ...tengo.Object, +) (tengo.Object, error) { + var b bytes.Buffer + for _, arg := range args { + bts, ok := tengo.ToByteSlice(arg) + if !ok { + return nil, tengo.ErrInvalidArgumentType{ + } + } + b.Write(bts) + } + + rendered, err := md.Render(b.Bytes()) + if err != nil { + return nil, err + } + + return tengo.FromInterface(rendered) +} + +func MakeDefaultParser() *parser.Parser { + return parser.NewWithExtensions( + parser.CommonExtensions | parser.AutoHeadingIDs | + parser.NoEmptyLineBeforeBlock | parser.Attributes | + parser.Tables, + ) +} + +func MakeDefaultRenderer() *html.Renderer { + return html.NewRenderer( + html.RendererOptions{ + Flags: html.CommonFlags | html.HrefTargetBlank, + }, + ) +} + diff --git a/mod/pre.tengo b/mod/pre.tengo index fee5bf5..5141dc4 100644 --- a/mod/pre.tengo +++ b/mod/pre.tengo @@ -6,7 +6,7 @@ htmExt := func(c){ `) - if !is_undefined(c.title) { + if c.title { c.pp.printf(`%s`, c.title) } c.pp.print(` diff --git a/run-tht.sh b/run-tht.sh new file mode 100755 index 0000000..646c7c1 --- /dev/null +++ b/run-tht.sh @@ -0,0 +1,4 @@ +#!/bin/sh +# +go build -o ./exe/ ./cmd/tht/ && \ + ./exe/tht diff --git a/src/page.htm.tpp b/src/page.htm.tpp new file mode 100644 index 0000000..036c11c --- /dev/null +++ b/src/page.htm.tpp @@ -0,0 +1,34 @@ +{{ + context.title = "Some page title" +}} + +{{markdown(` + +# Hello, World! + +This is the markdown example shit. + + func main() { + print("Fuck you") + } + +## Yet another level of header + +And even more text + +> Cheap is talk, show the code. + +(c) Linus Torvalds + +`)}} + +
+ Even more HTML +
+ +{{markdown(` +### Third level header + +Some shit + +`)}} diff --git a/tool.go b/tool.go index f677bd0..348bde8 100644 --- a/tool.go +++ b/tool.go @@ -3,7 +3,7 @@ package tpp import ( //"github.com/d5/tengo/v2" "github.com/d5/tengo/v2/stdlib" - "vultras.su/core/cli/mtool" + "surdeus.su/core/cli/mtool" //"fmt" "os" "log"