From 485235996ffcb52fa28ee0ae4ec31dd070d15316 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 22 May 2024 15:16:07 +0500 Subject: [PATCH] fix: fixed yet another subst problem. --- main.go | 33 +++++++++++++++++++++------------ mod/post.tengo | 3 +++ mod/pre.tengo | 7 ++++++- src/contact/edit.htm.tpp | 20 ++++++++++++++++++++ src/contact/view.htm.tpp | 13 +++++++++++++ src/main.htm.tpp | 2 +- src/page.htm.tpp | 4 ++-- tengo.go | 19 +++++++++++++++---- 8 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 src/contact/edit.htm.tpp create mode 100644 src/contact/view.htm.tpp diff --git a/main.go b/main.go index 6ec15f8..188474c 100644 --- a/main.go +++ b/main.go @@ -32,9 +32,21 @@ func (pp *Preprocessor) Process( data []byte, ) ([]byte, error) { var b bytes.Buffer - preCodes := [][]byte{} + preCode := []byte(nil) texts := [][]byte{} codes := [][]byte{} + pref := append(pp.tags[0], pp.preTag) + if bytes.HasPrefix(data, pref) { + idxEnd := bytes.Index(data, pp.tags[1]) + if idxEnd < 0 { + return nil, UnexpectedError{ + What: "pre-code start tag", + } + } + preCode = data[len(pref):idxEnd] + texts = append(texts, []byte{}) + data = data[idxEnd+len(pp.tags[1]):] + } for { idxStart := bytes.Index(data, pp.tags[0]) idxEnd := bytes.Index(data, pp.tags[1]) @@ -57,35 +69,32 @@ func (pp *Preprocessor) Process( texts = append(texts, text) code := data[idxStart+len(pp.tags[0]):idxEnd] - if len(code) > 0 && code[0] == pp.preTag { - code = code[1:] - preCodes = append(preCodes, code) - } else { - codes = append(codes, code) - } + codes = append(codes, code) data = data[idxEnd + len(pp.tags[1]):] /*if len(data) > 0 && data[0] == '\n' { data = data[1:] }*/ } + codeRets, err := pp.tengo.Eval( ctx, recompile, filePath, - preCodes, + preCode, codes, ) if err != nil { return nil, err } - for i, codeRet := range codeRets { + for i := range codeRets { b.Write(texts[i]) - b.Write(codeRet) + b.Write(codeRets[i]) + } + if len(texts) > len(codeRets) { + b.Write(texts[len(codeRets)]) } - - b.Write(texts[len(codeRets)]) return b.Bytes(), nil } diff --git a/mod/post.tengo b/mod/post.tengo index 12086f1..f73eb45 100644 --- a/mod/post.tengo +++ b/mod/post.tengo @@ -1,6 +1,9 @@ fmt := import("fmt") paths := import("paths") htmExt := func(c){ + if c.is_compo { + return + } c.pp.print(` `) diff --git a/mod/pre.tengo b/mod/pre.tengo index 5141dc4..b62c370 100644 --- a/mod/pre.tengo +++ b/mod/pre.tengo @@ -2,9 +2,14 @@ fmt := import("fmt") paths := import("paths") htmExt := func(c){ + if c.is_compo { + return + } + fmt.println("shit") c.pp.print(` + `) if c.title { c.pp.printf(`%s`, c.title) @@ -24,7 +29,7 @@ export func(c){ ext := paths.ext(url_path) cl := exts[ext] - if !is_undefined(cl) { + if cl { cl(c) } } diff --git a/src/contact/edit.htm.tpp b/src/contact/edit.htm.tpp new file mode 100644 index 0000000..b762aac --- /dev/null +++ b/src/contact/edit.htm.tpp @@ -0,0 +1,20 @@ +{{# + context.is_compo = true +}} + +
+
+ + +
+
+ + +
+
+ + +
+ + +
diff --git a/src/contact/view.htm.tpp b/src/contact/view.htm.tpp new file mode 100644 index 0000000..04fff93 --- /dev/null +++ b/src/contact/view.htm.tpp @@ -0,0 +1,13 @@ +{{# + //context.is_compo = true +}} + +
+
: Joe
+
: Blow
+
: joe@blow.com
+ +
+ diff --git a/src/main.htm.tpp b/src/main.htm.tpp index f3feef1..70fee3c 100644 --- a/src/main.htm.tpp +++ b/src/main.htm.tpp @@ -2,8 +2,8 @@ context.title = "The example.site main page" }} -

This is the example page for the THT

+

This is the example page for the THT

{{ /*if !context.global.int { context.global.int = 0 diff --git a/src/page.htm.tpp b/src/page.htm.tpp index 036c11c..1be8346 100644 --- a/src/page.htm.tpp +++ b/src/page.htm.tpp @@ -1,5 +1,5 @@ -{{ - context.title = "Some page title" +{{# + context.title = "Let's celebrate and suck, SOME DICK" }} {{markdown(` diff --git a/tengo.go b/tengo.go index 9683e9b..a1c8c59 100644 --- a/tengo.go +++ b/tengo.go @@ -48,7 +48,7 @@ func (pp *Tengo) Eval( ctx context.Context, recompile bool, filePath string, - preCodes [][]byte, + preCode []byte, codes [][]byte, ) ([][]byte, error) { var fullCodeBuf bytes.Buffer @@ -81,17 +81,28 @@ func (pp *Tengo) Eval( fmt.Fprint(&fullCodeBuf, retHead) - for _, preCode := range preCodes { - fmt.Fprintln(&fullCodeBuf, "\n" + string(preCode) + retSeparator) + if preCode != nil { + fmt.Fprintln( + &fullCodeBuf, + string(preCode)+"\n", + ) } + if pp.preCode != nil { fullCodeBuf.Write(pp.preCode(ctx)) + fullCodeBuf.Write([]byte(retSeparator)) } + for _, code := range codes { - fmt.Fprintln(&fullCodeBuf, "\n" + string(code) + retSeparator) + fmt.Fprintln( + &fullCodeBuf, + "\n" + string(code) + retSeparator, + ) } + if pp.postCode != nil { fullCodeBuf.Write(pp.postCode(ctx)) + fullCodeBuf.Write([]byte(retSeparator)) } script := tengo.NewScript(fullCodeBuf.Bytes())