feat: now able to insert Markdown.

This commit is contained in:
Andrey Parhomenko 2024-05-20 22:34:11 +05:00
parent 7c7d1f7766
commit 02a22eaea1
11 changed files with 154 additions and 10 deletions

1
.gitignore vendored
View file

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

BIN
cat.webp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

5
go.mod
View file

@ -3,6 +3,7 @@ module surdeus.su/util/tpp
go 1.21.7 go 1.21.7
require ( require (
github.com/d5/tengo/v2 v2.16.1 // indirect github.com/d5/tengo/v2 v2.17.0
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 // indirect github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
surdeus.su/core/cli v0.0.2
) )

10
go.sum
View file

@ -1,4 +1,6 @@
github.com/d5/tengo/v2 v2.16.1 h1:/N6dqiGu9toqANInZEOQMM8I06icdZnmb+81DG/lZdw= github.com/d5/tengo/v2 v2.17.0 h1:BWUN9NoJzw48jZKiYDXDIF3QrIVZRm1uV1gTzeZ2lqM=
github.com/d5/tengo/v2 v2.16.1/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8= github.com/d5/tengo/v2 v2.17.0/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8=
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 h1:acA77oEg7hDuUchyBvw7scG9gayVLFAC9/CGuqyfLSA= github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 h1:yEt5djSYb4iNtmV9iJGVday+i4e9u6Mrn5iP64HH5QM=
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0/go.mod h1:rYu/sFWE3vUkDSSJCZt+K1aVaso0MYcZ+tmxQd4whdQ= 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=

View file

@ -2,6 +2,7 @@ package httpx
import ( import (
//"github.com/d5/tengo/v2" //"github.com/d5/tengo/v2"
"surdeus.su/util/tpp/mdx"
"surdeus.su/util/tpp" "surdeus.su/util/tpp"
"path/filepath" "path/filepath"
"net/http" "net/http"
@ -34,6 +35,7 @@ type Handler struct {
// request so you can keep the states // request so you can keep the states
// between requests. // between requests.
global any global any
md *mdx.Markdown
} }
// Returns the new Handler with // Returns the new Handler with
@ -65,8 +67,12 @@ func DefaultPP(mod string) *tpp.Preprocessor {
s.Add("__http_request__", ctx.Value(KeyRequest)) s.Add("__http_request__", ctx.Value(KeyRequest))
s.Add("__global__", ctx.Value(KeyGlobal)) s.Add("__global__", ctx.Value(KeyGlobal))
s.Add("__markdown__", ctx.Value(KeyMarkdown))
}).SetPreCode(func(ctx context.Context) []byte { }).SetPreCode(func(ctx context.Context) []byte {
return []byte(` return []byte(`
markdown := func(...args) {
pp.write_raw(__markdown__(args...))
}
http := immutable({ http := immutable({
request : __http_request__ request : __http_request__
}) })
@ -84,6 +90,11 @@ func DefaultPP(mod string) *tpp.Preprocessor {
return tpp.New(t) return tpp.New(t)
} }
func (h *Handler) SetMD(md *mdx.Markdown) *Handler {
h.md = md
return h
}
func (h *Handler) ServeHTTP( func (h *Handler) ServeHTTP(
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
@ -132,6 +143,12 @@ func (h *Handler) ServeHTTP(
h.global, h.global,
) )
ctx = context.WithValue(
ctx,
KeyMarkdown,
h.md,
)
// Setting before the code to let it change own // Setting before the code to let it change own
// content type. // content type.
contentType := mime.TypeByExtension(urlExt) contentType := mime.TypeByExtension(urlExt)

View file

@ -1,9 +1,11 @@
package httpx package httpx
import ( import (
"github.com/d5/tengo/v2" "github.com/d5/tengo/v2"
//"github.com/d5/tengo/v2/stdlib" //"github.com/d5/tengo/v2/stdlib"
"vultras.su/core/cli/mtool" "surdeus.su/util/tpp/mdx"
"surdeus.su/core/cli/mtool"
"net/http" "net/http"
"log" "log"
//"context" //"context"
@ -15,6 +17,7 @@ type CKey string
const ( const (
KeyRequest CKey = "http-request" KeyRequest CKey = "http-request"
KeyGlobal = "global" KeyGlobal = "global"
KeyMarkdown = "markdown"
) )
// Simple PHP-like server implementation. // Simple PHP-like server implementation.
@ -37,8 +40,10 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
DefaultPP(mod), DefaultPP(mod),
src, ext, src, ext,
map[string] tengo.Object{}, map[string] tengo.Object{},
), ).SetMD(mdx.MakeDefaultMarkdown()),
} }
log.Printf("Listening on %q\n", addr)
err := srv.ListenAndServe() err := srv.ListenAndServe()
if err != nil { if err != nil {
log.Printf("Error: srv.ListenAndServe(...): %s\n", err) log.Printf("Error: srv.ListenAndServe(...): %s\n", err)

80
mdx/main.go Normal file
View file

@ -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 "<nil>"
}
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,
},
)
}

View file

@ -6,7 +6,7 @@ htmExt := func(c){
<!doctype html> <!doctype html>
<html><head> <html><head>
`) `)
if !is_undefined(c.title) { if c.title {
c.pp.printf(`<title>%s</title>`, c.title) c.pp.printf(`<title>%s</title>`, c.title)
} }
c.pp.print(` c.pp.print(`

4
run-tht.sh Executable file
View file

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

34
src/page.htm.tpp Normal file
View file

@ -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
`)}}
<div id="uniq">
Even <strong>more</strong> HTML
</div>
{{markdown(`
### Third level header
Some shit
`)}}

View file

@ -3,7 +3,7 @@ package tpp
import ( import (
//"github.com/d5/tengo/v2" //"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib" "github.com/d5/tengo/v2/stdlib"
"vultras.su/core/cli/mtool" "surdeus.su/core/cli/mtool"
//"fmt" //"fmt"
"os" "os"
"log" "log"