2024-08-02 17:00:46 +03:00
|
|
|
package gopp
|
2024-02-26 00:22:09 +03:00
|
|
|
|
|
|
|
import (
|
2024-04-25 22:38:04 +03:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-08-02 17:00:46 +03:00
|
|
|
"io"
|
2024-02-26 00:22:09 +03:00
|
|
|
)
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
type Parsed struct {
|
|
|
|
Texts [][]byte
|
|
|
|
PreCode []byte
|
|
|
|
Codes [][]byte
|
|
|
|
}
|
2024-04-25 22:38:04 +03:00
|
|
|
|
2024-02-26 00:22:09 +03:00
|
|
|
type Preprocessor struct {
|
2024-08-02 17:00:46 +03:00
|
|
|
xgo *XGo
|
2024-04-25 22:38:04 +03:00
|
|
|
tags [2][]byte
|
2024-05-17 23:55:02 +03:00
|
|
|
preTag byte
|
2024-02-26 00:22:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new preprocessor with default options.
|
2024-08-02 17:00:46 +03:00
|
|
|
func New(xgo *XGo) *Preprocessor {
|
2024-04-25 22:38:04 +03:00
|
|
|
pp := &Preprocessor{}
|
2024-08-02 17:00:46 +03:00
|
|
|
pp.xgo = xgo
|
2024-04-25 22:38:04 +03:00
|
|
|
pp.tags = [2][]byte{
|
|
|
|
[]byte("{{"),
|
|
|
|
[]byte("}}"),
|
2024-02-26 00:22:09 +03:00
|
|
|
}
|
2024-05-17 23:55:02 +03:00
|
|
|
pp.preTag = '#'
|
2024-02-26 00:22:09 +03:00
|
|
|
return pp
|
|
|
|
}
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
func (pp *Preprocessor) XGo() *XGo {
|
|
|
|
return pp.xgo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *Preprocessor) Parse(
|
2024-04-25 22:38:04 +03:00
|
|
|
ctx context.Context,
|
2024-08-02 17:00:46 +03:00
|
|
|
input io.Reader,
|
|
|
|
) (*Parsed, error) {
|
|
|
|
data, err := io.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-09 14:51:03 +03:00
|
|
|
//var b bytes.Buffer
|
2024-05-22 13:16:07 +03:00
|
|
|
preCode := []byte(nil)
|
2024-04-25 22:38:04 +03:00
|
|
|
texts := [][]byte{}
|
|
|
|
codes := [][]byte{}
|
2024-05-22 13:16:07 +03:00
|
|
|
pref := append(pp.tags[0], pp.preTag)
|
|
|
|
if bytes.HasPrefix(data, pref) {
|
|
|
|
idxEnd := bytes.Index(data, pp.tags[1])
|
|
|
|
if idxEnd < 0 {
|
2024-08-02 17:00:46 +03:00
|
|
|
return nil, UnexpectedError{
|
2024-05-22 13:16:07 +03:00
|
|
|
What: "pre-code start tag",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
preCode = data[len(pref):idxEnd]
|
2024-06-09 15:51:45 +03:00
|
|
|
//texts = append(texts, []byte{})
|
2024-05-22 13:16:07 +03:00
|
|
|
data = data[idxEnd+len(pp.tags[1]):]
|
|
|
|
}
|
2024-02-26 00:22:09 +03:00
|
|
|
for {
|
2024-05-17 23:55:02 +03:00
|
|
|
idxStart := bytes.Index(data, pp.tags[0])
|
|
|
|
idxEnd := bytes.Index(data, pp.tags[1])
|
2024-02-26 00:22:09 +03:00
|
|
|
//fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd)
|
|
|
|
if idxStart < 0 {
|
|
|
|
if idxEnd >= 0 {
|
2024-08-02 17:00:46 +03:00
|
|
|
return nil, UnexpectedError{
|
2024-02-26 00:22:09 +03:00
|
|
|
What: "end tag",
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 23:55:02 +03:00
|
|
|
texts = append(texts, data)
|
2024-02-26 00:22:09 +03:00
|
|
|
break
|
|
|
|
} else if idxEnd < 0 {
|
2024-08-02 17:00:46 +03:00
|
|
|
return nil, UnexpectedError{
|
2024-02-26 00:22:09 +03:00
|
|
|
What: "start tag",
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 23:55:02 +03:00
|
|
|
|
|
|
|
text := data[:idxStart]
|
2024-02-26 20:16:12 +03:00
|
|
|
texts = append(texts, text)
|
|
|
|
|
2024-02-26 00:22:09 +03:00
|
|
|
code := data[idxStart+len(pp.tags[0]):idxEnd]
|
2024-05-22 13:16:07 +03:00
|
|
|
codes = append(codes, code)
|
2024-02-26 20:16:12 +03:00
|
|
|
|
|
|
|
data = data[idxEnd + len(pp.tags[1]):]
|
|
|
|
}
|
2024-05-22 13:16:07 +03:00
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
return &Parsed{
|
|
|
|
Texts: texts,
|
|
|
|
PreCode: preCode,
|
|
|
|
Codes: codes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *Preprocessor) Render(
|
|
|
|
ctx context.Context,
|
|
|
|
input io.Reader,
|
|
|
|
output io.Writer,
|
|
|
|
) error {
|
|
|
|
parsed, err := pp.Parse(ctx, input)
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
return pp.xgo.Render(ctx, parsed, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *Preprocessor) Compile(
|
|
|
|
ctx context.Context,
|
|
|
|
input io.Reader,
|
|
|
|
) (*Compiled, error) {
|
|
|
|
parsed, err := pp.Parse(ctx, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
compiled, err := pp.xgo.Compile(
|
2024-04-25 22:38:04 +03:00
|
|
|
ctx,
|
2024-08-02 17:00:46 +03:00
|
|
|
parsed,
|
2024-04-25 22:38:04 +03:00
|
|
|
)
|
2024-02-26 20:16:12 +03:00
|
|
|
if err != nil {
|
2024-08-02 17:00:46 +03:00
|
|
|
return nil, err
|
2024-02-26 20:16:12 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 17:00:46 +03:00
|
|
|
return compiled, nil
|
2024-02-26 00:22:09 +03:00
|
|
|
}
|
|
|
|
|