From 625d73689ac9bb94e89c06dd411ad0ebc720ef85 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 18 May 2024 01:55:02 +0500 Subject: [PATCH] fix: fixed the wrong order of rendering. --- httpx/handler.go | 45 ++++++++++++++++++++++++++++++--------------- httpx/tool.go | 6 ++---- httpx/url.go | 4 ++-- main.go | 21 +++++++++++++++------ mod/pre.tengo | 6 +++++- src/main.htm.tpp | 28 ++++++++++++++++++++-------- src/request.json | 3 +++ src/script.js | 3 +++ tengo.go | 6 +++++- 9 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 src/request.json create mode 100644 src/script.js diff --git a/httpx/handler.go b/httpx/handler.go index f85eccf..9fb64e1 100644 --- a/httpx/handler.go +++ b/httpx/handler.go @@ -48,6 +48,7 @@ func (h *Handler) ServeHTTP( w http.ResponseWriter, r *http.Request, ) { + shouldProcess := true urlPath := r.URL.Path // Cleaning URL path to prevent injections. urlPath = path.Clean(urlPath) @@ -62,8 +63,12 @@ func (h *Handler) ServeHTTP( //log.Println("pth:", urlPath, filePathTpp) file, err := os.Open(filePathTpp) if err != nil { - http.NotFound(w, r) - return + shouldProcess = false + file, err = os.Open(filePath) + if err != nil { + http.NotFound(w, r) + return + } } //process := true @@ -73,25 +78,35 @@ func (h *Handler) ServeHTTP( return } - ctx := context.WithValue(r.Context(), KeyRequest, &Request{ - Request: r, - }) + ctx := context.WithValue( + r.Context(), + KeyRequest, + &Request{ + Request: r, + }, + ) // Setting before the code to let it change own // content type. contentType := mime.TypeByExtension(urlExt) w.Header().Set("Content-Type", contentType) - processedData, err := h.pp.Process( - ctx, - true, - filePathTpp, - fileData, - ) - if err != nil { - http.NotFound(w, r) - log.Printf("Error: pp.Process(...): %s\n", err) - return + processedData := fileData + if shouldProcess { + processedData, err = h.pp.Process( + ctx, + true, + filePathTpp, + fileData, + ) + if err != nil { + http.NotFound(w, r) + log.Printf( + "Error: pp.Process(...): %s\n", + err, + ) + return + } } w.Write(processedData) diff --git a/httpx/tool.go b/httpx/tool.go index 4cc613a..cbcfbb9 100644 --- a/httpx/tool.go +++ b/httpx/tool.go @@ -44,10 +44,8 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { http := immutable({ request : __http_request__ }) - __context__ := { - http: http, - pp: pp - } + __context__.http = http + __context__.pp = pp import("./pre")(__context__) `) }).SetPostCode(func(ctx context.Context) []byte { diff --git a/httpx/url.go b/httpx/url.go index a8cf06a..1e0727e 100644 --- a/httpx/url.go +++ b/httpx/url.go @@ -66,9 +66,9 @@ func (u *URL) IndexGet( case "path" : return tengo.FromInterface(u.Path) case "query" : - return tengo.FromInterface(&Values{ + return &Values{ Values: u.Query(), - }) + }, nil } // Nothing found. diff --git a/main.go b/main.go index a4f0c25..6ec15f8 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( type Preprocessor struct { tengo *Tengo tags [2][]byte + preTag byte } // Get the new preprocessor with default options. @@ -20,6 +21,7 @@ func New(tengo *Tengo ) *Preprocessor { []byte("{{"), []byte("}}"), } + pp.preTag = '#' return pp } @@ -30,12 +32,12 @@ func (pp *Preprocessor) Process( data []byte, ) ([]byte, error) { var b bytes.Buffer - last := 0 + preCodes := [][]byte{} texts := [][]byte{} codes := [][]byte{} for { - idxStart := bytes.Index(data[last:], pp.tags[0]) - idxEnd := bytes.Index(data[last:], pp.tags[1]) + idxStart := bytes.Index(data, pp.tags[0]) + idxEnd := bytes.Index(data, pp.tags[1]) //fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd) if idxStart < 0 { if idxEnd >= 0 { @@ -43,18 +45,24 @@ func (pp *Preprocessor) Process( What: "end tag", } } - texts = append(texts, data[last:]) + texts = append(texts, data) break } else if idxEnd < 0 { return nil, UnexpectedError{ What: "start tag", } } - text := data[last:idxStart] + + text := data[:idxStart] texts = append(texts, text) code := data[idxStart+len(pp.tags[0]):idxEnd] - codes = append(codes, code) + if len(code) > 0 && code[0] == pp.preTag { + code = code[1:] + preCodes = append(preCodes, code) + } else { + codes = append(codes, code) + } data = data[idxEnd + len(pp.tags[1]):] /*if len(data) > 0 && data[0] == '\n' { @@ -65,6 +73,7 @@ func (pp *Preprocessor) Process( ctx, recompile, filePath, + preCodes, codes, ) if err != nil { diff --git a/mod/pre.tengo b/mod/pre.tengo index e7c7c7c..fee5bf5 100644 --- a/mod/pre.tengo +++ b/mod/pre.tengo @@ -5,7 +5,11 @@ htmExt := func(c){ c.pp.print(` - Check shit + `) + if !is_undefined(c.title) { + c.pp.printf(`%s`, c.title) + } + c.pp.print(` `) } diff --git a/src/main.htm.tpp b/src/main.htm.tpp index 343e5b4..03f6537 100644 --- a/src/main.htm.tpp +++ b/src/main.htm.tpp @@ -1,16 +1,24 @@ +{{# + __context__.title = "The example.site main page" +}} + +

This is the example page for the THT

+ {{ fmt := import("fmt") req := http.request q := req.url.query // List checking. - list := [1, 2, 3, 4, 123] - body := req.body - if body { - fmt.println(string(body)) + list := [] + rng := int(q.range[0]) + fmt.println("range:", q.range) + if rng { + pp.print(`") } -}} - -{{ if q.name { pp.print("
", q.name[0], "
") } @@ -18,7 +26,7 @@
Hello, Cock!
-
+
{{ pp.print(req.url.path) }} @@ -28,3 +36,7 @@ pp.print("
  • ", v, "
  • ") } }} + + + +
    diff --git a/src/request.json b/src/request.json new file mode 100644 index 0000000..3b0cd87 --- /dev/null +++ b/src/request.json @@ -0,0 +1,3 @@ +{ + "name":"Andrew" +} diff --git a/src/script.js b/src/script.js new file mode 100644 index 0000000..f0d1a35 --- /dev/null +++ b/src/script.js @@ -0,0 +1,3 @@ + +console.log("Hello, World!") + diff --git a/tengo.go b/tengo.go index 8eff28f..46476b4 100644 --- a/tengo.go +++ b/tengo.go @@ -48,10 +48,12 @@ func (pp *Tengo) Eval( ctx context.Context, recompile bool, filePath string, + preCodes [][]byte, codes [][]byte, ) ([][]byte, error) { var fullCodeBuf bytes.Buffer const retHead = ` + __context__ := {} __ret_one__ := bytes("") pp := immutable({ filepath: __filepath__, @@ -69,7 +71,6 @@ func (pp *Tengo) Eval( __ret_one__ += bytes(bts) } }) - ` const retSeparator = ` @@ -80,6 +81,9 @@ func (pp *Tengo) Eval( fmt.Fprint(&fullCodeBuf, retHead) + for _, preCode := range preCodes { + fmt.Fprintln(&fullCodeBuf, "\n" + string(preCode) + retSeparator) + } if pp.preCode != nil { fullCodeBuf.Write(pp.preCode(ctx)) }