Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for template/if/range/with #6

Merged
merged 3 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: "1.20"
go-version: "1.23"
- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
Expand Down
27 changes: 11 additions & 16 deletions cmd/xtemplate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ module github.com/youthlin/t/cmd/xtemplate
go 1.17

require (
github.com/cockroachdb/errors v1.8.6
github.com/smartystreets/goconvey v1.6.4
github.com/youthlin/t v0.0.8
github.com/smartystreets/goconvey v1.8.1
github.com/youthlin/t v0.0.9
)

require (
github.com/Xuanwo/go-locale v1.0.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210803070921-b358b509191a // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/redact v1.1.1 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/Xuanwo/go-locale v1.1.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/text v0.3.7 // indirect
github.com/smarty/assertions v1.15.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
655 changes: 474 additions & 181 deletions cmd/xtemplate/go.sum

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions cmd/xtemplate/internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ func resolveTmpl(filename string, ctx *Context, tmpl *template.Template) {
}
root := tmpl.Tree.Root
for _, node := range root.Nodes {
// param.debugPrint(" > node=%#v", node)
ctx.debugPrint(" > node=%#v", node)
// comment 会被忽略,这里拿不到注释信息
if node.Type() == parse.NodeAction {
// 只需要关注 action 节点
switch node.Type() {
case parse.NodeAction:
actionNode := node.(*parse.ActionNode)
resolvePipe(filename, actionNode.Line, ctx, actionNode.Pipe)
case parse.NodeIf:
branchNode := node.(*parse.IfNode)
resolvePipe(filename, branchNode.Line, ctx, branchNode.Pipe)
case parse.NodeRange:
branchNode := node.(*parse.RangeNode)
resolvePipe(filename, branchNode.Line, ctx, branchNode.Pipe)
case parse.NodeWith:
withNode := node.(*parse.WithNode)
resolvePipe(filename, withNode.Line, ctx, withNode.Pipe)
case parse.NodeTemplate:
templateNode := node.(*parse.TemplateNode)
resolvePipe(filename, templateNode.Line, ctx, templateNode.Pipe)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/xtemplate/internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGlob(t *testing.T) {

func TestFile(t *testing.T) {
Convey("resolveOneFile", t, func() {
resolveOneFile("testdata/base.tmpl", &Context{
resolveOneFile("testdata/index.tmpl", &Context{
Param: &Param{
Left: "{{",
Right: "}}",
Expand All @@ -36,6 +36,7 @@ func TestFile(t *testing.T) {
})
})
}

func Test_run(t *testing.T) {
Convey("run", t, func() {
Run(&Param{
Expand All @@ -45,6 +46,7 @@ func Test_run(t *testing.T) {
Keyword: "T;X:1c,2;N:1,2;XN:1c,2,3",
Function: "T",
OutputFile: "-",
Debug: true,
})
})
}
11 changes: 7 additions & 4 deletions cmd/xtemplate/internal/testdata/base.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
<html>

<head>
<title>{{- .t.T "Hello, World" -}}</title>// FieldNode=[t, T], StringNode="txt"
</head>
{{- end -}}

{{- define "title" -}}
<title>{{.}}</title>
</head>
<body>
{{- end -}}

{{ "id2" | .XN "ctxt" "id" }}// Cmd | Cmd // StringNode | FieldNode=[XN], StringNode="ctxt", StringNOde="id"
{{ .args | .XN "ctxt" "id" "id2" }}// FieldNode=[args] | FieldNode=[XN], StringNode=ctxt, id, id2
{{ .XN "ctxt" "id" `id2` }}// FieldNode=[XN], StringNode=ctxt, id, id2
Expand All @@ -22,14 +26,13 @@
{{ T ("id") }}// Ident PipeNode=() 不支持
{{ or (T "id") (T "id") }}// Ident=or PipeNode=() PipeNode=()

{{- end -}}

1. PipeNode
2. Cmd.Args FieldNode[last] > StringNode
3. Cmd.Args Ident, Ident > String
4. Cmd.Args has PipeNode -> 1.

{{- define "footer" -}}
{{- define "footer" -}}
</body>

</html>
Expand Down
11 changes: 11 additions & 0 deletions cmd/xtemplate/internal/testdata/index.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{{- template "header" . -}}
{{- template "title" .T "Title" -}}
{{- /*Comments*/ -}}

{{with $foo := .T "foo"}}
{{$foo}}
{{end}}
{{if .T "ok"}}
{{end}}
{{range .T "range"}}
<li>{{.}}</li>
{{end}}

{{ .T "Hello, World" }}
{{ .T "Hello, %v" }}
Expand Down
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.23.5

use (
.
./cmd/xtemplate
)
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/youthlin/t v0.0.9/go.mod h1:rW0l6z4hnkQSWZ2MYatEDwE3XEsQ2eep8yCD1umwvWg=
Loading