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(`
-