feat: now able to insert Markdown.
This commit is contained in:
parent
7c7d1f7766
commit
02a22eaea1
11 changed files with 154 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/tpp
|
||||
/exe
|
||||
Session.vim
|
||||
|
|
BIN
cat.webp
BIN
cat.webp
Binary file not shown.
Before Width: | Height: | Size: 127 KiB |
5
go.mod
5
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
|
||||
)
|
||||
|
|
10
go.sum
10
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=
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
80
mdx/main.go
Normal file
80
mdx/main.go
Normal 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,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ htmExt := func(c){
|
|||
<!doctype html>
|
||||
<html><head>
|
||||
`)
|
||||
if !is_undefined(c.title) {
|
||||
if c.title {
|
||||
c.pp.printf(`<title>%s</title>`, c.title)
|
||||
}
|
||||
c.pp.print(`
|
||||
|
|
4
run-tht.sh
Executable file
4
run-tht.sh
Executable file
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
go build -o ./exe/ ./cmd/tht/ && \
|
||||
./exe/tht
|
34
src/page.htm.tpp
Normal file
34
src/page.htm.tpp
Normal 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
|
||||
|
||||
`)}}
|
2
tool.go
2
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"
|
||||
|
|
Loading…
Reference in a new issue