fix: fixed yet another subst problem.

This commit is contained in:
Andrey Parhomenko 2024-05-22 15:16:07 +05:00
parent 02a22eaea1
commit 485235996f
8 changed files with 81 additions and 20 deletions

29
main.go
View file

@ -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)
}
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)])
}
return b.Bytes(), nil
}

View file

@ -1,6 +1,9 @@
fmt := import("fmt")
paths := import("paths")
htmExt := func(c){
if c.is_compo {
return
}
c.pp.print(`
</body></html>
`)

View file

@ -2,9 +2,14 @@ fmt := import("fmt")
paths := import("paths")
htmExt := func(c){
if c.is_compo {
return
}
fmt.println("shit")
c.pp.print(`
<!doctype html>
<html><head>
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
`)
if c.title {
c.pp.printf(`<title>%s</title>`, 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)
}
}

20
src/contact/edit.htm.tpp Normal file
View file

@ -0,0 +1,20 @@
{{#
context.is_compo = true
}}
<form hx-put="/contact/1" hx-target="this" hx-swap="outerHTML">
<div>
<label>First Name</label>
<input type="text" name="firstName" value="Joe">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" value="Blow">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="joe@blow.com">
</div>
<button class="btn">Submit</button>
<button class="btn" hx-get="/contact/view.htm">Cancel</button>
</form>

13
src/contact/view.htm.tpp Normal file
View file

@ -0,0 +1,13 @@
{{#
//context.is_compo = true
}}
<div hx-target="this" hx-swap="outerHTML">
<div><label>First Name</label>: Joe</div>
<div><label>Last Name</label>: Blow</div>
<div><label>Email</label>: joe@blow.com</div>
<button hx-get="/contact/edit.htm" class="btn btn-primary">
Click To Edit
</button>
</div>

View file

@ -2,8 +2,8 @@
context.title = "The example.site main page"
}}
<h1>This is the example page for the THT</h1>
<h1>This is the example page for the THT</h1>
{{
/*if !context.global.int {
context.global.int = 0

View file

@ -1,5 +1,5 @@
{{
context.title = "Some page title"
{{#
context.title = "Let's celebrate and suck, SOME DICK"
}}
{{markdown(`

View file

@ -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())